//											Ce module gère l'ensemble du domaine coté utilisateur de la Google Map
//											¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// Si le navigateur est compatible
if (GBrowserIsCompatible()){


//			Les variables Globales
//			¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

	var map; // La Google Map
	var nbResultats = 0; // Le nombre de fiche affiché
	var typeCoche = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];  // Variable des types qui sont coché Restaurateurs Vignerons Autres
	
	var tabIcon = []; // Variable pour les icons des indicateurs
	var tabIconImg = [];
	var lesIndicateurs = []; // Tableau qui contient tous les Markers
	var infoIndicateur = [];
	var mapMinZoom = 9;
	var mapMaxZoom = 11;
 	var map;
	var hybridOverlay;
	var opacity = 1;
	//var mapBounds = new GLatLngBounds(new GLatLng(42.3458546407, 1.51954650879), new GLatLng(43.5808933172, 3.53555121979));
	 var mapBounds = new GLatLngBounds(new GLatLng(42.3499191749, 1.2915802002), new GLatLng(43.7120661457, 3.71407291783));


//			Initialisation
//			¯¯¯¯¯¯¯¯¯¯¯¯¯¯

			 
		    function LoadGoogleMap() {

			       if (GBrowserIsCompatible()) {

			          // Bug in the Google Maps: Copyright for Overlay is not correctly displayed
			          var gcr = GMapType.prototype.getCopyrights;
			          GMapType.prototype.getCopyrights = function(bounds,zoom) {
			              return ["&copy;"].concat(gcr.call(this,bounds,zoom));
			          }

			          map = new GMap2( document.getElementById("CartesMap"));			         

			         var tilelayer = [new GTileLayer(GCopyrightCollection(''), mapMinZoom, mapMaxZoom)];
			          var mercator = new GMercatorProjection(mapMaxZoom+1);
			          tilelayer[0].getTileUrl = function(tile,zoom) {
			              if ((zoom < mapMinZoom) || (zoom > mapMaxZoom)) {
			                  return "http://www.auberges-rurales.fr/image/blank.jpg";
			              } 
			              var ymax = 1 << zoom;
			              var y = ymax - tile.y -1;
			              var tileBounds = new GLatLngBounds(
			                  mercator.fromPixelToLatLng( new GPoint( (tile.x)*256, (tile.y+1)*256 ) , zoom ),
			                  mercator.fromPixelToLatLng( new GPoint( (tile.x+1)*256, (tile.y)*256 ) , zoom )
			              );
			              if (mapBounds.intersects(tileBounds)) {
			                  //return "/tiles/"+zoom+"/"+tile.x+"/"+y+".png";
							  return "/tiles/"+zoom+"/"+tile.x+"/"+y+".jpg";
			              } else {
			                  return "http://www.auberges-rurales.fr/image/blank.jpg";
			              }
			          }
			         
					  var custommap = new GMapType(tilelayer, mercator, "Campus");
					  var opts2 = {
						  zoomInBtnTitle : "Zoomer",
						  zoomOutBtnTitle : "Dézoomer",
						  moveNorthBtnTitle : "Décaler vers le Haut",
						  moveSouthBtnTitle : "Décaler vers le Bas",
						  moveEastBtnTitle : "Décaler à droite",
						  moveWestBtnTitle : "Décaler à gauche",
						  homeBtnTitle : "Revenir à la position initiale"
						};

					  map.addMapType(custommap);
 					  //map.setCenter( mapBounds.getCenter(), 9, custommap);	
					  map.setCenter( new GLatLng(43.056313,2.449608), 9, custommap);	
					  map.addControl(new GLargeMapControl3D(opts2),new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(20,160))); 
			          //map.enableContinuousZoom();
			          //map.enableScrollWheelZoom();
					  
					  
					 
					  // ====== Restricting the range of Zoom Levels =====
					  // Get the list of map types      
					  var mt = map.getMapTypes();
					  // Overwrite the getMinimumResolution() and getMaximumResolution() methods
					  for (var i=0; i<mt.length; i++) {
						mt[i].getMinimumResolution = function() {return 9;}
						mt[i].getMaximumResolution = function() {return 11;}
					  }
					  
					  // Add a move listener to restrict the bounds range
					  GEvent.addListener(map, "move", function() {
						checkBounds();
					  });
				
					  // The allowed region which the whole map must be within
					  // obliger de faire en fonction du zoom sinon problème sur les bords.
					  //var allowedBounds9 = new GLatLngBounds(new GLatLng(42.819765,2.252712), new GLatLng(43.112195,2.781773));
					  var allowedBounds9 = new GLatLngBounds(new GLatLng(42.819765,2.252712), new GLatLng(43.38465563778664, 2.7875232696533203));
					  //var allowedBounds10 = new GLatLngBounds(new GLatLng(42.69327,2.016678), new GLatLng(43.234065,3.056602));
					  var allowedBounds10 = new GLatLngBounds(new GLatLng(42.69327,1.916678), new GLatLng(43.404065,3.056602));
					 // var allowedBounds11 = new GLatLngBounds(new GLatLng(42.689233,1.920547), new GLatLng(43.374476,3.079948));
					  var allowedBounds11 = new GLatLngBounds(new GLatLng(42.689233,1.820547), new GLatLng(43.474476,3.179948));
					  //var allowedBounds12 = new GLatLngBounds(new GLatLng(42.689233,1.920547), new GLatLng(43.492152,3.133507));
					  //var allowedBounds12 = new GLatLngBounds(new GLatLng(42.689233,1.820547), new GLatLng(43.602152,3.133507));
					 
					  // If the map position is out of range, move it back
					  function checkBounds() {
						 var Zoom2 = map.getZoom();
						 //alert (Zoom2);
						 zone = eval('allowedBounds'+Zoom2);
						// Perform the check and return if OK
						if (zone.contains(map.getCenter())) {
						  return;
						}
						// It`s not OK, so find the nearest allowed point and move there
						var C = map.getCenter();
						
						var X = C.lng();
						var Y = C.lat();
				
						var AmaxX = zone.getNorthEast().lng();
						var AmaxY = zone.getNorthEast().lat();
						var AminX = zone.getSouthWest().lng();
						var AminY = zone.getSouthWest().lat();
				
						if (X < AminX) {X = AminX;}
						if (X > AmaxX) {X = AmaxX;}
						if (Y < AminY) {Y = AminY;}
						if (Y > AmaxY) {Y = AmaxY;}
						//alert ("Restricting "+Y+" "+X);
						map.setCenter(new GLatLng(Y,X),Zoom2);
					  }
					map.removeMapType(G_DEFAULT_MAP_TYPES);
					
					ActionClick(1); //restaurateur
					ActionClick(2); //vignerons
					if ($('#poles').val() == 1)	ActionClick('all'); //caunes
					else $('#CarteVignerons').attr("src","image/bouton-afficher-sites3.gif");
					//ActionClick(4); //fontfroide
					document.getElementById("nbResultats").innerHTML = nbResultats+' Auberge(s)';
			        $(".css_3").addClass("hidden"); // désactive affichage vignerons
			       }			      
			    }

