var map;
var gdir;
var geocoder = null;
var NBDental = new GLatLng(34.24985, -119.1959);
var NBDmarker;

function placeNBD() {
	NBDmarker = new GMarker(NBDental);
	map.addOverlay(NBDmarker);
    map.openInfoWindow(NBDental,'<div class="marker"><div class="mkr_title"><em><strong>Northbank Dental Center</strong></em></div>' +
		'2772 Johnson Dr. Suite 101<br />Ventura, CA 93003<br />(805) 658-1577' +
		'</div>' +
		''
	);
}

function getDirections(address,locale)
{
	var to = '2772 Johnson Dr. Suite 101 Ventura, CA 93003';
	var from = address;
	var locale = locale;

	gdir.load("from: " + from + " to: " + to, { "locale": locale });
	if ($defined(NBDmarker))
	{
		NBDmarker.remove();
	}
	return false;
}

function handleErrors()
{
	switch(gdir.getStatus().code)
	{
		case 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);
		break;
		case 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);
		break;
		case 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);
		break;
		case 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: " + gdir.getStatus().code);
		break;
		case 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);
		break;
		case G_GEO_BAD_REQUEST:
			alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		break;
		default:
			alert("An unknown error occurred.");
	}
}

function printPopup(address,locale)
{
	window.open('print_directions.php?address=' + address + '&amp;locale=' + locale,'PrintDirections','location=no,menubar=no,status=no,titlebar=no,toolbar=no,directories=no');
}

function printLink()
{
	if ($defined($('printlink')))
	{
		var from = $('address').value;
		var locale = $('locale').value;
		$('printlink').innerHTML = '<a href="javascript:printPopup(\'' + from + '\',\'' + locale + '\');">Print These Directions</a>';
	}
}

if (GBrowserIsCompatible())
{
	map = new GMap2($("map_canvas"));
	gdir = new GDirections(map, $("directions"));
	GEvent.addListener(gdir, "error", handleErrors);
	GEvent.addListener(gdir, "load", printLink);


	map.setCenter(NBDental, 16);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	placeNBD();
}
