var gdir;
var map;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    var point = new GLatLng(41.483179, -81.833858);
  
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

  
    map.setCenter(point, 13);
    map.addOverlay(createMarker(point, map));
   
  }
}

function createMarker(point, map) {
   var marker = new GMarker(point);  
   GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml("Stino da Napoli<br />19070 Old Detroit Road<br />"
    + "Rocky River, OH 44116<br /><b><i>Telephone: 440 331-3944</i></b>", 25);  });
   GEvent.addListener(marker, "infowindowclose", function() { map.panTo(point); });
   return marker;
}


function loadDirections() {
    if (GBrowserIsCompatible())
    {
        var startingLocation;
        var endingLocation = "19070 Old Detroit Rd Cleveland, OH 44116";
        if (document.getElementById("ctl00_ContentPlaceHolder1_StartingAddr"))
        {
           startingLocation = document.getElementById("ctl00_ContentPlaceHolder1_StartingAddr").value;
        }
        else
        {
            startingLocation = "Cleveland, OH";
        }
        
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
        gdir = new GDirections(map, document.getElementById("directions"));
        //GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        
        setDirections(startingLocation, endingLocation, "en_US");
    }
}


function setDirections(fromAddress, toAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + toAddress,
     { "locale": locale });
}

function onGDirectionsLoad()
{
    // Use this function to access information about the latest load()
    // results.
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
    // and yada yada yada...
}


function handleErrors()
{
    var directions;
    
    directions = document.getElementById("directions");
    map = document.getElementById("map");

    if (map)
        map.style.height = "200px" ;

    
    if (directions)
    {
        if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>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 + "</p>";
        else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
            directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>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 + "</p>";
        else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	        directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>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 + "</p>";
	     else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	        directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code + "</p>";
        else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
            directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code + "</p>";
        else
            directions.innerHTML = "<p style=\"font-weight:bold;color:red;\">Errors Encountered</p><p>An unknown error occured.</p>";

    }
   
}