/*
 * Object Javascript visant à simplifier l'utilisation de GoogleMap
 * Une variable ErellisMap est automatiquement créée.
 *
 * Exemple d'utilisation :
 *
 * 	ErellisMap.initialiser(document.getElementById("map"), 49.908842, 4.071722000000001, 13);
 *	ErellisMap.ajouterPoint(49.908842, 4.071722000000001, "<h1>SEML Intégrale</h1>");
 *	ErellisMap.afficher();
 *
 */

var ErellisMap = {
	
	// Attributs
	
	"cible"			:	null,
	"points"		: 	Array,
	"overlays"		:	Array,
	"latitude"		:	0,
	"longitude"		:	0,
	"zoom"			:	13,
	
	// Méthodes
	
	/*
	 * Initialise les valeurs pour la googlemap
	 *
	 * @param cible 
	 * 		La cible pour la google map (par ex. : document.getElementById("map") )
	 * @param latitude 
	 * 		La latitude de centrage de la googlemap
	 * @param longitude
	 *		La longitude de centrage de la googlemap
	 * @param zoom
	 *		Le facteur de zoom de base de la googlemap (13 est une bonne valeur)
	 */
	"initialiser"	:	function(cible, latitude, longitude, zoom){
							
							this.cible = cible;
							
							if(latitude != null)
								this.latitude = latitude;
							
							if(longitude != null)
								this.longitude = longitude;
							
							if(zoom != null)
								this.zoom = zoom;
							
							this.points = new Array();
							
							this.overlays = new Array();
							
						},
						
	/*
	 * Ajoute un point sur la carte
	 *
	 * @param latitude 
	 * 		La latitude du nouveau point
	 * @param longitude
	 *		La longitude du nouveau point
	 * @param infobulle
	 *		Le texte a mettre en infobulle (quand on clique sur le marqueur); Mettre à null si inutilisé
	 * @param icone
	 *		Icone customisée a mettre sur la carte (peut ne pas être renseigné)
	 * @param hauteur
	 *		Hauteur de l'icone
	 * @param largeur
	 *		Largeur de l'icone
	 */
	"ajouterPoint"	:	function(latitude, longitude, infobulle, icone, hauteur, largeur, ancre_x, ancre_y){
							
							//alert("Ajout du point [" + latitude + ":" + longitude + "]");
							
							/*if(icone != null){
								
								var icone_custom = new GIcon(G_DEFAULT_ICON);
								icone_custom.image = icone;
								icone_custom.iconSize = new GSize(hauteur, largeur);
								icone_custom.shadowSize = new GSize(hauteur, largeur);
								icone_custom.iconAnchor = new GPoint(hauteur, largeur);
								icone_custom.infoWindowAnchor = new GPoint(ancre_x, ancre_y);
								
								markerOptions = { icon:icone_custom };
								
							}else{
							*/	
								markerOptions = {};
								
							//}
							
							var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
							this.points.push(marker);
							
							if(infobulle != null){
								
								GEvent.addListener(marker, "click", function(){
									
									marker.openInfoWindowHtml(infobulle);
									
								});
								
							}
							
						},
						
	/*
	 * Ajoute un point sur la carte en utilisant une adresse postale.
	 *
	 * @param adresse
	 *		L'adresse postale
	 * @param infobulle
	 *		Le texte a mettre en infobulle (quand on clique sur le marqueur); Mettre à null si inutilisé
	 * @todo
	 *		Il faut trouver un moyen pour que l'affichage attende que les points soient chargés depuis le serveur google
	 *		(parceque on passe en asynchrone en faisant la requête)
	 */
	"ajouterAdresse" :	function(adresse, infobulle){
							
							geo = new GClientGeocoder();
							
							geo.setBaseCountryCode('fr');
							
							geo.getLatLng(adresse, function(point){								
								
								if(point != null)
									ErellisMap.ajouterPoint(point.lat(), point.lng(), infobulle);
								
								ErellisMap.afficher(); // Obligtoire, sinon on passe dedans sans que les points aient eu le temps d'être chargés
								
							});
							
						},
						
	/*
	 * Centrage automatique de la GoogleMap par rapport aux points présents.
	 */
	"centrageAuto"	:	function(){
							
							if(this.points.length > 0){
							
								var lat_lng_ini = this.points[0].getLatLng();
								
								var max_lat = lat_lng_ini.lat();
								var max_lng = lat_lng_ini.lng();
								var min_lat = lat_lng_ini.lat();
								var min_lng = lat_lng_ini.lng();
								
								for(var bcl = 0 ; bcl < this.points.length ; bcl++){
									
									var lat_lng = this.points[bcl].getLatLng();
									
									if(lat_lng.lat() < min_lat)
										min_lat = lat_lng.lat();
									
									if(lat_lng.lat() > max_lat)
										max_lat = lat_lng.lat();
									
									if(lat_lng.lng() < min_lng)
										min_lng = lat_lng.lng();
									
									if(lat_lng.lng() > max_lng)
										max_lng = lat_lng.lng();
									
								}
								
								this.latitude = min_lat + ( (max_lat - min_lat) / 2 );
								this.longitude = min_lng + ( (max_lng - min_lng) / 2 );
							
							}
							
						},
						
	/*
	 * Charge un fichier XML/KML et le prépare pour l'affichage sur la GoogleMap.
	 *
	 * @param fichier
	 *		Le fichier XLM à charger.
	 */
	"chargerXML"	:	function(fichier){
							
							var kml = new GGeoXml(fichier);
							this.overlays.push(kml);
							
						},
						
	/*
	 * Finalise l'affichage de la googlemap
	 */
	"afficher"		:	function(){
							
							if (GBrowserIsCompatible()) {
								
								var map = new GMap2(this.cible);
								map.setCenter(new GLatLng(this.latitude, this.longitude), this.zoom);
								map.addControl(new GSmallMapControl());
								map.addControl(new GMapTypeControl());
								
								// Ajout des points
								for(var bcl = 0 ; bcl < this.points.length ; bcl++)
									map.addOverlay(this.points[bcl]);
								
								// Ajout des calques (XML, ...)
								for(var bcl = 0 ; bcl < this.overlays.length ; bcl++)
									map.addOverlay(this.overlays[bcl]);
								
							}else{
								
								alert("Votre navigateur n'est pas compatible avec GoogleMap.");
								
							}
							
						}
						
};
