	var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		
		//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
		
		if(defaultCP != 0 && defaultFrom != 0){
			setDirections(defaultFrom, defaultCP, "fr_CA");
		}else{
			if(defaultLat && defaultLng){
				map.setCenter(new GLatLng(defaultLat, defaultLng), 13);
				// ajout marker
				var marker = new GMarker(new GLatLng(defaultLat, defaultLng));
				marker.bindInfoWindow("<b>Appartement Hotel</b><br />455, West Sherbrooke Street, Montreal (Quebec)<br />H3A 1B7");
				map.addOverlay(marker);
			}else{
				map.setCenter(new GLatLng(45.506850, -73.572332), 7);
			}
		}
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      //$("#directions").slideUp("slow");
	  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	 //gdir.loadFromWaypoints("from: " + fromAddress + " to: " + toAddress, { "locale": locale })
    }

    function handleErrors(){
		if(defaultLat && defaultLng){
			map.setCenter(new GLatLng(defaultLat, defaultLng), 13);
			// ajout marker
			var marker = new GMarker(new GLatLng(defaultLat, defaultLng));
			marker.bindInfoWindow("<b><b>Appartement Hotel</b><br />455, West Sherbrooke Street, Montreal (Quebec)<br />H3A 1B7");
			map.addOverlay(marker);
		}else{
			map.setCenter(new GLatLng(45.506850, -73.572332), 7);
		}
		
		marker.bindInfoWindow(defaultCP);
		map.addOverlay(marker);
	  
	 
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\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.");
	   else alert("Please specify a valid address.");
	    /**/
	}

	function onGDirectionsLoad(){
      // Use this function to access information about the latest load()
      // results.

	  	var hauteurDirection = $("#directions").height();
		if( hauteurDirection < 15 ){
			//alert("ouvre les directions");
			$("#directions").slideDown("slow");
			$("#toggleDirec").html("Hide directions");
		}else{
			$("#toggleDirec").html("Show directions");
		}
		// e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
	
	
	
	
  
	function getDistance(monDiv, start, end){
		var Gdist = new GDirections();
		
		GEvent.addListener(Gdist,"error", function() {
			//GLog.write("Failed: "+Gdist.getStatus().code);
			//alert("Failed: "+Gdist.getStatus().code);
			$("#"+monDiv).html("Distance not found");
		});
		
		GEvent.addListener(Gdist,"load", function() {
			//GLog.write("The distance is "+Gdist.getDistance().meters+" metres");
			//GLog.writeHtml("The distance is "+Gdist.getDistance().html);
			//alert("The distance is "+Gdist.getDistance().html);
			$("#"+monDiv).html(Gdist.getDistance().html);
		});
		
		//var start = "H1W 3N5";
		//var end = "H2T 1S9";
		
		Gdist.loadFromWaypoints([start,end],{getSteps:true});
	}
	
	
	
	
	function toggleDirection(){
		$("#directions").slideToggle("slow");
		var hauteurDirection = $("#directions").height();
		if( hauteurDirection > 5 ){
			$("#toggleDirec").html("Show directions");
		}else{
			$("#toggleDirec").html("Hide directions");
		}
		
		/*
		si on veut utiliser une fonction lorsque fini faire comme le suivant
		$("p").slideUp("fast",function(){
		   alert("Animation Done.");
		 });
		*/
	}