//<![CDATA[
var map;
var marker;
var geocoder = new GClientGeocoder();
var tilelayers;
//--- Valeur par défaut à utiliser s'il n'y a pas de cookie
var lat = 45.67901739936284;
var lng = 3.287158203125;
var zoom = 8;
var MapTyp = 4;
//--- Paramètre du cookie
//------ Nom du cookie
var cookiename = "mapinfo";
//------ Délai d'expiration en nombre de jours
var expiredays = 1;
//--- Types d'informations à charger sur la carte
var ListTyp = "--";
//--- Position horizontale de la barre de chargement
var LeftPos;

//================================================================================================
//--- FONCTIONS DE GESTION DES CONTENUS DE LA CARTE
//================================================================================================

//-----------------------------------------------
//--- Fonction de gestion des types d'informations à charger sur la carte
function DataToLoad(Id,Check)
	{

	// Cas particulier pour fonctionnement avec Mozilla
	if (Check == true && navigator.appName=="Netscape")
		return;
		
	ListTyp = "--";
	if ((Check == true) && (document.getElementById("mapleg"+Id).checked == true))
		{ document.getElementById("mapleg"+Id).checked = false; }
	else if (Check == true)
		{ document.getElementById("mapleg"+Id).checked = true; }

	for (i=1; i<11; i++)
		{
		if (document.getElementById("mapleg"+i).checked == true)
			{
			ListTyp += i +"-";
			}
		}
	loadData();
	}

//-----------------------------------------------
//--- Fonction de chargement de la carte
function load()
	{
	if (GBrowserIsCompatible())
		{
		//--- Stockage de la position initiale de la barre de chargement
		LeftPos = document.getElementById("maploading").style.left;

		//--- Pointage de l'objet carte et définition des options d'affichage
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		var hierarchy = new GHierarchicalMapTypeControl(true);
		map.addControl(hierarchy);
		map.addControl(new GOverviewMapControl());

		//--- Paramétrage des cartes personnalisées
		var copyright = new GCopyright(1,new GLatLngBounds(new GLatLng(35.779017399363, -6.912841796875), new GLatLng(55.779017399363, 13.087158203125)), 0, "");
		var copyrightCollection = new GCopyrightCollection('');
		copyrightCollection.addCopyright(copyright);
		//------ Carte simple
		var tilelayers = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		var custommap = new GMapType(tilelayers, new GMercatorProjection(15), "Carte", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
		map.addMapType(custommap);
//		//------ Carte cyclotourisme
//		var tilelayersCyclo = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
//		tilelayersCyclo[0].getTileUrl = CustomGetTileUrlCyclo;
//		var custommapCyclo = new GMapType(tilelayersCyclo, new GMercatorProjection(15), "Cyclotourisme", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
//		map.addMapType(custommapCyclo);
//		hierarchy.addRelationship(custommap, custommapCyclo, "Itinéraires cyclotourisme", false);
		//------ Carte grands itinéraires cyclotouristiques
		var tilelayersCyclo1 = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
		tilelayersCyclo1[0].getTileUrl = CustomGetTileUrlCyclo1;
		var custommapCyclo1 = new GMapType(tilelayersCyclo1, new GMercatorProjection(15), "Boucles cyclotouristiques locales", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
		map.addMapType(custommapCyclo1);
		hierarchy.addRelationship(custommap, custommapCyclo1, "Boucles cyclotouristiques locales", false);
		//------ Carte boucles cyclotouristiques locales
		var tilelayersCyclo2 = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
		tilelayersCyclo2[0].getTileUrl = CustomGetTileUrlCyclo2;
		var custommapCyclo2 = new GMapType(tilelayersCyclo2, new GMercatorProjection(15), "Grands itinéraires cyclotouristiques", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
		map.addMapType(custommapCyclo2);
		hierarchy.addRelationship(custommap, custommapCyclo2, "Grands itinéraires cyclotouristiques", false);
		//------ Carte VTT
		var tilelayersVtt = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
		tilelayersVtt[0].getTileUrl = CustomGetTileUrlVtt;
		var custommapVtt = new GMapType(tilelayersVtt, new GMercatorProjection(15), "V.T.T.", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
		map.addMapType(custommapVtt);
		hierarchy.addRelationship(custommap, custommapVtt, "Itinéraires V.T.T.", false);
		//------ Carte voies vertes
		var tilelayersVV = [new GTileLayer(copyrightCollection, 7, 10), G_HYBRID_MAP.getTileLayers()[1]];
		tilelayersVV[0].getTileUrl = CustomGetTileUrlVV;
		var custommapVV = new GMapType(tilelayersVV, new GMercatorProjection(15), "Voies Vertes", {maxResolution:10, minResolution:7, errorMessage:"Aucun fond de carte disponible"});
		map.addMapType(custommapVV);
		hierarchy.addRelationship(custommap, custommapVV, "Itinéraires Voies Vertes", false);

		//--- Utilisation des données du cookie
		if (document.cookie.length>0)
			{
			cookieStart = document.cookie.indexOf(cookiename + "=");
			if (cookieStart!=-1)
				{
				cookieStart += cookiename.length+1; 
				cookieEnd=document.cookie.indexOf(";",cookieStart);
				if (cookieEnd==-1)
					{
					cookieEnd=document.cookie.length;
					}
				cookietext = document.cookie.substring(cookieStart,cookieEnd);
				//--- Récupération des données dans la chaîne texte du cookie
				bits = cookietext.split("|");
				lat = parseFloat(bits[0]);
				lng = parseFloat(bits[1]);
				zoom = parseInt(bits[2]);
				MapTyp = parseInt(bits[3]);
				ListTyp = bits[4];
				}
			}
		//--- Centrage de la carte
		map.setCenter(new GLatLng(lat,lng),zoom,map.getMapTypes()[MapTyp]);
		//--- Ajout du déclencheur au changement du type de carte
		GEvent.addListener(map, "maptypechanged", function()
			{
			//--- Mémorisation du type de carte
			getMapTyp();
			//--- Appel de la fonction de chargement
			loadData();
			});
		
		//--- Masquage de la barre de chargement
		document.getElementById("maploading").style.left = "-999em";

		//--- Chargement des données sur la carte
		if ((ListTyp != "--") || (MapTyp == 4) || (MapTyp == 5) || (MapTyp == 6) || (MapTyp == 7))
			{
			//--- Appel de la fonction de chargement
			loadData();
			//--- Coche des cases correspodantes dans le bloc de légende
			for (i=1; i<11; i++)
				{
				if (ListTyp.indexOf("-"+ i +"-") > 0)
					{
					document.getElementById("mapleg"+i).checked = true;
					}
				}
			}
		}
	}

//-----------------------------------------------
//--- Mémorisation du type de carte courante
function getMapTyp()
	{
	MapTyp = 0;
	for (var i=0;i<map.getMapTypes().length;i++) 
		{
		if (map.getCurrentMapType() == map.getMapTypes()[i])
			{
			MapTyp = i;
			}
		}
	}

//-----------------------------------------------
//--- mise à jour du cookie avant fermeture de la page
function setCookie()
	{
	MapTyp = 0;
	for (var i=0;i<map.getMapTypes().length;i++) 
		{
		if (map.getCurrentMapType() == map.getMapTypes()[i])
			{
			MapTyp = i;
			}
		}
	var cookietext = cookiename+"="+map.getCenter().lat()+"|"+map.getCenter().lng()+"|"+map.getZoom()+"|"+MapTyp+"|"+ListTyp;
	if (expiredays)
		{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		cookietext += ";expires="+exdate.toGMTString();
		}
	//--- Ecriture des informations dans le cookie
	document.cookie=cookietext;
	//--- Appel de la fonction de déchargement
	GUnload();
	}

//-----------------------------------------------
//--- Fonction de chargement du fond de carte personnalisé
function CustomGetTileUrl(a,b)
	{
	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
		var imgNum = ((a.y-45)*2) + (a.x-63);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_"+ imgNum +".jpg";
		}
	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
		var imgNum = ((a.y-90)*3) + (a.x-128);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_"+ imgNum +".jpg";
		}
	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
		var imgNum = ((a.y-180)*5) + (a.x-257);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_"+ imgNum +".jpg";
		}
	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
		var imgNum = ((a.y-360)*10) + (a.x-515);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_"+ imgNum +".jpg";
		}
	else {
		return "../images/google-maps/blank.jpg";
		}
	}

