     // Display the map, with some controls and set the initial location 
     
      var pJourdets = new GLatLng(43.011025055863975,2.4840903282165527);
      var map = new GMap2(document.getElementById("map") );
      map.setCenter(pJourdets, 10);
     
	  map.setUIToDefault();

	  var bounds = new GLatLngBounds();
	  
	  var minMapScale = 9; 
      var maxMapScale = 15; 
      // get array of map types 
      var mapTypes = map.getMapTypes(); 
      // overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type 
      for (var i=0; i<mapTypes.length; i++) { 
      mapTypes[i].getMinimumResolution = function() {return minMapScale;} ;
      mapTypes[i].getMaximumResolution = function() {return maxMapScale;} ;
      }

 
	  var poly;
	  var itiOn = false;

      // ====== Create a Client Geocoder ======
      var geo = new GClientGeocoder(new GGeocodeCache()); 

      // ====== Array for decoding the failure codes ======
      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

      var ip;
      function createMarker(point,i,icon) {
    	  	if (active[i])
    		  map.removeOverlay(gmarkers[i]);
  		  	/* if ( poly )
  		  		{
  		  		map.removeOverlay(poly);
  		  		} */ gdir.clear();

    	  	ip = i;
      	  	active[i] = true;
      	  	if ( i == 0 )
      	  		gmarkers[i] = new GMarker(point, {draggable:true,icon:icon});
      	  	else
      	  		gmarkers[i] = new GMarker(point, {icon:icon});

          	GEvent.addListener(gmarkers[i], "dragend", function() 
                  	{
            		path[i] = gmarkers[i].getPoint();
            		if ( !addresses[i] ) geo.getLocations(path[i],findAddress);
          			});
          	map.addOverlay(gmarkers[i]);

        }

      
      function createInterMarker(point) 
      		{
      	    var marker = new GMarker(point, {icon:icon2});
        	tMarks[ntMark++] = marker;
        	map.addOverlay(marker);
      		}

      function removeMarks()
    	{
  		for ( var i = 0 ; i < 2 ; i++ )
  	  		
	  		map.removeOverlay(gmarkers[i]);
    	}
  	
      function removetMarks()
      	{
    	for ( var i = 0 ; i < ntMark ; i++ )
	  		map.removeOverlay(tMarks[i]);
  		ntMark = 0;
      	}
    	
      function findAddress(response) {
          if (!response || response.Status.code != 200) {
            alert("Status Code:" + response.Status.code);
          	} 
    	  else {
            place = response.Placemark[0];
    		addresses[ip] = place.address;
    		}
    	}
  	
      var ntMark = 0;
      var tMarks = [];
      var path = [];
      var active = [false,false];
      var gmarkers = [];
      var addresses = [];

      var baseIcon = new GIcon(G_DEFAULT_ICON);
      baseIcon.iconSize = new GSize(24,38);
      
      
      var icon1 = G_START_ICON;
      var icon2 = G_PAUSE_ICON;
      var icon3 = G_END_ICON;
      var icon4 = new GIcon(baseIcon,"http://labs.google.com/ridefinder/images/mm_20_white.png");
          icon4.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
          icon4.iconSize = new GSize(12, 20);
          icon4.shadowSize = new GSize(22, 20);
          icon4.iconAnchor = new GPoint(6, 20);
          icon4.infoWindowAnchor = new GPoint(5, 1);
      
      GEvent.addListener(map, "click", function(overlay,point) 
    	      	{
          		if (point) 
            		doStart(point) ;
            	
        		});
        	

      function doStart(point) {
          createMarker(point,0,icon1);
          path[0] = point;
          bounds.extend(path[0]);
          bounds.extend(path[1]);
          removetMarks();
          // map.setZoom(map.getBoundsZoomLevel(bounds));
          map.setCenter(bounds.getCenter(),map.setZoom(map.getBoundsZoomLevel(bounds)));
          
          doEnd(pJourdets); // inchangeable
          if ( itiOn == true )
          	{
        	// document.getElementById("path").className = "off";
        	// document.getElementById("ginfos").className = "on";
        	  $('#path').hide();
        	  $('#ginfos').show();
          	}
        }

      function doEnd(point) {
          createMarker(point,1,icon3);
          path[1] = point;  

        }
        
        
      // ====== Geocoding ======
      
      function showAddress() {
       
        var search = document.getElementById("search").value;
        addresses[0] = search;
     
        geo.getLatLng(search, function (point)
          { 
            if (point) {
              doStart(point);
            }
            else {
              var result=geo.getCache().get(search);
              if (result) {
                var reason="Code "+result.Status.code;
                if (reasons[result.Status.code]) {
                  reason = reasons[result.Status.code];
                }
              } else {
                var reason = "";
              } 
              alert('Could not find "'+search+ '" ' + reason);
            }
          }
        );
      }

      var gdir=new GDirections(map,document.getElementById("path"));

      GEvent.addListener(gdir,"error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = "Code "+code +" : "+reasons[code];
        } 
        alert("Failed to obtain directions, "+reason);
      });

      
      GEvent.addListener(gdir, "load", function() {
    	/*  if (poly) map.removeOverlay(poly);
        poly = gdir.getPolyline();
        map.addOverlay(poly); 
        */
        itiOn = true;
        removeMarks();
        removetMarks();
        
        $('#ginfos').hide();
        $('#path').show();
  	  	
        // document.getElementById("ginfos").className = "off";
        // document.getElementById("path").className = "on";
      });
      
      function directions() {
          if (addresses[0]) {var a = addresses[0] + "@" + path[0].toUrlValue(6);}
            else {var a = path[0].toUrlValue(6);} 
          if (addresses[1]) {var b = addresses[1] + "@" + path[1].toUrlValue(6);}
            else {var b = path[1].toUrlValue(6);} 
          var a = "from: "+a + " to: " + b;

          gdir.load(a, {getPolyline:true,getSteps:true});
        }

  	function showNarbonne()
  		{
  		var pNarbonne = new GLatLng(43.18246867845076,2.9970574378967285);
  		doStart(pNarbonne);
  		
  		if (addresses[0]) {var a = addresses[0] + "@" + path[0].toUrlValue(6);}
        else {var a = path[0].toUrlValue(6);} 
  		
  		if (addresses[1]) {var b = addresses[1] + "@" + path[1].toUrlValue(6);}
        else {var b = path[1].toUrlValue(6);} 
  		
        // points interm�diaires
        var pStLaurent = new GLatLng(43.085054442,2.6968002319335938);
  		b = "Saint Laurent de la Cabrerisse@" + pStLaurent.toUrlValue(6) +" to: "+b;
  		
  		createInterMarker(pStLaurent);
        var pMontredon = new GLatLng(43.18409588897154,2.9343581199645996);
  		b = "rond point de Montredon@" + pMontredon.toUrlValue(6) +" to: "+b;
  		createInterMarker(pMontredon);
  		
	
      	var a = "from: "+a + " to: " + b;
      	
	
      	gdir.load(a, {getPolyline:true,getSteps:true});		
  		}

  	function showCarca()
		{
		var pCarca = new GLatLng(43.204752475957406,2.3328351974487305);
		doStart(pCarca);
		
		if (addresses[0]) {var a = addresses[0] + "@" + path[0].toUrlValue(6);}
    	else {var a = path[0].toUrlValue(6);} 
		
		if (addresses[1]) {var b = addresses[1] + "@" + path[1].toUrlValue(6);}
    	else {var b = path[1].toUrlValue(6);} 
		
    	// points intermédiaires
    	/* var pStLaurent = new GLatLng(43.085054442,2.6968002319335938);
		b = "Saint Laurent de la Cabrerisse@" + pStLaurent.toUrlValue(6) +" to: "+b;
		
		createInterMarker(pStLaurent);
    	var pMontredon = new GLatLng(43.18409588897154,2.9343581199645996);
		b = "rond point de Montredon@" + pMontredon.toUrlValue(6) +" to: "+b;
		createInterMarker(pMontredon); */
		

  		var a = "from: "+a + " to: " + b;
  	

  		gdir.load(a, {getPolyline:true,getSteps:true});		
		}
		
		function showToulouse()
		{
		var pCarca = new GLatLng(43.54887367227833,1.4973163604736328);
		doStart(pCarca);
		
		var a = "Toulouse Autoroute des deux mers@" + path[0].toUrlValue(6);
		
		if (addresses[1]) {var b = addresses[1] + "@" + path[1].toUrlValue(6);}
    	else {var b = path[1].toUrlValue(6);} 
		
    	// points intermédiaires
    	var pVilla = new GLatLng(43.22509665752689,2.0347023010253906);
		b = "Villasavary@" + pVilla.toUrlValue(6) +" to: "+b;
		
		createInterMarker(pVilla);
    	
		

  		var a = "from: "+a + " to: " + b;
  	

  		gdir.load(a, {getPolyline:true,getSteps:true});		
		}
		
     doEnd(pJourdets);
		
	 
