﻿var directions;

// Creates a marker on the map
function createMarker(point,description,address,showOnLoad)
{
    var html;
    html = description;
    html = html + "<br /><br /><input type='text' SIZE=40 MAXLENGTH=40 style='color: #CCCCCC' name='saddr' id='saddr' value='Address, City, VA' /><br />";
    html = html + "<a href='javascript:setToDirections(\"" + address + "\");'>Directions</a>";
    html = html + "   |   ";
    html = html + "<a href='javascript:setFromDirections(\"" + address + "\");'>Return Directions</a>";
    
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });

    if (showOnLoad)
        marker.openInfoWindowHtml(html);
    
    return marker;
}

// Sets the to directions
function setToDirections(address)
{
    var from = document.getElementById("saddr").value;
    var dest = address;

    setDirections(from, dest);
}

// Sets the from directions
function setFromDirections(address)
{
    var from = address;
    var dest = document.getElementById("saddr").value;

    setDirections(from, dest);
}

// Displays the directions from the address to the destination
function setDirections(fromAddress, destAddress)
{
    // Get the From/Dest addresses
    var from = fromAddress;
    var dest = destAddress;

    if (from == "")
    {
        alert("Please enter an address, city, and state.");
        return false;
    }

    // Load the directions map
    var map = new GMap2(document.getElementById('map_directions'));
    map.setUIToDefault();

    // Load the directions route
    var directionsPanel = document.getElementById("route");
    directionsPanel.innerHTML = "";
    directions = new GDirections(map, directionsPanel);
    GEvent.addListener(directions, "error", googleMapHandleErrors);
    directions.load("from: " + from + " to: " + dest);
}

// Displays any errors with the directions
function googleMapHandleErrors()
{
    if (directions.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: " + directions.getStatus().code);
    else if (directions.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: " + directions.getStatus().code);
    else if (directions.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: " + directions.getStatus().code);

    //   else if (directions.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 (directions.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: " + directions.getStatus().code);

    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);

    else alert("An unknown error occurred.");
}