//function CustomGetTileUrlCyclo(a,b)
//	{
//	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
//		var imgNum = ((a.y-45)*2) + (a.x-63);
//		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo_"+ imgNum +".jpg";
//		}
//	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
//		var imgNum = ((a.y-90)*3) + (a.x-128);
//		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo_"+ imgNum +".jpg";
//		}
//	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
//		var imgNum = ((a.y-180)*5) + (a.x-257);
//		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo_"+ imgNum +".jpg";
//		}
//	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
//		var imgNum = ((a.y-360)*10) + (a.x-515);
//		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo_"+ imgNum +".jpg";
//		}
//	else {
//		return "../images/google-maps/blank.jpg";
//		}
//	}

function CustomGetTileUrlCyclo1(a,b)
	{
	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
		var imgNum = ((a.y-45)*2) + (a.x-63);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo1_"+ imgNum +".jpg";
		}
	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
		var imgNum = ((a.y-90)*3) + (a.x-128);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo1_"+ imgNum +".jpg";
		}
	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
		var imgNum = ((a.y-180)*5) + (a.x-257);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo1_"+ imgNum +".jpg";
		}
	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
		var imgNum = ((a.y-360)*10) + (a.x-515);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo1_"+ imgNum +".jpg";
		}
	else {
		return "../images/google-maps/blank.jpg";
		}
	}

