var map = GMap2;
var BoolCacherSite = true;
var vIDSite = "";
var vProjet = "";
var vDepartement = "";
var vPoint = GLatLng;
var URLBase = "";
var btCacherSite = null;

function ImageIcon(Icone) {
	var Icn = "";
	if (Icone.length>6) {
		if (Icone.substring(6,8)=="AC") {
			Icn = "AC";
		} else {
			Icn = Icone.substring(6, Icone.length);
		}
		return URLBase + "Ressources_WEB/icon24_" + Icn + ".gif";
	} else {
		return "";
	}
}

function CreateIcon(Icone) {
	var vIcone = ImageIcon(Icone);
	if (vIcone=="") {
		return;
	} else {
		var icon = new GIcon();
		icon.image = vIcone;
		icon.iconSize = new GSize(24, 24);
		icon.iconAnchor = new GPoint(8, 24);
		icon.infoWindowAnchor = new GPoint(7, 1);
		return icon;
	}
}

function LectureGPXProche() {
	GDownloadUrl(URLBase + "GenereGPX.aspx?Projet=" + vProjet + "&IDSite=" + vIDSite + "&GPS=GPX&Proche=Oui&Flux=1", function(data, responseCode) {
		LectureGPX(data,16,"Oui");
   	});
}

function LectureGPXIDSite() {
	GDownloadUrl(URLBase + "GenereGPX.aspx?Projet=" + vProjet + "&IDSite=" + vIDSite + "&GPS=GPX&Flux=1", function(data, responseCode) {
		LectureGPX(data,7,"Oui");
  	});
}

function LectureGPX(data,Zoom,PointAcces) {
	map.clearOverlays();
	var Zom = Zoom;
	var xml = GXml.parse(data);
	var markers = xml.documentElement.getElementsByTagName("wpt");
	var nom = xml.documentElement.getElementsByTagName("name");
	var cmt = xml.documentElement.getElementsByTagName("cmt");
	var sym = xml.documentElement.getElementsByTagName("sym");
	var src = xml.documentElement.getElementsByTagName("src");
	var bounds = new GLatLngBounds();
	for (var i = 0; i < markers.length; i++) {
		if (sym[i].childNodes[0]==undefined) {
			sym[i].appendChild(document.createTextNode(""));
		}
		if ((sym[i].childNodes[0].nodeValue!="Trail Head") || (PointAcces=="Oui")) { 
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lon")));
			bounds.extend(point);
			map.addOverlay(CreateMarker(point, sym[i].childNodes[0].nodeValue, nom[i].childNodes[0].nodeValue, cmt[i].childNodes[0].nodeValue, src[i].childNodes[0].nodeValue));
		}
	}
	if (Zom!=0) {
	    if (map.getBoundsZoomLevel(bounds)<Zom) {
		    Zom = map.getBoundsZoomLevel(bounds);
	    }
	    map.setCenter(bounds.getCenter(), Zom);
    } else {
        map.setCenter(vPoint, map.getBoundsZoomLevel(bounds));
    }
}

function CreateMarker(Coordonnees, Symbole, IDNom, Description, Icone) {
	var icon = GIcon;
	icon = CreateIcon(Icone);
	var marker = new GMarker(Coordonnees, icon);
	GEvent.addListener(marker, "click", function() {
	    var text = ImageIcon(Icone);
	    if (text!="") {
			text = "<IMG SRC=\"" + text + "\">";
		}
		marker.openInfoWindowHtml("<TABLE BGCOLOR=\"BLACK\" BORDER=\"0\"><TR><TD COLSPAN=\"2\"><H2>" + text + "IDSite :" + IDNom + "</H2><HR></TD></TR><TR><TD>" + Description + "<BR><A HREF=\"AfficheSite.aspx?Projet=" + vProjet + "&IDSite=" + IDNom + "\">Fiche</A></TD><TD><IMG SRC=\"AffichePreview.aspx?Projet=" + vProjet + "&IDSite=" + IDNom + "\" WIDTH=\"161\" HEIGHT=\"161\"></TD></TR></TABLE>");
	});
	return marker;
}

function WaitMessage() {
}

WaitMessage.prototype = new GControl();

