var BUSINESS_ADDRESS = "8725 S. Kyrene Road, Tempe, AZ 85284";

var map;
var dir;
var getDirectionToHere = true;

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/, '');
}

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("mapDiv"));

		dir = new GDirections(map, document.getElementById("directionDiv"));
		GEvent.addListener(dir, "load", onDirLoad);
		GEvent.addListener(dir, "error", handleDirErrors);

		var geocoder = new GClientGeocoder();

		geocoder.getLatLng(BUSINESS_ADDRESS, function(point) {
			var marker = new GMarker(point);
			var infoWindowContentHtml = document.getElementById("infoWindowContentDiv").innerHTML;

			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(infoWindowContentHtml);
			});

			map.setCenter(point, 14);
			map.addOverlay(marker);

			marker.openInfoWindowHtml(infoWindowContentHtml);
		});
	}
}


function toHere() {
	document.getElementById("startAddressDiv").style.display = "block";
	document.getElementById("endAddressDiv").style.display = "none";
	document.getElementById("addressInputDiv").style.display = "block";

	getDirectionToHere = true;
}


function fromHere() {
	document.getElementById("startAddressDiv").style.display = "none";
	document.getElementById("endAddressDiv").style.display = "block";
	document.getElementById("addressInputDiv").style.display = "block";

	getDirectionToHere = false;
}


function setDirection() {
	var startOrEndAddress = document.getElementById("addressInputBox").value.trim();

	if (startOrEndAddress == "") {
		return;
	}

	document.getElementById("directionDiv").innerHTML = "";

	if (getDirectionToHere) {
		getDirection(startOrEndAddress, BUSINESS_ADDRESS);
	} else {
		getDirection(BUSINESS_ADDRESS, startOrEndAddress);
	}
}


function getDirection(from, to) {
	dir.load("from: " + from + " to: " + to, { "locale": "en_US" });
}


function onDirLoad() {
}


function handleDirErrors() {
	var e = document.getElementById("directionDiv");
	var code = dir.getStatus().code;

	switch (code) {
	case G_GEO_BAD_REQUEST:
		e.innerHTML = "The address you entered could not be parsed.  Please try again.";
		break;

	case G_GEO_SERVER_ERROR:
		e.innerHTML = "The map server is currently experiencing problems.  Please try again later.";
		break;

	case G_GEO_UNKNOWN_ADDRESS:
		e.innerHTML = "The address you entered could not be found.  Perhaps it is too new or it is incorrect.  Please try again.";
		break;

	case G_GEO_UNAVAILABLE_ADDRESS:
		e.innerHTML = "The address you entered can not be displayed due to contractual or legal reasons.";
		break;

	case G_GEO_UNKNOWN_DIRECTIONS:
		e.innerHTML = "The map server could not calculate the route between the two addresses.  Please try a different address.";
		break;

	case G_GEO_TOO_MANY_QUERIES:
		e.innerHTML = "The map server could not process more route requests at this time.  Please try again later.";
		break;

	default:
		e.innerHTML = "An unknown error has occurred.  Please contact Fast Athlete USA.";
		break;
	}
}