//			Les fonctions
//			¯¯¯¯¯¯¯¯¯¯¯¯¯

	// La fonction pour parametrer et créer un Indicateur ( marker )
	function CreateIndicateur(numType, num){
		
		var type 		= infoType[numType];
		var id 			= infoIndicateur[numType][num][0];
		var longitude 	= infoIndicateur[numType][num][1];
		var latitude 	= infoIndicateur[numType][num][2];
		var nom 		= infoIndicateur[numType][num][3];
		var ville 		= infoIndicateur[numType][num][4];
		var adresse 	= infoIndicateur[numType][num][5];
		var cp 			= infoIndicateur[numType][num][6];
		var lien 		= infoIndicateur[numType][num][7];
		var tel 		= infoIndicateur[numType][num][8];

		var pos = new GLatLng(latitude, longitude);

		if (numType >=3) {
		// Le html de la petite fenêtre du marker
		var html = '';
		var html = html + '<span class="soumligne">' + nom + '</span><br />';
		if (adresse) var html = html + adresse + '<br />';
		var html = html + cp + ' ' + ville + '<br />';
		var html = html + tel + '<br />';
		//var html = html + '<a href="' + lien + '"><img src="/images/icon_info.png" alt="ic&ocirc;ne fiche complete" width="17" height="20" border="0" align="absmiddle" /> Voir la fiche complete</a><br />';
		}
		else
		{
			var html = '';
			var html = html + '<span class="soumligne">' + nom + '</span><br />';
			if (adresse) var html = html + adresse + '<br />';
			var html = html + cp + ' ' + ville + '<br />';			
			var html = html + '<a href="' + lien + '"><img src="/images/icon_info.png" alt="ic&ocirc;ne fiche complete" width="17" height="20" border="0" align="absmiddle" /> Voir la fiche complete</a><br />';
		}

		// En envois les infos à Google
		lesIndicateurs[id] = new GMarker(pos, tabIcon[type]);
		map.addOverlay(lesIndicateurs[id]);
		lesIndicateurs[id].value = id;
		//if (numType == 1) {
			GEvent.addListener(lesIndicateurs[id], "click", function() {
				lesIndicateurs[id].openInfoWindowHtml(html, {maxWidth:300});
			});
		//}

	}
	
	// En cas de clique de souris sur une catégorie ( ActionClick est déclenchée par Rollover )
	function ActionClick(numType) {
		
		// S’il existe
		if (numType == 'all')
		{
			for (var ij=3;ij<=21;ij++) //cas site poles
			{
				if(typeCoche[ij]) {
					
					// Alors on cache toute les cases du type
					for(var i = 0; i < infoIndicateur[ij].length; i++){
						var id = infoIndicateur[ij][i][0];
						map.removeOverlay(lesIndicateurs[id]);
						if (numType == 1) nbResultats--;
					}
					
					// Fonction Jquery qui ajoute le « display:none » à la class des fiches du type numType+1
					numCSS = ij+1;
					$(".css_"+numCSS).addClass("hidden");
					
					typeCoche[ij] = 0;
					$('#poles').val("0");
				}
				else 
				{					
					// Alors on affiche toute les cases du type
					//alert(infoIndicateur[ij].length+' '+ij);
					for(var i = 0; i < infoIndicateur[ij].length; i++){						
						CreateIndicateur(ij, i);
						if (ij == 1) nbResultats++;
					}
					
					// Fonction Jquery qui enlève le « display:none » à la class des fiches du type numType+1
					numCSS = ij+1;
					$(".css_"+numCSS).removeClass("hidden");
					
					typeCoche[ij] = 1;
					$('#poles').val("1");
				}
			}
		}
		else
		{
			if(typeCoche[numType]) {
					
					// Alors on cache toute les cases du type
					for(var i = 0; i < infoIndicateur[numType].length; i++){
						var id = infoIndicateur[numType][i][0];
						map.removeOverlay(lesIndicateurs[id]);
						if (numType == 1) nbResultats--;
					}
					
					// Fonction Jquery qui ajoute le « display:none » à la class des fiches du type numType+1
					numCSS = numType+1;
					$(".css_"+numCSS).addClass("hidden");
					
					typeCoche[numType] = 0;
			}
			else {
					
				// Alors on affiche toute les cases du type
				for(var i = 0; i < infoIndicateur[numType].length; i++){
					CreateIndicateur(numType, i);
					if (numType == 1) nbResultats++;
				}
				
				// Fonction Jquery qui enlève le « display:none » à la class des fiches du type numType+1
				numCSS = numType+1;
				$(".css_"+numCSS).removeClass("hidden");
				typeCoche[numType] = 1;
			}
		}

	}
	
	// Rollover déclenché en cas détection d'événement de souris sur les boutons de la catégorie
	function Rollover(action, num){
		if(action == 3){
			ActionClick(num);
			document.getElementById("nbResultats").innerHTML = nbResultats+' Auberge(s)';
		}
		switch (num){
			case 1 :
				if(action == 3){
					if(typeCoche[1]) document.getElementById("CarteRestaurateurs").src = "image/bouton-afficher-auberges1.gif";
					else document.getElementById("CarteRestaurateurs").src = "image/bouton-afficher-auberges2.gif";
				}
				if(!typeCoche[1]){
					if(action == 1) document.getElementById("CarteRestaurateurs").src = "image/bouton-afficher-auberges2.gif";
					if(action == 2) document.getElementById("CarteRestaurateurs").src = "image/bouton-afficher-auberges3.gif";
				}
			break;
			/*
			case 2 :
				if(action == 3) {
					if(typeCoche[2]) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites1.gif";
					else document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites2.gif";
				}
				if(!typeCoche[2]){
					if(action == 1) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites2.gif";
					if(action == 2) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites3.gif";
				}
			break;
			*/
			
			case 'all' :
				if(action == 3) {
					if(typeCoche[4]) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites1.gif";
					else document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites2.gif";
				}
				if(!typeCoche[4]){
					if(action == 1) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites2.gif";
					if(action == 2) document.getElementById("CarteVignerons").src = "image/bouton-afficher-sites3.gif";
				}
			break;
			
			default :
				if(action == 3) {
					if(typeCoche[0]) document.getElementById("CarteAutres").src = "images/Img-carte-autres3.gif";
					else document.getElementById("CarteAutres").src = "images/Img-carte-autres2.gif";
				}
				if(!typeCoche[0]){
					if(action == 1) document.getElementById("CarteAutres").src = "images/Img-carte-autres2.gif";
					if(action == 2) document.getElementById("CarteAutres").src = "images/Img-carte-autres1.gif";
				}
			break;
		}
		
	}

	// Rollover déclenché en cas détection d'événement de souris sur les boutons de la catégorie pour l'anglais
	function Rollover_en(action, num){
		if(action == 3){
			ActionClick(num);
			document.getElementById("nbResultats").innerHTML = nbResultats;
		}
		switch (num){
			case 1 :
				if(action == 3){
					if(typeCoche[1]) document.getElementById("CarteRestaurateurs").src = "/images/Img-carte-restaurateurs-en3.gif";
					else document.getElementById("CarteRestaurateurs").src = "/images/Img-carte-restaurateurs-en2.gif";
				}
				if(!typeCoche[1]){
					if(action == 1) document.getElementById("CarteRestaurateurs").src = "/images/Img-carte-restaurateurs-en2.gif";
					if(action == 2) document.getElementById("CarteRestaurateurs").src = "/images/Img-carte-restaurateurs-en1.gif";
				}
			break;
			case 2 :
				if(action == 3) {
					if(typeCoche[2]) document.getElementById("CarteVignerons").src = "/images/Img-carte-vignerons-en3.gif";
					else document.getElementById("CarteVignerons").src = "/images/Img-carte-vignerons-en2.gif";
				}
				if(!typeCoche[2]){
					if(action == 1) document.getElementById("CarteVignerons").src = "/images/Img-carte-vignerons-en2.gif";
					if(action == 2) document.getElementById("CarteVignerons").src = "/images/Img-carte-vignerons-en1.gif";
				}
			break;
			default :
				if(action == 3) {
					if(typeCoche[0]) document.getElementById("CarteAutres").src = "/images/Img-carte-autres-en3.gif";
					else document.getElementById("CarteAutres").src = "/images/Img-carte-autres-en2.gif";
				}
				if(!typeCoche[0]){
					if(action == 1) document.getElementById("CarteAutres").src = "/images/Img-carte-autres-en2.gif";
					if(action == 2) document.getElementById("CarteAutres").src = "/images/Img-carte-autres-en1.gif";
				}
			break;
		}
		
	}
	
	function ZoomCiteMedieval(){
		map.setCenter(new GLatLng(43.206, 2.363), 16);
	}
	
	function ZoomRoute(){
		map.setCenter(new GLatLng(43.252, 2.282), 8);
	}

}
else alert('Votre configuration ne permet pas d\'afficher la Google Map');
