/*
<a class="GMarker" rel="45,0.05,coucou">Cliquer pour geolocaliser</a>
*/
/*
Classe GoogleMaps :
- constructeur : 2 arguments (element et options)
	- initialiser la carte googlemaps avec les contrôles
- options :
	- markers : tableau de {lat,lng,address,text,events} si renseigné, alors initialise la carte avec ce marker
	- url : url d'un fichier XML, si renseigné, alors charge les markers et les affiche
	- options carte (vue satellite, vue plan, ...)
- fonction createMarker : créer un marker à partir de coordonnées (texte, lat, lng, adresse)
- fonction addMarker : ajouter un marker à la carte
- fonction addMarkers : idem pour un tableau de markers
- fonction geoencode : renvoi le marker avec les attribut lat et lng mis à jour par rapport à l'attribut address à partir d'une adresse
- fonction loadMarkers : charger un tableau de markers à partir d'une URL (fichier XML)
*/
var GoogleMaps = new Class({
	implements : [Options],
	element : null,
	options : {
		markers : null,
		url : null,
		controls :{
			smallMap : true,
			type : true
		},
		icon : {
			image : "/images/markerA.png",
			shadow : "/images/shadow50.png",
			iconSize : new GSize(20, 34),
			shadowSize : new GSize(37, 34),
			iconAnchor : new GPoint(9, 34),
			infoWindowAnchor : new GPoint(9, 2),
			infoShadowAnchor : new GPoint(18, 25)
		},
		center : null,
		zoom : 7,
		markerDefaultEvents : null
	},
	map : null,
	geocoder : null,
	icon : null,
	markers : [],
	initialize : function(element,options){
		this.element = element;
		this.setOptions(options);
		// Initialisation de l'API Google Maps
		this.geocoder = new GClientGeocoder();
		this.map = new GMap2(this.element);
		if (this.options.controls.smallMap) this.map.addControl(new GSmallMapControl());
		if (this.options.controls.type)  this.map.addControl(new GMapTypeControl());
		if (this.options.icon){
			this.icon = new GIcon();
			$extend(this.icon,this.options.icon);
		}
		if (this.options.markers) this.addMarkers(this.options.markers);
		if (this.options.url) this.loadMarkers(this.options.url);
		if (this.options.center) this.setCenter(this.options.center);
	},
	addMarkers : function(markers){
		markers.each(function (marker){
			//alert('addMarkers : address='+marker.address+' lat='+marker.lat+' lng='+marker.lng);
			if (!marker.lat || !marker.lng){
				this.geoencode(marker);
			}else{
				this.addMarker(marker);
			}
		}.bind(this));
	},
	addMarker : function(marker){
		//alert('addMarker : address='+marker.address+' lat='+marker.lat+' lng='+marker.lng);
		var gPoint = new GLatLng(marker.lat,marker.lng);
		var gMarker = new GMarker(gPoint, new GIcon(this.icon));
		if (marker.text) gMarker.openInfoWindowHtml(marker.text);
		gMarker.marker = marker;
		if (!marker.events){
			marker.events = this.options.markerDefaultEvents;
			var eventsHash = new Hash(marker.events);
			eventsHash.each(function(eventFunction,eventName){
				GEvent.addListener(gMarker,eventName,eventFunction);
			});
		}else{
			var events = JSON.decode(marker.events);
			var eventsHash = new Hash(events);
			eventsHash.each(function(eventFunction,eventName){
				GEvent.addListener(gMarker,eventName,eventFunction);
			});
		}
		this.markers.push(gMarker);
		this.map.addOverlay(gMarker);
		if (!this.options.center) this.setCenter(gPoint);
	},
	geoencode : function(marker){
		this.geocoder.getLatLng(
			marker.address,
			function(point) {
				if (!point) {
					//GLog.write("Adresse \"" + marker.address + "\" non trouvée");
				} else {
					marker.lat=point.y;
					marker.lng=point.x;
				}
				this.addMarker(marker);
			}.bind(this)
		);
	},
	loadMarkers : function(url){
		//alert(url);
		var result = [];
		GDownloadUrl(url, function(data) {
	        var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			//alert (markers.length);
			for (var i = 0; i < markers.length; i++) {
				result.push({
					lat : markers[i].getAttribute("latitude"),
					lng : markers[i].getAttribute("longitude"),
					address : markers[i].getAttribute("address"),
					texte : markers[i].getAttribute("texte"),
					img : markers[i].getAttribute("img"),
					link : markers[i].getAttribute("link"),
					tel : markers[i].getAttribute("tel"),
					fax : markers[i].getAttribute("fax"),
					email : markers[i].getAttribute("email"),
					events: markers[i].getAttribute("events")
				});
			}
			this.addMarkers(result);
		}.bind(this));
	},
	setCenter : function(center){
		this.options.center = center;
		this.map.setCenter(center, this.options.zoom);
	}
});
GoogleMaps.implement(new Options);
