﻿var map;
var icon;
var directions;

var ERRORS = {
  map: "Sorry, we're unable to access facility directions right now. Please try again later",
  from: "Please fill in the 'From' field",
  to: "Please fill in the 'To' field",
	address: "No corresponding geographic location could be found for one of the specified addresses.",
  general: "Sorry, an error prevented us from providing directions at this time."
};

var isIE6 = false/*@cc_on || @_jscript_version < 5.7@*/;

$(window).unload(unloadMap);
$(document).keypress(function(event) {
  if (event.keyCode == 13) {
    getDirections();
    return false;
  }
});

function loadMap(lat, lng) {
  if (GMap2 && GBrowserIsCompatible()) {

    // Create map
    map = new GMap2($('#map')[0]);
    map.addControl(new GLargeMapControl());

    // Add facility
    if (lat && lng) {
      var icon = new GIcon();
      icon.iconSize = new GSize(23, 22);
      icon.iconAnchor = new GPoint(7, 21);
      icon.infoWindowAnchor = new GPoint(7, 21);
      if (isIE6) {  // IE6 can't handle all the markers
          icon.image = "/sites/global/images/marker.gif";
          icon.transparent = null;
          icon.shadow = null;
          icon.shadowSize = null;
          icon.infoShadowAnchor = null;
      } else {
          icon.image = "/sites/global/images/marker.png";
          icon.shadow = "/sites/global/images/marker_shadow.png";
          icon.shadowSize = new GSize(23, 22);
          icon.infoShadowAnchor = new GPoint(7, 21);
      }

      var options = { icon: icon };
      var point = new GLatLng(lat, lng);
      map.addOverlay(new GMarker(point, options));
      map.setCenter(point, 9);
    } else {
      map.setCenter(new GLatLng(38.5, -95), 4);
    }

    // Create directions
    directions = new GDirections(map, document.getElementById('directions'));
    GEvent.addListener(directions, "error", handleErrors);
  } else {
    error(ERRORS.map);
  }
}

function unloadMap() {
  if (GMap2) GUnload();
}

function getDirections() {
  var from = jQuery.trim($("#directions_from").val());
  var to = jQuery.trim($("#directions_to").val());
  if (from.length == 0)
    error(ERRORS.from);
  else if (to.length == 0)
    error(ERRORS.to);
  else
    directions.load("from: " + from + " to: " + to);
}

function handleErrors(){
  switch (directions.getStatus().code) {
  case G_GEO_UNKNOWN_ADDRESS:
    error(ERRORS.address);
    break;
	case G_GEO_SERVER_ERROR:
    error(ERRORS.general, 'G_GEO_SERVER_ERROR');
    break;
	case G_GEO_MISSING_QUERY:
    error(ERRORS.general, 'G_GEO_MISSING_QUERY');
    break;
	case G_GEO_BAD_KEY:
    error(ERRORS.general, 'G_GEO_BAD_KEY');
    break;
  case G_GEO_BAD_REQUEST:
    error(ERRORS.general, 'G_GEO_BAD_REQUEST');
    break;
  default:
    error(ERRORS.general, directions.getStatus().code);
    break;
  }
}

function error(text, code) {
  var elt = $("#error");
  if (code) text += " Error code: " + code;
  elt.text(text);
  elt.fadeIn();
}

