// Define variables
var map;
var directions;

function startMap() {
  if (GBrowserIsCompatible()) {
  
    // Create map
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(43.2364477,-88.2232553), 14);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    // Add info bubble
    /* var info = '<div style="color: black; text-align: left;">Hubertus House of Horror</div>';
    map.openInfoWindowHtml(map.getCenter(), info); */
    
    // Set icon shadow, dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "";
    baseIcon.iconSize = new GSize(34, 32);
    baseIcon.shadowSize = new GSize(0, 0);
    baseIcon.iconAnchor = new GPoint(20, 0);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
    // Set image URL
    var icon = new GIcon(baseIcon);
    icon.image = "http://hubertushouseofhorror.com/images/directions/map/icon.png";
    
    // Set marker location and add to map
    var marker = new GMarker(new GLatLng(43.2364477,-88.2232553), { icon:icon });
    map.addOverlay(marker);
    
    // Directions
    document.getElementById('from-location').value = '';
    
    var directionsPanel = document.getElementById("route-directions");
    directions = new GDirections(map, directionsPanel);
    GEvent.addListener(directions, "load", onDirectionsLoad);
    GEvent.addListener(directions, "error", handleErrors);
    
    /* setDirections('N91 W24209 Crooked Bridge Court, Sussex, WI', '3733 Hubertus Road, Hubertus, WI 53033 (Hubertus House of Horror)'); */
    
  }
  else {
    alert('Your browser is not compatible!');
  }
}

function resetMap() {
  document.getElementById('route').style.display = 'none';
  document.getElementById('route-general').style.display = 'block';
  document.getElementById('from-location').value = '';
  document.getElementById('from-holder').style.backgroundImage = "url(images/directions/directions/from.png)";
  
  directions.clear();
  map.setCenter(new GLatLng(43.2364477,-88.2232553), 14);
  map.setMapType(G_NORMAL_MAP);
}

function setDirections(fromAddress) {
  directions.load("from: " + fromAddress + " to: 3733 Hubertus Road, Hubertus, WI 53033 (Hubertus House of Horror)");
  
  var location = "http://maps.google.com/maps?f=d&source=s_d&hl=enie=UTF8&pw=2&daddr=3733+hubertus+rd+hubertus+wi&saddr="+fromAddress+"&z="+map.getZoom();
  document.getElementById('print-link').href=location;
}

function handleErrors(){
  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_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.");
  }
}

function onDirectionsLoad() {
  // Hide general directions and show specific
  document.getElementById('route-general').style.display = 'none';
  document.getElementById('route').style.display = 'block';
  
  // Make sure the sidebar is open!
  document.getElementById('route-holder').style.display = 'block';
}

function checkDirectionsKey(e) {
  var key;

  if (window.event) { // IE
    key = e.keyCode;
  }
  else if (e.which) { // Netscape/Firefox/Opera
    key = e.which;
  }

  if (key == 13) {
    submitDirections();
  }
}

function submitDirections() {
  setDirections(document.getElementById('from-location').value);
}

function closeRoute() {
  document.getElementById('route-holder').style.display = 'none';
  document.getElementById('route-open').style.display = 'block';
}
function openRoute() {
  document.getElementById('route-holder').style.display = 'block';
  document.getElementById('route-open').style.display = 'none';
}

function removeFromExplain() {
  document.getElementById('from-holder').style.backgroundImage = 'none';
}

