// JavaScript Document

var map;
var gdir;
var geocoder = null;
var addressMarker;
var markerFormatic; 

//Au chargement de la page on crée la map
function load() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map"), {mapTypes:[G_NORMAL_MAP, G_HYBRID_MAP, G_SATELLITE_MAP]});
		//Ajout de marqueur adresse
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var pointFormatic = new GLatLng(48.1946280, 3.2823920);
		map.setCenter(pointFormatic, 11);
		markerFormatic = createMarker(pointFormatic);
		map.addOverlay(markerFormatic);
		markerFormatic.openInfoWindowHtml("<table><tr valign=\"middle\"><td width=\"55\"><img src=\"./images/logo-formatic-moyen.jpg\" alt=\"\"/></td><td><p>Formatic<br/>Immeuble le Saint Pregts<br/>14, rue Auguste Morel<br/>89100 SENS<br/>Tél : 03 86 83 81 50</p></td></tr></table>");
		//Ajout de la fonctionnalité de routage a la carte	
		gdir = new GDirections(map, document.getElementById("map_directions"));
		GEvent.addListener(gdir, "error", handleErrors);
	}
}

// Crée un marqueur a un point donné avec une bulle qui contient l'adresse de Formatic
function createMarker(point) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<table><tr valign=\"middle\"><td width=\"55\"><img src=\"./images/logo-formatic-moyen.jpg\" alt=\"\"/></td><td><p>Formatic<br/>Immeuble le Saint Pregts<br/>14, rue Auguste Morel<br/>89100 SENS<br/>Tél : 03 86 83 81 50</p></td></tr></table>");
	});
	return marker;
}

// Trace la route depuis la ville passée en parametre
function setDirections(fromAddress) {
	//Suppression du marqueur adresse
	map.removeOverlay(markerFormatic);
	map.closeInfoWindow();
	//Tracer de votre itinéraire
	var toFormaticAddress = "Rue Auguste Morel, Sens, France";
	var locale = "fr";
	gdir.load("from: " + fromAddress + " to: " + toFormaticAddress,{ "locale": locale });
}

// Gestion des erreurs du traceurs de route
function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("Aucun lieu ne correspond à cette adresse. Veuillez saisir une nouvelle adresse de départ.");
   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_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("Aucun lieu ne correspond à cette adresse. Veuillez saisir une nouvelle adresse de départ.");
   // si erreur on reaffiche le point de vegh
   map.addOverlay(markerFormatic);
   markerFormatic.openInfoWindowHtml("<table><tr valign=\"middle\"><td width=\"55\"><img src=\"./images/logo-formatic-moyen.jpg\" alt=\"\"/></td><td><p>Formatic<br/>Immeuble le Saint Pregts<br/>14, rue Auguste Morel<br/>89100 SENS<br/>Tél : 03 86 83 81 50</p></td></tr></table>");
}


