/* Mini Map_function.js
 * 
 *
 * Contient toutes les fonctions utiles pour gérer la map google dans l'affichage texte
 */
 
//déclaration et initialisation des variables globales
var map;
var _mFlags = {}; 
var gdir;
// exécuté au chargement
// recupere la ville en base
// vérifie si il y a plusieurs villes du meme nom
// ou si la ville n'existe pas en base
function load() {
	if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("mini_map"));
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();          
		map.addControl(new GSmallMapControl);
		map.setCenter(new GLatLng(latitude,longitude), 15);
        var point = new GLatLng(latitude,longitude);
   		// on cree l'icone pour ce garage
    	var icon = new GIcon();	
    	icon.image = url;
    	icon.shadow = "/v2/images/shadow_marqueur.png";
    	icon.iconSize = new GSize(36,36);
    	icon.shadowSize = new GSize(55, 36);
    	icon.iconAnchor = new GPoint(18, 36);
    	icon.infoWindowAnchor = new GPoint(16, 1);
    	var marker = new GMarker(point,icon);
		map.addOverlay(marker);
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "error", handleErrors);

		// arrange for our onunload handler to 'listen' for onunload events
		if (window.attachEvent) {
        	window.attachEvent("onunload", function() {
        	        GUnload();      // Internet Explorer
        	});
		} else {
        	window.addEventListener("unload", function() {
                	GUnload(); // Firefox and standard browsers
        	}, false);
		}
      }
}

function setDirections(fromAddress, toAddress) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": "fr" });
}


    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("L'adresse n'a pas été trouvée. Réessayez avec une adresse plus précise.\nError code: " + 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.");
	   
	}

// BEGIN NEW CODE TO AVOID USING BODY TAG FOR LOAD/UNLOAD

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(load);
