var map;
var gdir;
var geocoder;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(41.381961,-82.087784), 10);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    var baseIcon = new GIcon();
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);

    function createMarker(point, index, html) {
      var letter = String.fromCharCode("A".charCodeAt(0) + index);
      var letteredIcon = new GIcon(baseIcon);
      letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

      markerOptions = { icon:letteredIcon };
      var marker = new GMarker(point, markerOptions);

      GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
      return marker;
    }

    var latlng = new GLatLng(41.424015,-82.086247);
    map.addOverlay(createMarker(latlng, 0, "<strong>Sheffield Village Office</strong><br>5001 Transportation Dr.<br>Sheffield Village, OH 44054"));
    var latlng = new GLatLng(41.294611,-82.225234);
    map.addOverlay(createMarker(latlng, 1, "<strong>Oberlin Office</strong><br>224 W. Lorain St.<br>Oberlin, OH 44074"));
    var latlng = new GLatLng(41.456317,-81.949124);
    map.addOverlay(createMarker(latlng, 2, "<strong>Westlake Office</strong><br>2211 Crocker Rd.<br>Westlake, OH 44145"));

    gdir = new GDirections(map, document.getElementById("directions"));

    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
  }
}

function locate(loc){
    if (loc == 1) { 
      map.setCenter(new GLatLng(41.424015,-82.086247), 13);
      map.openInfoWindowHtml(map.getCenter(),"<strong>Sheffield Village Office</strong><br>5001 Transportation Dr.<br>Sheffield Village, OH 44054");
    } else if (loc == 2) {
        map.setCenter(new GLatLng(41.294611,-82.225234), 13);
        map.openInfoWindowHtml(map.getCenter(),"<strong>Oberlin Office</strong><br>224 W. Lorain St.<br>Oberlin, OH 44074");
    } else if (loc == 3) {
        map.setCenter(new GLatLng(41.456317,-81.949124), 13);
        map.openInfoWindowHtml(map.getCenter(),"<strong>Westlake Office</strong><br>2211 Crocker Rd.<br>Westlake, OH 44145");
    }
}

function setDirections(fromAddress, toAddress, locale) {
  document.getElementById("directions").style.display = "inline";
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
  changeMap(fromAddress, toAddress, locale);
}

function changeMap(fromAddress, toAddress, locale){
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

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_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." + gdir.getStatus().csode);
}

function onGDirectionsLoad(){ 
}
