startList = function() {
	var aServicesPage = RegExp("Services", "i").test(document.title) || RegExp("Estimates", "i").test(document.title) || RegExp("Packing", "i").test(document.title) || RegExp("Moving", "i").test(document.title) || RegExp("Distance Map", "i").test(document.title);
	if (aServicesPage) {
		document.getElementById("services-subnav").style.display = 'block';
	} else {
		navServices = document.getElementById("nav-services");
		navServices.onmouseover = function() {
			document.getElementById("services-subnav").style.display = 'block';
		}
		navServices.onmouseout = function() {
			document.getElementById("services-subnav").style.display = 'none';
		}
	}
	var nav = new Array();
	nav[0] = document.getElementById("nav-company");
	nav[1] = document.getElementById("nav-tips");
	nav[2] = document.getElementById("nav-specials");
	for (i=0; i < nav.length; i++) {
		nav[i].onmouseover = function() {
			if (aServicesPage) {
				document.getElementById("services-subnav").style.display = 'none';
			}
		}
		nav[i].onmouseout = function() {
			if (aServicesPage) {
				document.getElementById("services-subnav").style.display = 'block';
			}
		}
	}
}

var map;
var gdir;
var geocoder = null;
var addressMarker;
var gdirFromAddress;
var gdirToAddress;
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
        	map.setCenter(new GLatLng(40.027719,-95.613281), 3);
		gdir = new GDirections(map, document.getElementById("directionsOutput"));
		GEvent.addListener(gdir, "load", function() { onGDirectionsLoad(gdir, gdirFromAddress, gdirToAddress) });
		GEvent.addListener(gdir, "error", handleErrors);
	}
}
function setDirections(fromAddress, toAddress) {
	gdirFromAddress = fromAddress;
	gdirToAddress = toAddress;
	gdir.load("from: " + fromAddress + " to: " + toAddress);
}
function onGDirectionsLoad(gdir, fromAddress, toAddress) {
	document.getElementById('movingDistance').innerHTML = "<p>Distance from "+fromAddress+" to "+toAddress+" is "+gdir.getDistance().html+".<br />Get an <a href='estimates.php'>estimate</a> for a move.</p>";
}
function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		document.getElementById('movingDistance').innerHTML = "<p class='formError'>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.</p>";
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		document.getElementById('movingDistance').innerHTML = "<p class='formError'>A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.</p>";
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		document.getElementById('movingDistance').innerHTML = "<p class='formError'>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.</p>";
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		document.getElementById('movingDistance').innerHTML = "<p class='formError'>The given key is either invalid or does not match the domain for which it was given.</p>";
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		document.getElementById('movingDistance').innerHTML = "<p class='formError'>A directions request could not be successfully parsed.</p>";
	else document.getElementById('movingDistance').innerHTML = "<p class='formError'>An unknown error occurred.</p>";
}

window.onload = function(e) {
	startList();
	load();
}