WaitMessage.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var btWaitMessage = document.createElement("btWaitMessage");
	this.setButtonStyle_(btWaitMessage);
	container.appendChild(btWaitMessage);
	btWaitMessage.appendChild(document.createTextNode("Veuillez patienter, chargement en cours..."));
	GEvent.addDomListener(btWaitMessage, "click", function() {
	});
	map.getContainer().appendChild(container);
	return container;
}

WaitMessage.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(140, 14));
}

WaitMessage.prototype.setButtonStyle_ = function(button) {
	button.style.color = "#0000cc";
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.border = "1px solid black";
	button.style.padding = "2px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
}

function SiteControle() {
}

SiteControle.prototype = new GControl();

SiteControle.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var btSiteProche = document.createElement("btSiteProche");
	this.setButtonStyle_(btSiteProche);
	container.appendChild(btSiteProche);
	btSiteProche.appendChild(document.createTextNode("Site Proche"));
	GEvent.addDomListener(btSiteProche, "click", function() {
		LectureGPXProche();
	});
	btCacherSite = document.createElement("btCacherSite");
	this.setButtonStyle_(btCacherSite);
	container.appendChild(btCacherSite);
	btCacherSite.appendChild(document.createTextNode("Cacher Site"));
	GEvent.addDomListener(btCacherSite, "click", function() {
		var NewTexte = null;
		if (BoolCacherSite) {
			BoolCacherSite = false;
			map.clearOverlays();
			NewTexte = document.createTextNode("Montrer Site");
		} else {
			BoolCacherSite = true;
			LectureGPXIDSite();
			NewTexte = document.createTextNode("Cacher Site");
		}
		btCacherSite.replaceChild(NewTexte, btCacherSite.lastChild);
	});
	map.getContainer().appendChild(container);
	return container;
}

SiteControle.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(70, 7));
}

SiteControle.prototype.setButtonStyle_ = function(button) {
	button.style.color = "#0000cc";
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.border = "1px solid black";
	button.style.padding = "2px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
}

function Coordonnees(pProjet,pPoint,pNombre) {
    vProjet = pProjet;
	vPoint = pPoint;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		var Wmg = new WaitMessage()
		map.addControl(Wmg);
		map.setCenter(vPoint, 7);
		map.setMapType(G_HYBRID_MAP);
		var Chn = "GenereGPX.aspx?Projet=" + vProjet + "&Grille=LAT+LON+DEG&Coordonnees="
		if (vPoint.lat()>0) {
		    Chn = Chn + "N";
		} else {
		    Chn = Chn + "S";
		}
		Chn = Chn + Math.abs(vPoint.lat()) + "+";
		if (vPoint.lng()>0) {
		    Chn = Chn + "E";
		} else {
		    Chn = Chn + "W";
		}
		Chn = Chn + Math.abs(vPoint.lng()) + "&Nombre=" + pNombre + "&GPS=GPX&Flux=1";
		GDownloadUrl(URLBase + Chn, function(data, responseCode) {
			LectureGPX(data,0,"Oui");
			map.removeControl(Wmg)
		});
    } else {
		ErreurNavigateur();
	}
}

function IDSite(pProjet, pPoint, pIDSite) {
	vProjet = pProjet;
	vPoint = pPoint;
	vIDSite = pIDSite;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new SiteControle());
		map.setCenter(vPoint, 7);
		map.setMapType(G_HYBRID_MAP);
		LectureGPXIDSite();
	} else {
		ErreurNavigateur();
	}
}

function ErreurNavigateur() {
	document.getElementById("map").innerHTML = "<H1>Navigateur non compatible avec Google Maps. Désolé...</H1>";
}

function Parametre(pProjet, pParam) {
	vProjet = pProjet;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		var Wmg = new WaitMessage()
		map.addControl(Wmg);
		map.setCenter(new GLatLng(48.86772, 2.38617), 7);
		map.setMapType(G_HYBRID_MAP);
		GDownloadUrl(URLBase + "GenereGPX.aspx?Projet=" + vProjet + pParam + "&GPS=GPX&Flux=1", function(data, responseCode) {
			LectureGPX(data,14,"Non");
			map.removeControl(Wmg)
		});
	} else {
		ErreurNavigateur();
	}
}

