/*

File: directions.js
Purpose: To display an interactive map via the Google Maps API. Directions are
  mapped when appropriate input is supplied. Maps parameters may be changed to
  better suit a given page.
Author: Ryan Brady
Last modified: July 2007
Requirements:
  - An Google Maps API key (<http://www.google.com/apis/maps/>) referenced in
    the <head> element via the <script> element. It must have at least the v=2.x
    parameter.
    e.g., <script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=#"></script>
  - onload() and onunload() events in the <body> tag. The GUnload() function is
    handled by Google.
    e.g., <body onload="loadMap('pagename');" onunload="GUnload();">
  - An element, such as a <div>, with an id attribute of "google_map"
Notes:
  - The page arquement isn't used in this particular implementation

*/



//==============================================================================
// Variables
//==============================================================================

var directions;
var directionsContainer;
var icon;
var latitude;
var longitude;
var map;
var mapContainer;
var marker;
var point;
var visited

latitude = 43.05362181481764;                                                   /* 3 Greenleaf Woods Drive, Portsmouth, NH  03801 */
longitude = -70.77039867639542;                                                 /* 3 Greenleaf Woods Drive, Portsmouth, NH  03801 */



//==============================================================================
// Display the map and its features
//==============================================================================

function loadMap(page) {

  if (GBrowserIsCompatible()) {

    mapContainer = document.getElementById("google_map");
    map = new GMap2(mapContainer);

    // Add controls ------------------------------------------------------------
    map.addControl(new GSmallMapControl());                                     // Pan/zoom controls. Alternatively: GLargeMapControl()
//  map.addControl(new GMapTypeControl());                                      // Display-type controls (map, satellite, hybrid)

    // Display a single point to start (no default directions)
    singlePoint(latitude, longitude);
    
    // Set-up the directions for future calls
    directions = new GDirections(map);                                          // To include text directions, add the id of the directions container as an argument
    //directions.load("500 Memorial Drive, Cambridge, MA to 3 Greenleaf Woods Drive, Portsmouth, NH 03801"); // An alternative method
//    GEvent.addListener(directions, "error", handleErrors);
    
  }

}



//==============================================================================
// Get the directions
//==============================================================================

function setDirections(fromAddress, toAddress) {

  if (fromAddress == "DHK" || fromAddress == "dhk") {
    fromAddress = "3 Greenleaf Woods Drive, Portsmouth, NH  03801";
  }
  if (toAddress == "DHK" || toAddress == "dhk") {
    toAddress = "3 Greenleaf Woods Drive, Portsmouth, NH  03801";
  }
  map.removeOverlay(marker);                                                    // Remove the single-point marker if it is there
  directions.load("from: " + fromAddress + " to: " + toAddress);
  
}



//==============================================================================
// Auxiliary functions
//==============================================================================


// Create custom marker icon ---------------------------------------------------
function customIcon() {

    icon = new GIcon();
    icon.image = "http://www.google.com/intl/en_us/mapfiles/arrow.png";
    icon.shadow = "http://www.google.com/intl/en_us/mapfiles/arrowshadow.png";
    icon.iconSize = new GSize(39, 34);
    icon.shadowSize = new GSize(39, 34);
    icon.iconAnchor = new GPoint(19, 34);
    icon.infoWindowAnchor = new GPoint(19, 1);

}


// Handle errors ---------------------------------------------------------------
function handleErrors() {                                                       // Code from Google

   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: " + directions.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.");
   }

}


// Create a pop-up label window for the marker on click ------------------------
function labelMarker() {

  marker.openInfoWindowHtml("<acronym>DHK</acronym> Financial Advisors, <abbr title=\"Incorporated\">Inc.</abbr><br />3 Greenleaf Woods Drive<br />Suite 202<br />Portsmouth, <abbr title=\"New Hampshire\">NH</abbr>&#x2003;03801");

}


// Display a maker for a single location ---------------------------------------
function singlePoint(lat, long) {

  customIcon();
  point = new GLatLng(lat, long);                                               // The location of interest (latitude, longitude)
  map.setCenter(point, 6);                                                      // Maximum zoom = 17
  marker = new GMarker(point, icon);                                            // To use a custom icon, add the icon object as an argument
  map.addOverlay(marker);                                                       // Mark the location with a red marker (This line must be after map.setCenter())
  GEvent.addListener(marker, "click", labelMarker);                             // Display a label for the maker on click

}

