var map, gdir, geocoder = null, addressMarker, shadys;

function init()
{
	ReturnControl = function() {};
	
	// To "subclass" the GControl, we set the prototype object to
	// an instance of the GControl object
	ReturnControl.prototype = new google.maps.Control();

	// Creates a one DIV for each of the buttons and places them in a container
	// DIV which is returned as our control element. We add the control to
	// to the map container and return the element for the map class to
	// position properly.
	ReturnControl.prototype.initialize = function(map) {
	  var container = document.createElement("div");

	  var returnDiv = document.createElement("div");
	  this.setButtonStyle_(returnDiv);
	  container.appendChild(returnDiv);
	  returnDiv.appendChild(document.createTextNode( 'Shady\'s' ));
	  GEvent.addDomListener(returnDiv, "click", function() {
		map.returnToSavedPosition();
	  });

	  map.getContainer().appendChild(container);
	  return container;
	}

	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	ReturnControl.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(35, 10)); // new GSize( 7, 7 )
	}

	// Sets the proper CSS for the given button element.
	ReturnControl.prototype.setButtonStyle_ = function(button) {
	  //button.style.textDecoration = "underline";
	  button.style.color = "#0000cc";
	  button.style.backgroundColor = "white";
	  button.style.font = "0.6em Arial";
	  button.style.border = "1px solid black";
	  button.style.padding = "2px";
	  button.style.marginBottom = "3px";
	  button.style.textAlign = "center";
	  //button.style.width = "5em";
	  button.style.cursor = "pointer";
	}
	
	if( GBrowserIsCompatible() )
	{
		map = new GMap2( document.getElementById( 'mapContainer' ) );
		gdir = new GDirections( map, document.getElementById( 'mapDirections' ) );
		shadys = new GLatLng( 39.837626, -75.009170 );
		GEvent.addListener( gdir, 'load', gDirectionsHandler );
		GEvent.addListener( gdir, 'error', gErrorHandler );
		
		map.setCenter( shadys, 15 );
		map.addControl( new ReturnControl() );
		map.addOverlay( new GMarker( shadys ) );
		map.savePosition();
		map.checkResize();
		map.setUIToDefault();
	}
	$(document.body).unload( function()
	{
		GUnload();
	});
}

function setDirections( from )
{
	gdir.load( 'from: ' + from + ' to: 814 S. White Horse Pike, Somerdale, NJ 08083', { 'locale': 'en-US' } );
}

function gErrorHandler()
{
	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.");
}

function gDirectionsHandler()
{
}