window.onload = function(){
	load_geolocalisation();
}

window.onunload = function(){
	GUnload();
}


	var map = null;
	var geocoder = null;
	var ref_bulle_active = null;

	
	// **** INITIALISATIONS AU CHARGEMENT DE LA PAGE **************************************************
    function load_geolocalisation() {
	  if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		//
			function TextualZoomControl() {}
		TextualZoomControl.prototype = new GControl();
		
		// Crée un div pour chaque bouton et on les place dans un container
		TextualZoomControl.prototype.initialize = function(map) {
		  var container = document.createElement("div");
		
		  var size_of_direction_label = 25;
		  var width = map.getSize().width;
		  var middle_w = 0;
		  if (width > 0) middle_w = width/2 - size_of_direction_label;
		
		  var height = map.getSize().height;
		  var middle_height = 0;
		  if (height > 0) middle_height = height/2;
				
			//zoom in
		  var zoomInDiv = document.createElement("div");
		  this.setButtonStyle2_(zoomInDiv, height - height, middle_w - middle_w);
		  container.appendChild(zoomInDiv);
		  var img_zoomin = document.createElement('img');
		  img_zoomin.src = 'images/geoloc_plus.png';
		  zoomInDiv.appendChild(img_zoomin);
		  GEvent.addDomListener(zoomInDiv, "click", function() {map.zoomIn();});

			
			//zoom out
		  var zoomOutDiv = document.createElement("div");
		  this.setButtonStyle2_(zoomOutDiv, height - height + 32 , middle_w - middle_w);
		  container.appendChild(zoomOutDiv);
		  var img_zoomout = document.createElement('img');
		  img_zoomout.src = 'images/geoloc_moins.png';
		  zoomOutDiv.appendChild(img_zoomout);
		  GEvent.addDomListener(zoomOutDiv, "click", function() {map.zoomOut();});
		
		  map.getContainer().appendChild(container);
		  return container;
		}

		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		TextualZoomControl.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 14));
		}
		
		// Sets the proper CSS for the given button element.
		TextualZoomControl.prototype.setButtonStyle_ = function(button,img) {
		  button.style.textDecoration = "underline";
		  button.style.font = "small Arial";
		  button.style.padding = "2px";
		  button.style.marginBottom = "10px";
		  button.style.textAlign = "center";
		  button.style.width = "24px";
		  button.style.height = "31px";
		  button.style.cursor = "pointer";
		  button.style.background = "url("+img+") top left no-repeat";
		}
		TextualZoomControl.prototype.setButtonStyle2_ = function(button, top_pos, left_pos) {
		  button.style.top = top_pos + "px";
		  button.style.left = left_pos + "px";
		  button.style.position = "absolute";
		  button.style.cursor = "pointer";
		}

		// initialement, se positionne sur le niveau 15 de zoom
		nivzoom = 15;
		// Par d̩efaut, se place au centre de toulouse
		lat_toulouse = '43.604573';
		lng_toulouse = '1.445646';
		map.enableDoubleClickZoom();//zoom au double click
		// ajout des options personnalisees de zooms	
		map.addControl(new TextualZoomControl());
       	map.setCenter(new GLatLng(lat_toulouse,lng_toulouse), nivzoom);
		// affiche les markers
		affiche_markers ();			
		if (zoom_geoloc != null) {
			zoom_geoloc_resto (zoom_geoloc[0], zoom_geoloc[1], zoom_geoloc[2]);
		} else {
			nivzoom = 11;
			map.setCenter(new GLatLng(lat_toulouse,lng_toulouse), nivzoom);
		}
	  }
	}	
	
	
	// **** AFFICHE LES MARKERS DE GEOLOCALISATION **************************************************
	function affiche_markers () {
	if (GBrowserIsCompatible()) {
		map.clearOverlays();
    	geocoder = new GClientGeocoder();
		if (liste_markers.length > 0) {
			for (var i = 0; i < liste_markers.length; i++) {
				var point = new GLatLng(parseFloat(liste_markers[i][0]), parseFloat(liste_markers[i][1]));
				idresto = liste_markers[i][2];		
				var marker = createMarker(point, idresto);
				// calcule le rectangle de zoom
				//if (i == 0) {
				//	coordonnees_max = new Array(point.lat(),point.lng(),point.lat(),point.lng());
				//} else {
				//	if (point.lat() < coordonnees_max[0]) { coordonnees_max[0] = point.lat(); }
				//	else { if (point.lat() > coordonnees_max[2]) { coordonnees_max[2] = point.lat(); } }
				//	if (point.lng() < coordonnees_max[1]) { coordonnees_max[1] = point.lng(); }
				//	else { if (point.lng() > coordonnees_max[3]) { coordonnees_max[3] = point.lng(); } }
				//}
				// ajoute marker
				map.addOverlay(marker);
				//coordonnees_max = new Array(point.lat(),point.lng(),point.lat(),point.lng());
				
			}
			// en mode fiche detaillee, zoom sur resto en cours
			if (openbulle) { map.setCenter(marker.getPoint(),15); }
			//zoomZoneObservations(coordonnees_max);

		}
      }
    }


	// **** CREATION DU MARKER CORRESPONDANT AU POINT EN PARAMETRE ***************************************
    function createMarker(point,idresto) {
		var iconresto = new GIcon(); 
    	iconresto.image = 'http://www.prixlucienvanel.com/images/marker_resto.png';
    	iconresto.shadow = 'http://www.prixlucienvanel.com/images/marker_shadow.png';
   	 	iconresto.iconSize = new GSize(25, 40);
   	 	iconresto.shadowSize = new GSize(44, 40);
    	iconresto.iconAnchor = new GPoint(12, 34);
   	 	iconresto.infoWindowAnchor = new GPoint(12, 1);
		iconresto.infoShadowAnchor = new GPoint(12, 34);
		// affiche marker
		var marker = new GMarker(point, iconresto);
	  	//if (openbulle) { open_infobulle("infobulle"+idresto); }
		if (openbulle) { zoom_geoloc_resto ("infobulle"+idresto, point.lat(),point.lng()); }
      	GEvent.addListener(marker, 'click', function() {
			zoom_geoloc_resto ("infobulle"+idresto, point.lat(),point.lng());										 
			//open_infobulle("infobulle"+idresto);
 			//nivzoom = 15;
			//map.setCenter(marker.getPoint(),nivzoom);
			//map.setCenter(new GLatLng(point.lat(),point.lng()), nivzoom);
     });
      return marker;
    }


	// **** ZOOM SUR LA ZONE QUI CONTIENT TOUTES LES OBSERVATIONS A AFFICHER *****************************
	function zoomZoneObservations (coordonnees) {
		ecart_x = coordonnees[2] - coordonnees[0];
		centre_x = coordonnees[0] + (ecart_x / 2);
		ecart_y = coordonnees[3] - coordonnees[1];
		centre_y = coordonnees[1] + (ecart_y / 2);
		plus_grand_ecart = Math.max(ecart_x,ecart_y);
		// alert(plus_grand_ecart);
		if (plus_grand_ecart < 0.2) {
			nivzoom = 15;
		} else {
			if (plus_grand_ecart < 0.4) {
				nivzoom = 12;
			} else {
				nivzoom = 10;
			}
		}
		map.setCenter(new GLatLng(centre_x,centre_y), nivzoom);
	}

	// **** INFOBULLES MARKERS *****************************
	function close_infobulle(ref) { 
		var infobulle = document.getElementById(ref);
		infobulle.style.visibility = "hidden";
		infobulle.style.left =  -1000 + "px";
	}
	
	function open_infobulle(ref) {
		if (ref_bulle_active != null) { close_infobulle(ref_bulle_active); }
		var infobulle = document.getElementById(ref);
		infobulle.style.visibility = "visible";
		infobulle.style.left =  80 + "px";
		infobulle.style.top = 20 + "px";
		ref_bulle_active = ref;
	}
	
	// **** ZOOM SUR UN RESTO EN MODE LISTE *****************************
	function zoom_geoloc_resto (ref, latitude, longitude) {
		map.setCenter(new GLatLng(latitude,longitude),15);
		open_infobulle(ref);
	}
//function myclick(i) {
//        map.setCenter(gmarkers[i].getPoint(),map.getZoom());
//        gmarkers[i].openInfoWindowHtml(htmls[i]);

//}