function CustomGetTileUrlCyclo2(a,b)
	{
	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
		var imgNum = ((a.y-45)*2) + (a.x-63);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo2_"+ imgNum +".jpg";
		}
	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
		var imgNum = ((a.y-90)*3) + (a.x-128);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo2_"+ imgNum +".jpg";
		}
	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
		var imgNum = ((a.y-180)*5) + (a.x-257);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo2_"+ imgNum +".jpg";
		}
	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
		var imgNum = ((a.y-360)*10) + (a.x-515);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_cyclo2_"+ imgNum +".jpg";
		}
	else {
		return "../images/google-maps/blank.jpg";
		}
	}

function CustomGetTileUrlVtt(a,b)
	{
	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
		var imgNum = ((a.y-45)*2) + (a.x-63);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vtt_"+ imgNum +".jpg";
		}
	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
		var imgNum = ((a.y-90)*3) + (a.x-128);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vtt_"+ imgNum +".jpg";
		}
	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
		var imgNum = ((a.y-180)*5) + (a.x-257);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vtt_"+ imgNum +".jpg";
		}
	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
		var imgNum = ((a.y-360)*10) + (a.x-515);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vtt_"+ imgNum +".jpg";
		}
	else {
		return "../images/google-maps/blank.jpg";
		}
	}
function CustomGetTileUrlVV(a,b)
	{
	if (b==7 && a.x>=64 && a.x<=65 && a.y>=45 && a.y<= 46) {
		var imgNum = ((a.y-45)*2) + (a.x-63);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vv_"+ imgNum +".jpg";
		}
	else if (b==8 && a.x>=129 && a.x<=131 && a.y>=90 && a.y<= 92) {
		var imgNum = ((a.y-90)*3) + (a.x-128);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vv_"+ imgNum +".jpg";
		}
	else if (b==9 && a.x>=258 && a.x<=262 && a.y>=180 && a.y<= 184) {
		var imgNum = ((a.y-180)*5) + (a.x-257);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vv_"+ imgNum +".jpg";
		}
	else if (b==10 && a.x>=516 && a.x<=526 && a.y>=360 && a.y<=371) {
		var imgNum = ((a.y-360)*10) + (a.x-515);
		return "../images/google-maps/tranches/carte_auvergne_"+ b +"_vv_"+ imgNum +".jpg";
		}
	else {
		return "../images/google-maps/blank.jpg";
		}
	}

//-----------------------------------------------
//--- Fonction de chargement de la carte
function loadData()
	{
	if (GBrowserIsCompatible())
		{
		//--- Affichage de la barre de chargement
		document.getElementById("maploading").style.left = LeftPos;
		//--- Fonction de création des marqueurs sur la carte
		function creerMarker(point, icon, html)
			{ 
			var marker = new GMarker(point, icon); 
			GEvent.addListener(marker, "click", function()
				{ 
				ew.openOnMarker(marker,html);
				map.setCenter(point);
				}); 
			return marker; 
			}
		//--- Fonction de création du code HTML contenu dans la EWindow avec un bouton de fermeture
		function EWindowContent(a,b)
			{
			return '<table border="0" width="185" cellpadding="0" cellspacing="0"><tr><td width="100%" class="EWTitle" nowrap>' + a +
			'<a href="javascript:ew.hide()"><img title="Fermer l\'infobulle" src="../images/google-maps/eclose.gif" border=0 style="position:absolute;right:4px;top:4px"></a>' +
			'</td></tr>' +
			'<tr><td nowrap>' + b + '</td></tr></table>';
			}
		//--- Chargement des données XML définissant les marqueurs de la carte
		map.clearOverlays();
		if ((ListTyp != "--") || (MapTyp == 4) || (MapTyp == 5) || (MapTyp == 6) || (MapTyp == 7))
			{
			GDownloadUrl("../_inc/CPN_GoogleMapsXml.php?ListTyp="+ListTyp+"&MapTyp="+ MapTyp, function(data)
				{
				var xml = GXml.parse(data); 
				var markers = xml.documentElement.getElementsByTagName("marker");
				if (markers.length > 0)
					{
					for (var i = 0; i < markers.length; i++)
						{ 
						//--- Création des icones pour les marqueurs sur la carte
						var icon = new GIcon();
						icon.image = markers[i].getAttribute("iconUrl");
						icon.iconSize = new GSize(32, 32);
						icon.iconAnchor = new GPoint(6, 20);
						icon.infoWindowAnchor = new GPoint(15, 5); 
						//--- Création du point sur la carte
						var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")), 8); 
						marker = creerMarker(point, icon, EWindowContent("", markers[i].getAttribute("info")));
						map.addOverlay(marker);
						//--- Création d'une infobulle personnalisée (EWindow)
						ew = new EWindow(map, E_STYLE_7);      
						map.addOverlay(ew);
						//--- Fermer la EWindow en cas de click sur la carte
						GEvent.addListener(map, "click", function(overlay,point) { if (!overlay) { ew.hide(); } });
						if (i == markers.length-1)
							{
							document.getElementById("maploading").style.left = "-999em";							
							}
						}
					}
				else
					{
					alert("Il n'y a pas de données pour cette catégorie.");
					}
				});
			}
		else
			{
			document.getElementById("maploading").style.left = "-999em";
			}
		}
	}