var map;
var gdir;
var geocoder = null;
var addressMarker;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(41.946979,-87.977507), 9);
    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;
    }

    map.openInfoWindowHtml(new GLatLng(41.846979,-87.977507),"<strong>Oak Brook office</strong><br>2425 W. 22nd Street, Suite 213<br>Oak Brook, IL 60523");

    var latlng = new GLatLng(41.846979,-87.977507);
    map.addOverlay(createMarker(latlng, 0, "<strong>Oak Brook office</strong><br>2425 W. 22nd Street, Suite 213<br>Oak Brook, IL 60523"));
    var latlng = new GLatLng(41.762525,-88.151765);
    map.addOverlay(createMarker(latlng, 1, "<strong>Naperville office</strong><br>120 Spalding Drive, Suite 305<br>Naperville, IL 60540"));
    var latlng = new GLatLng(42.244611,-87.95429);
    map.addOverlay(createMarker(latlng, 2, "<strong>Vernon Hills office</strong><br>230 Center Drive<br>Vernon Hills, IL 60061"));

    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.846979,-87.977507), 13);
      map.openInfoWindowHtml(map.getCenter(),"<strong>Oak Brook office</strong><br>2425 W. 22nd Street, Suite 213<br>Oak Brook, IL 60523");
    } else if (loc == 2) {
        map.setCenter(new GLatLng(41.762525,-88.151765), 13);
        map.openInfoWindowHtml(map.getCenter(),"<strong>Naperville office</strong><br>120 Spalding Drive, Suite 305<br>Naperville, IL 60540");
    } else if (loc == 3) {
        map.setCenter(new GLatLng(42.244611,-87.95429), 13);
        map.openInfoWindowHtml(map.getCenter(),"<strong>Vernon Hills office</strong><br>230 Center Drive<br>Vernon Hills, IL 60061");
    }
}

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(){ 
}
