var xmlDirections;
var map, gdirections;
var dirStart, dirEnd, loadingDirs;

function onDirectionsError() {
	var errObj = gdirections.getStatus();
	
	document.getElementById("div_status").innerHTML.replace("Loading directions ", "") + " ERROR";
}

function onDirectionsLoaded() {
	map.removeOverlay(dirStart);
	map.removeOverlay(dirEnd);
	
	document.getElementById("div_status").innerHTML = document.getElementById("div_status").innerHTML.replace("Loading directions ", "") + " LOADED";
}

function addStartDirection(el, directionNode) {
	var latlng = getLatLng(directionNode);
	addStartMarker(latlng, directionNode);
	showMarkers();
}
function addEndDirection(el, directionNode) {
	var latlng = getLatLng(directionNode);
	addEndMarker(latlng, directionNode);
	showMarkers();
}
function loadDirections() {
	loadingDirs = true;
	var f = dirStart.dir.getAttribute("address") + ", " + dirStart.dir.getAttribute("state");
	var t = dirEnd.dir.getAttribute("address") + ", " + dirEnd.dir.getAttribute("state");
	var r = "from: " + f + " to: " + t;
	document.getElementById("div_status").innerHTML = "Loading directions FROM <b>" + f + "</b> TO <b>" + t + "</b>";
	gdirections.loadFromWaypoints([dirStart.getLatLng(), dirEnd.getLatLng()]);
}

function showMarkers() {
	var b = new GLatLngBounds();
	if(dirStart)
		b.extend(dirStart.getLatLng());
	if(dirEnd)
		b.extend(dirEnd.getLatLng());
	
	map.setCenter(b.getCenter(), map.getBoundsZoomLevel(b));
}

function addStartMarker(latlng, directionNode) {
	dirStart = new GMarker(latlng);
	dirStart.dir = directionNode;
	map.addOverlay(dirStart);
}
function addEndMarker(latlng, directionNode) {
	dirEnd = new GMarker(latlng);
	dirEnd.dir = directionNode;
	map.addOverlay(dirEnd);
}
function getLatLng(directionNode) {
	var lat = parseFloat(directionNode.getAttribute("lat"));
	var lng = parseFloat(directionNode.getAttribute("lng"));
	
	return new GLatLng(lat, lng);
}

function enableDir(el) {
	el.style.color = "#fff";
	el.style.cursor = "pointer";
}
function disableDir(el) {
	el.style.color = "#626262";
	el.style.cursor = "default";
}

function onDirectionClick(el, directionNode) {
	if(loadingDirs) return;
	
	disableDir(el);
	
	if(!dirStart)
		addStartDirection(el, directionNode);
	else if(!dirEnd) {
		addEndDirection(el, directionNode);
		loadingDirs = true;
		loadDirections();
	}
}

function buildList() {
	var el = document.getElementById("directions_canvas");
	var i, newEl;
	
	for(i=0;i<xmlDirections.length;i++) {
		newEl = getDirectionElement(xmlDirections[i]);
		el.appendChild(newEl);
	}
}

function getDirectionContent(directionNode) {
	var data = [];
	
	data.push(directionNode.getAttribute("title"));
	data.push(directionNode.getAttribute("address"));
	data.push(directionNode.getAttribute("state"));
	data.push(directionNode.getAttribute("tel"));
	
	return data.join("<br/>");
}
function getDirectionElement(directionNode) {
	var dir = document.createElement("dir");
	dir.id = "dir_" + directionNode.getAttribute("id");
	dir.style.cursor = "pointer";
	dir.innerHTML = getDirectionContent(directionNode);
	
	GEvent.addDomListener(dir, "click", function() {
		if(dir.style.cursor == "pointer")
			onDirectionClick(dir, directionNode);
	});
	
	return dir;
}

function loadXML() {
	GDownloadUrl("directions.xml", function(data, responseCode) {
		var xml = GXml.parse(data);
		xmlDirections = xml.documentElement.getElementsByTagName("dir");
		buildList();
	});
}

function resetAll() {
	map.clearOverlays();
	document.getElementById("steps_canvas").innerHTML = document.getElementById("div_status").innerHTML = "";
	resetDirs();
	loadingDirs = false;
}

function resetDirs() {
	var els = document.getElementById("directions_canvas").childNodes;
	var i;
	
	dirStart = dirEnd = null;
	
	for(i=0;i<els.length;i++)
		enableDir(els[i]);
}

function initializeMap() {
	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(39.9516, -75.1638), 7);
	map.setUIToDefault();
	
	gdirections = new GDirections(map, document.getElementById("steps_canvas"));
	GEvent.addListener(gdirections, "addoverlay", onDirectionsLoaded);
	GEvent.addListener(gdirections, "error", onDirectionsError);

	loadXML();
	
	document.getElementById("gmap_reset_btn").style.visibility = "visible";
}

function unloadGMap() {
	GUnload();
}

window.onload = initializeMap;
window.onunload = unloadGMap;