/*
	Script pour Google Maps API
*/
var GLoad = function(carte,_options){
	if (GBrowserIsCompatible()) {
		// Initialisation de GoogleMaps
		var geocoder = new GClientGeocoder();
		var map = new GMap2(document.getElementById(carte));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var baseIcon = new GIcon();
		baseIcon.image = "/images/markerA.png";
		baseIcon.shadow = "/images/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		var center = false;
		// Fonction pour récupérer le code XML d'un élément
		function getXml(element){
			if (element.xml) return element.xml;
			return (new XMLSerializer()).serializeToString(element);
		}
		// Créer un marqueur avec un texte
		function createMarker(point, texte, zoom, infobulle) {
			// Cas spéciale pour l'annonce principale
			if(!center) {
				map.setCenter(point, zoom);
				center = true;
			}
			// Création du marker avec le point et l'icone
			var marker = new GMarker(point, new GIcon(baseIcon));
			// Fonction de gestion du click sur le point
			if (texte!=""){
				if (infobulle) map.openInfoWindowHtml(point,texte);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(texte);
				});
			}
			return marker;
		}
		// Crée un markeur à l'adresse (via Geocoder) et avec le texte indiqués
		function showAddress(address,latitude,longitude,texte,zoom,infobulle) {
			if (latitude!="" && longitude!=""){
				var point = new GLatLng(latitude,longitude);
				//GLog.write("Adresse \""+address+"\" enregistrée point GPS ("+latitude+","+longitude+")");
				map.addOverlay(createMarker(point, texte, zoom, infobulle));
			}else{
				geocoder.getLatLng(
					address,
					function(point) {
						if (!point) {
							//GLog.write("Adresse \"" + address + "\" non trouvée");
						} else {
							map.addOverlay(createMarker(point, texte, zoom, infobulle));
							//GLog.write("Adresse \"" + address + "\" trouvée au point ("+point.x+","+point.y+")");
						}
					}
				);
			}
		}
		// Chargement de la Google Map avec en entrée l'URL du fichier XML avec les markeurs
		function loadXml(_url) {
			// Parse du fichiers xml pour les coordonnées
			GDownloadUrl(_url, function(data) {
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				// Décode les adresse en points avec geocoder
				for (var i = 0; i < markers.length; i++) {
					showAddress(
						markers[i].getAttribute("address"),
						markers[i].getAttribute("lat"),
						markers[i].getAttribute("lng"),
						getXml(markers[i]),
						i == 0
					);
				}
			});
		}
		// Execute l'affichage d'un point et/ou le chargement d'une URL en fonction des options
		if (_options.url){
			loadXml(_options.url);
		}
		if (_options.points){
			for (var i=0;i<_options.points.length;i++){
				with(_options.points[i]){
					if (typeof(address)=='undefined') var address = "";
					if (typeof(lat)=='undefined') var lat = "";
					if (typeof(lng)=='undefined') var lng = "";
					if (typeof(zoom)=='undefined') var zoom = 7;
					if (typeof(text)=='undefined'){
						var text = "";
						if (typeof(html)!='undefined'){
							text = document.getElementById(html).innerHTML;
						}
					}
					showAddress(address,lat,lng,text,zoom,i == 0);
				}
			}
		}
	}
}
