/*
 * 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é
	 */
	"ajouterPoint"	:	function(latitude, longitude, infobulle){
							
							var marker = new GMarker(new GLatLng(latitude, longitude));
							this.points.push(marker);
							
							if(infobulle != null){
								
								GEvent.addListener(marker, "click", function(){
									
									marker.openInfoWindowHtml(infobulle);
									
								});
								
							}
							
						},
						
	/*
	 * Centrage automatique de la GoogleMap par rapport aux points présents.
	 */
	"centrageAuto"	:	function(){
							
							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);
							/*
							if(kml.loadedCorrectly()){
							*/
								this.overlays.push(kml);
								//kml.show();
							/*
							}else{
								
								alert("Erreur lors du chargement du fichier XML : " + fichier);
								
							}
							*/
						},
						
	/*
	 * 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.");
								
							}
							
						}
						
};

