/**
	SUPPRIME TOUS LES NOEUDS FILS...
*/
function deleteChildNodes(noeud) { while (noeud.childNodes.length>0) noeud.removeChild(noeud.firstChild); }

/**
	UTILISE : 
		- http://maps.google.com/maps?...
		- MyHttpRequest
*/

function GoogleMap(idGoogleDev,varname)
{
	this.googleMapOffers = new Array();			// Tableau de GoogleMapOffer
	this.idDev = idGoogleDev;
	this.idHtmlEtapes = "";
	this.idHtmlErreurs = "";
	this.idHtmlMap = "";					// Id de la div contenant GoogleMap.
	this.zoom = 10;
	this.zoomActivated = false;
	this.myHttp = null;
	this.langue = "fr";					// Utilise pour la lengue des menus de Google Map.
	this.itineraire = null;					// Utilise pour les itineraires. (GDirections)
	this.map = null;					// GMap2
	this.varName = varname;					// Nom de la variable GooleMap (variable globale). (utilise en cas de perte du contexte).
	
	this.flagErrors = false;
	this.withPhotos = true;					// Genere un google map avec les photos.
	
	/**
		Apres l'initialisation (si necessaire), le chargement de la carte...
	*/
	this.load = function(mapId,idEtapes,idErrorOutput) {
		 
		 this.idHtmlEtapes = idEtapes;
		 this.idHtmlErreurs = idErrorOutput;
		 this.idHtmlMap = mapId;
		 
	     if(GBrowserIsCompatible())
	     {
			this.map = new GMap2(document.getElementById(mapId));
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GScaleControl());
			this.map.addControl(new GMapTypeControl());
			
			var tmpOffer = null;
			var tmpMarker = null;
			
			this.map.setCenter(new GLatLng(this.googleMapOffers[0].latitude,this.googleMapOffers[0].longitude), this.zoom);
			
			for(var i=0;i<this.googleMapOffers.length;i++)
			{	
				
				tmpOffer = this.googleMapOffers[i];
				
				marker = this.createMarker(tmpOffer,tmpOffer.index);
				this.map.addOverlay(marker);
				if(i==0) this.map.openInfoWindow(marker.getPoint(),tmpOffer.toHtml(this.varName,this.withPhotos,i));
			}
	     }
	}
	
	/**
		Ajoute une offre au tableau.
	*/
	this.addOffer = function(offer) { this.googleMapOffers.push(offer); }
	
	/**
		Creer un "GMarker" a partir d'une offre.
	*/
	this.createMarker = function(offer,index) {
		
		
		if(index=="")
		{
			var marker = new GMarker(new GLatLng(offer.latitude,offer.longitude), offer.gIcon);
			
			
		}
		else
		{
			// Create a base icon for all of our markers that specifies the
			// shadow, icon dimensions, etc.
			var baseIcon = new GIcon(G_DEFAULT_ICON);
			baseIcon.shadow = "http://www.google.com/mapfiles/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);

			// Creates a marker whose info window displays the letter corresponding
			// to the given index.
			// Create a lettered icon for this point using our icon class
			 
			  var letteredIcon = new GIcon(baseIcon);
			  letteredIcon.image = "http://www.google.com/mapfiles/marker" + index + ".png";

			  // Set up our GMarkerOptions object
			  markerOptions = { icon:letteredIcon };
			var marker = new GMarker(new GLatLng(offer.latitude,offer.longitude),markerOptions);
			
		}
			
		GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(offer.toHtml('googleMap',index)); } );
				
		/*if(document.all) // For IE Only
			GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(offer.toHtml('googleMap',index)); } );
		else
			GEvent.addListener(marker, 'click', eval("function(){ marker.openInfoWindowHtml(offer.toHtml('"+this.varName+"',"+index+"))}"));
		*/
			
		return marker;
	}
	
	/**
		Gestion des erreurs sur l'objet "GDirections".
		Ne semble pas fonctionner pour le moment...retour systematique de l'erreur 500 : G_GEO_SERVER_ERROR.
	*/
	this.erreursItineraire = function ()
	{
		if(this.itineraire.getStatus().code == 200)
		{
			outputErreur.style.display = "none";
			return;
		}
		
		var outputErreur = document.getElementById(this.idHtmlErreurs);
		outputErreur.style.display = "block";
		
		if (this.itineraire.getStatus().code == G_GEO_UNAVAILABLE_ADDRESS)
		{	if(this.langue == "fr") outputErreur.firstChild.nodeValue = "La géolocalisation des points de l'itinéraire a echouée.";
			else if(this.langue == "de") outputErreur.firstChild.nodeValue = "A geocoding or directions request could not be successfully processed.";
			else if(this.langue == "en") outputErreur.firstChild.nodeValue = "A geocoding or directions request could not be successfully processed.";
			this.flagErrors = true;
		}
		else if (this.itineraire.getStatus().code == G_GEO_BAD_REQUEST || this.itineraire.getStatus().code == G_GEO_MISSING_QUERY || this.itineraire.getStatus().code == G_GEO_BAD_KEY)
		{	if(this.langue == "fr") outputErreur.firstChild.nodeValue = "The request failure.";
			else if(this.langue == "de") outputErreur.firstChild.nodeValue = "The request failure.";
			else if(this.langue == "en") outputErreur.firstChild.nodeValue = "La requête a échouée.";
			this.flagErrors = true;
		}
		else if (this.itineraire.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		{	if(this.langue == "fr") outputErreur.firstChild.nodeValue = "Adresse spécifiée inconnue.";
			else if(this.langue == "de") outputErreur.firstChild.nodeValue = "No location could be found for specified addresse.";
			else if(this.langue == "en") outputErreur.firstChild.nodeValue = "No location could be found for specified addresse.";
			this.flagErrors = true;
		}
		else
		{	if(this.langue == "fr") outputErreur.firstChild.nodeValue = "Une erreur inconnues est survenue.";
			else if(this.langue == "de") outputErreur.firstChild.nodeValue = "An unknown error occurred.";
			else if(this.langue == "en") outputErreur.firstChild.nodeValue = "An unknown error occurred.";
			this.flagErrors = true;
		}
	}
	
	/**
		Itineraires : De fromAdress Vers toAdress.
	*/
	this.setItineraire = function(fromAddress, toAddress) 
	{	var outputErreur = document.getElementById(this.idHtmlErreurs);
			outputErreur.style.display = "none";
		
		this.flagErrors = false;
		GEvent.addListener(this.itineraire,"error",setTimeout(this.varName+".erreursItineraire()",2000));
		this.itineraire.load("from: " + fromAddress + " to: " + toAddress);
	}
	
	/**
		Itineraire : De fromAdress Vers index de l'offre.
	*/
	this.setItineraireOffre = function(fromAddress, indexOffre) { this.setItineraire(fromAddress,this.googleMapOffers[indexOffre].adresse); }
	
	/**
		Lien entre la map et la liste des etapes.
	*/
	this.initItineraire = function() { if(this.itineraire) this.itineraire.clear(); this.itineraire = new GDirections(this.map, document.getElementById(this.idHtmlEtapes)); }
	
	
	this.setZoom = function(param) {
		if(param == false)  this.zoomActivated = false;
		else this.zoomActivated = true;
		
		if(this.zoomActivated) this.map.enableScrollWheelZoom();
		else this.map.disableScrollWheelZoom();
	}
	
	/**
		Ajoute l'evenement au bloc concerne et retourne l'id du bloc oÃƒÂ¹ seront ÃƒÂ©crites les infos concernant cet evenement.
		L'id de l'element est impose !!!.
	*/
	this.event_addZoomClick = function(idInfoSouris,pathImgOn,pathImgOff) {
		var elem = document.getElementById(this.idHtmlMap);
		var onclickFctStr = "function(){ if(!"+this.varName+".zoomActivated) "+this.varName+".event_exeZoomClick('"+idInfoSouris+"','"+pathImgOn+"','"+pathImgOff+"'); }";
		elem.onclick = eval(onclickFctStr);
	}

	/**
		Ajoute l'evenement au bloc concernÃƒÂ© et retourne l'id du bloc oÃƒÂ¹ seront ÃƒÂ©crites les infos concernant cet evenement.
		L'id de l'element est imposÃ© !!!.
	*/
	this.event_addZoomMouseout = function(idInfoSouris,pathImgOn,pathImgOff) {
		var elem = document.getElementById(this.idHtmlMap);
		var onMouseOutFctStr = "function(){ if("+this.varName+".zoomActivated) "+this.varName+".event_exeZoomClick('"+idInfoSouris+"','"+pathImgOn+"','"+pathImgOff+"'); }";
		elem.onmouseout = eval(onMouseOutFctStr);
	}
	
	this.event_exeZoomClick = function(infoSourisId,imgOn,imgOff) {
		this.setZoom(!this.zoomActivated);
		this.event_exeZoomDisplay(this.zoomActivated,infoSourisId,imgOn,imgOff);
	}
	
	this.event_exeZoomDisplay = function(etatAttendu,infoSourisId,imgOn,imgOff) {
		if(this.map.scrollWheelZoomEnabled() == etatAttendu)
		{	var infoSouris = document.getElementById(infoSourisId);
			if(!infoSouris) return false;
			else deleteChildNodes(infoSouris);
			
			if(!this.zoomActivated) {
				var img = document.createElement('img');
				    img.setAttribute('src',imgOff);
				    img.setAttribute('alt','Etat : Zoom off.');
				infoSouris.appendChild(img);
			}
			else { var img = document.createElement('img');
				   img.setAttribute('src',imgOn);
				   img.setAttribute('alt','Etat : Zoom on.');
				infoSouris.appendChild(img);
			}
		}
		else setTimeout(""+this.varName+".event_exeZoomDisplay('"+etatAttendu+"','"+infoSourisId+"','"+imgOn+"','"+imgOff+"');",3000);
	}
}






