﻿ var map;
    var gdir;
    var geocoder = null; // ?
    var addressMarker;   // ?
    var ErrorMsg1 = "We could not calculate driving directions from the location you entered.  Please enter a different \"From\" location.";
    var ErrorMsg2 = "We could not calculate driving directions from the location you entered.  Please enter a different \"From\" location.";


    function initialize() {
      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);
        var getdirections = document.getElementById("directions");
//        GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay); // added to trigger marker swap

       
       geocoder = new GClientGeocoder();
       var tAddress = document.getElementById("toAddress").value;
       
       showAddress(tAddress);
       var fromAddress = document.getElementById("fromAddress");
       fromAddress.focus();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

      }
    }
    function ShowDirections(){
    
    var fromAddress = document.getElementById("fromAddress").value;
    var toAddress = document.getElementById("toAddress").value;
    
    
    var locale = "en_US";
    if(fromAddress == ""){
        alert("Please Enter Valid Address");
    }
    else{
        setDirections(fromAddress, toAddress, locale);
        
    }
    
    
    
    }
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale , "getSteps":true});
                gdir.visible = false;
    }
    

    function handleErrors(){
    
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)

       {
        SetErrorMsg(1);
        }
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)

       {
        SetErrorMsg(2);
        }
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)

       {
       SetErrorMsg(2);
        }

	
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)

        {
        SetErrorMsg(2);
        }

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)

        {
        SetErrorMsg(2);
        }
	    
	   else 
	   {
	    SetErrorMsg(1);
        
        }

	   
	}
	
	function SetErrorMsg(msgno){
	var fromAddress = document.getElementById("fromAddress");
	var tAddress = document.getElementById("toAddress").value;
       
       showAddress(tAddress);
	    if(msgno==1){
	        alert(ErrorMsg1);
	       fromAddress.focus();
	       fromAddress.select();
	    }
	    else{
	    alert(ErrorMsg2);
            fromAddress.focus();
	       fromAddress.select();
	    }
	}
	
	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load() results.
	}
	  
	  
	  function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
//              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
///////////////////////////////////////////////////////////////////////
// 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');
		  });
		}
// End of draggable markers code
// Esa 2008
///////////////////////////////////////////////////////////////////////
	}
