﻿/* This file will have all the javascript functions which are used to get google maps direction
    Created : 08/08/2008
    Created by : Nas
*/

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
    var localSearch = new GlocalSearch();
    var toAddressPoint="";

    /**This function will send AJAX request to Google AJAX Map to get exact Log and Lati of from address and to address*/
      function usePointFromPostcode(postcode, callbackFunction) {
      localSearch.setSearchCompleteCallback(null,
        function() {
                if (localSearch.results[0]) {    
                    var resultLat = localSearch.results[0].lat;
                    var resultLng = localSearch.results[0].lng;
                    var point = new GLatLng(resultLat,resultLng);
                    callbackFunction(point);
                }else{
                    alert("Postcode not found!");
               }
        });  
    
        localSearch.execute(postcode + ", UK");
        }
        
        
              
             
    /*This function used by get direction button which will call usePointFromPostcode method*/              
              
    function getPoints(addressFrom,addressTo)
    {
    
        if(addressFrom=="")
        {
            alert("Please enter your postcode and then search.")
            return false;
        }
        var addressFromPoints,addressToPoints;
        usePointFromPostcode(addressFrom,
        function (point) {
            addressFromPoints = point.lat() + ',' + point.lng();
            
            setDirections(addressFromPoints, toAddressPoint, "en_UK")
            

        }); // end of first calling function
      
    
    }
                  
    
    /*This is called in onload page to initiliaze map*/    
    function initialize(lt,ln,parkAddress) {
    
      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);
      
        point = new GLatLng(lt, ln)
        
        toAddressPoint=point
        
        map.setCenter(point, 13);

        // Create a marker
        marker = new GMarker(point);
        // Add the marker to map

        map.addOverlay(marker);
        // Add address information to marker
        marker.openInfoWindowHtml(parkAddress);
  
       GEvent.addListener(marker, "click", function()
       {marker.openInfoWindowHtml(parkAddress);});
       
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
                
      }
    }
    
    
    
    function createMarker(point, title, code, info)
                {
                        var marker = new GMarker(point,
{title:title,icon:code})
                        marker.title = code;
                        var html =  info;
                        maxWidth = 220;
                        GEvent.addListener(marker, "click", function()
{marker.openInfoWindowHtml(html,{maxWidth:maxWidth});});
                        return marker;
                }; 
                
                
    /*This sets the direction on the map we need to pass Lat,Long of from and to addresss*/
    
    function setDirections(fromAddress, toAddress, locale) {
    
      var a1 = fromAddress.toString();
      a1 = a1.replace('(','').replace(')','');
      
      var a2 = toAddress.toString();
      a2 = a2.replace('(','').replace(')','');
      
      gdir.load("from: " + a1 + " to: " + a2,
                { "locale": locale });
    }

    /*Used to get the error message*/
    function handleErrors(){
	   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.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
	
/* can be used to display information on map*/	
  function addToMap(response)
   {
      // Retrieve the object
      place = response.Placemark[0];

       //Retrieve the latitude and longitude
      point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);

      // Center the map on this point
      
      map.setCenter(point, 13);

      // Create a marker
      marker = new GMarker(point);

      // Add the marker to map
      map.addOverlay(marker);

      // Add address information to marker
      marker.openInfoWindowHtml(point,"some text");
   }
   