  var map;
    var gdir;
geocoder = new GClientGeocoder();

    var addressMarker;   // ?
	

    function initialize(from,to) {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay); // added to trigger marker swap
		map.setCenter(new GLatLng(0,0),0);	//Inital setCenter()  added. Esa.

        setDirections(from, to, "en_US");

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

    function handleErrors(){
	   hideMap();
	   
	}
	
	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load() results.
	}
	  
	  
///////////////////////////////////////////////////////////////////////
// The add-on code for draggable markers
// Esa 2008
//
	var newMarkers = [];
	var latLngs = [];
	var icons = [];
	// Note the 'addoverlay' GEvent listener added inside initialize() function

	function onGDirectionsAddOverlay(){ 
	// Remove the draggable markers from previous function call.
	for (var i=0; i<newMarkers.length; i++) 
	{
		map.removeOverlay(newMarkers[i]);
	}

	// Loop through the markers and create draggable copies
	for (var i=0; i<=gdir.getNumRoutes(); i++) 
		{
		var originalMarker = gdir.getMarker(i);
		latLngs[i] = originalMarker.getLatLng();
		icons[i] = originalMarker.getIcon();
		newMarkers[i] = new GMarker(latLngs[i],{icon:icons[i], draggable:true, title:'Draggable'});
		map.addOverlay(newMarkers[i]);

		// Get the new waypoints from the newMarkers array and call loadFromWaypoints by dragend
		GEvent.addListener(newMarkers[i], "dragend", function()
		  {
		var points = [];
		for (var i=0; i<newMarkers.length; i++) 
			{
		points[i]= newMarkers[i].getLatLng();
			}
		gdir.loadFromWaypoints(points);
		  });

		//Bind 'click' event to original hidden marker
		copyClick(newMarkers[i],originalMarker);
		
		// hide or remove the original marker
		//originalMarker.hide();
		map.removeOverlay(originalMarker);
		}
		
		function copyClick(newMarker,oldMarker){
		GEvent.addListener(newMarker, 'click', function()
		  {GEvent.trigger(oldMarker,'click');
		  });
		}
			map.enableScrollWheelZoom();

// End of draggable markers code
// Esa 2008
///////////////////////////////////////////////////////////////////////
	}
	
	function hideMap(){
		     document.getElementById("map_canvas").style.display = 'none';
	}
	
	
	//Show Geocoded addres
function showAddress(address,message) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              document.getElementById("success").innerHTML = "<span style='color: #f00'>Failure!</span>";
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              document.getElementById("success").innerHTML = "<span style='color: #0f0'>Success!</span>";
              GEvent.addListener(marker,"click", function(){
              	marker.openInfoWindowHtml(message);
              });
            }
          }
        );
      }
    }