var gdir;
var MaCarte;
var geocoder = null;
var Adresse;
var Ville;
var DescriptionCentre;
var marker;
var pointMag;
var Latitude;
var Longitude;
function Gload() {
	
	ChargeInfo();
 	if (GBrowserIsCompatible()) {
		// Initialisation d'un nouveau géocodeur nommé "geocoder"	
 		geocoder = new GClientGeocoder();
		
		pointMag=geocoder.getLatLng(Adresse, function(point) {
			 
			  if (!point) {
				//alert('\r\nl\'adresse :'+Adresse + ' n\'a pas été localisée !\r\nLe centre de :'+ Ville);
						DescriptionCentre+='<br>ATTENTION ADRESSE APPROXIMATIVE LE CENTRE DE :<br>'+Ville+' SERA UTILISE COMME POINT DE LOCALISATION';
						pointMag=geocoder.getLatLng(Ville, function(point) {
					  if (!point) {
						alert('IMPOSSIBLE DE LOCALISER');
					  }else{
						  Adresse=Ville;
						// Création d'une nouvelle carte nommée "MaCarte" s'affichant en lieu et place de la <div> ayant pour identifiant id="EmplacementDeMacarte"	
						MaCarte = new GMap2(document.getElementById('EmplacementDeMacarte'));
						// Affichage des contrôles sur la carte nommée "MaCarte" : Déplacement/Zoom - Type de carte - Mini-carte en bas à droite - Echelle
						MaCarte.addControl(new GLargeMapControl());
						MaCarte.addControl(new GMapTypeControl());
						MaCarte.addControl(new GOverviewMapControl());
						//MaCarte.addControl(new GScaleControl());
						// La carte nommée "MaCarte" est centrée sur le point
						MaCarte.setCenter(point, 12);
							
						
						gdir = new GDirections(MaCarte, document.getElementById("directions"));
						var tinyIcon = new GIcon();
						tinyIcon.image = "http://test.attrait.com/charte/picto_magasin.png";
						tinyIcon.iconSize = new GSize(25, 35);
						tinyIcon.iconAnchor = new GPoint(12, 35);
						tinyIcon.infoWindowAnchor = new GPoint(25, 1);
						
						// Set up our GMarkerOptions object literal
						markerOptions = { icon:tinyIcon };
						marker = new GMarker(point, markerOptions);
						MaCarte.addOverlay(marker);
						marker.openInfoWindowHtml(DescriptionCentre);
						GEvent.addListener(marker,"click", function() {
							MaCarte.openInfoWindowHtml(point, DescriptionCentre);
						});
						gdir = new GDirections(MaCarte, document.getElementById("directions"));
						GEvent.addListener(gdir, "error", handleErrors);
						}
					});
			  }else{
				  	
				// Création d'une nouvelle carte nommée "MaCarte" s'affichant en lieu et place de la <div> ayant pour identifiant id="EmplacementDeMacarte"	
				MaCarte = new GMap2(document.getElementById('EmplacementDeMacarte'));
				
				// Affichage des contrôles sur la carte nommée "MaCarte" : Déplacement/Zoom - Type de carte - Mini-carte en bas à droite - Echelle
				MaCarte.addControl(new GLargeMapControl());
				MaCarte.addControl(new GMapTypeControl());
				MaCarte.addControl(new GOverviewMapControl());
				//MaCarte.addControl(new GScaleControl());
				// La carte nommée "MaCarte" est centrée sur le point
				
				MaCarte.setCenter(point, 12);
					
				
				gdir = new GDirections(MaCarte, document.getElementById("directions"));
				var tinyIcon = new GIcon();
				tinyIcon.image = "http://test.attrait.com/charte/picto_magasin.png";
				tinyIcon.iconSize = new GSize(25, 35);
				tinyIcon.iconAnchor = new GPoint(12, 35);
				tinyIcon.infoWindowAnchor = new GPoint(25, 1);
				
				// Set up our GMarkerOptions object literal
				markerOptions = { icon:tinyIcon };
				marker = new GMarker(point, markerOptions);
				MaCarte.addOverlay(marker);
				marker.openInfoWindowHtml(DescriptionCentre);
				GEvent.addListener(marker,"click", function() {
					MaCarte.openInfoWindowHtml(point, DescriptionCentre);
				});
				gdir = new GDirections(MaCarte, document.getElementById("directions"));
				GEvent.addListener(gdir, "error", handleErrors);
				}
			}
		);
 	}else{
 		alert("Désolé, mais votre navigateur n\'est pas compatible avec Google Maps");
 	}
}

 function setDirections(fromAddress) {
	 MaCarte.removeOverlay(marker);
      gdir.load("from: " + fromAddress + " to: "+Adresse,
                { "locale": 'fr' });
    }
	
 function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Votre adresse ne correspond pas à une position géographique. Cela peut être du au fait que l'adresse soit relativement récente ou incorrecte.\nCode Erreur: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

