
var map;
var geocoder;
var directions;
var home = "865 Lombardi Avenue Green Bay, WI 54304";

// load the map as soon as the DOM is available
Event.onDOMReady(function() { loadMap(); });

function loadMap()
{
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("directions_map"));
		map.enableDoubleClickZoom();

		geocoder = new GClientGeocoder();

        // control position
        overview = new GOverviewMapControl();
        control = new GSmallMapControl();
        map.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT, 0));
        map.addControl(overview);

        // show tundra lodge
        showAddress(home, "Tundra Lodge");
	}
}

function showAddress(address, title) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);

        if (title != '') {
        	marker.openInfoWindowHtml("<h2>"+ title +"</h2>"+ address);
        }
      }
    }
  )
}

function fillForm(address, city, state, zip, location, url)
{
	$('Street').value = address;
	$('City').value = city;
	$('State').value = state;
	$('Zip').value = zip;
	$('Location').value = location;
	$('URL').value = url;

	mapDirections();
}

function mapDirections()
{
	$('printLink').style.visibility = "hidden";
	$('directionsLink').style.visibility = "hidden";

	// log request in database
	new Ajax.Request(
       "/directions/ajax.php?street="+$('Street').value+"&city="+$('City').value+"&state="+$('State').value+"&zip="+$('Zip').value,
       {
           method:'get'
       }
    );

	// change link text
	$('addressLink').innerHTML = "Change Address";
	$('directions_text').innerHTML = "";

	// clear all previous markers
	map.clearOverlays();

	from = $('Street').value + ", " + $('City').value + ", " + $('State').value + " " + $('Zip').value;

	directionsPanel = document.getElementById("directions_text");
	directions = new GDirections(map, directionsPanel);

	GEvent.addListener(directions, "load", mapLoaded);
	GEvent.addListener(directions, "error", handleErrors);

	// show tooltip
	if ($('Location').value == ""){
		$('Location').value = "My Location";
	}
	tooltip = $('Location').value + "<br />" +  "<br /><a href='"+$('URL').value+"'>" + $('URL').value + "</a>";
	showAddress(from, tooltip);

	directions.load("from: "+ from + " to: " + home);

	$('printLink').href = "http://maps.google.com/maps?f=d&hl=en&geocode=&saddr="+from+"&daddr="+home;

	// clear tooltip info in case the user changes address
	$('Location').value = "";
	return false;
}

    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_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.");

	}

function mapLoaded()
{
	$('printLink').style.visibility = "visible";
	$('directionsLink').style.visibility = "visible";
	showMap();
}

function showForm()
{
	$('directions_form').style.visibility = "visible";
	$('closeButton').style.visibility = "visible";
	$('directions_text').style.visibility = "hidden";
}

function showDirections()
{
	$('directions_text').style.visibility = "visible";
	$('closeButton').style.visibility = "visible";
	$('directions_form').style.visibility = "hidden";
}

function showMap()
{
	$('directions_text').style.visibility = "hidden";
	$('closeButton').style.visibility = "hidden";
	$('directions_form').style.visibility = "hidden";
}

