var map;
var directionsPanel;
var directions;
var updateTarget;

function PopulateAddress() {
	document.frmPlanner.endadd.value = document.frmPlanner.startadd.value;
	document.frmPlanner.endcity.value = document.frmPlanner.startcity.value;
	document.frmPlanner.endstate.value = document.frmPlanner.startstate.value;
}

function address(addr, city, state) {
	if (addr == "" || city == "" || state == "") return "";
	return addr + ", " + city + ", " + state;
}

function startAddress() {
	var addr = document.frmPlanner.startadd.value;
	var city = document.frmPlanner.startcity.value;
	var state = document.frmPlanner.startstate.value;
	return address(addr, city, state);
}

function endAddress() {
	var addr = document.frmPlanner.endadd.value;
	var city = document.frmPlanner.endcity.value;
	var state = document.frmPlanner.endstate.value;
	return address(addr, city, state);
}

function loadDirections(waypoints) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		directionsPanel = document.getElementById("dirs");
		
		map.addControl(new GSmallMapControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		map.clearOverlays();
		directionsPanel.innerHTML = "";
		directions = new GDirections(map, directionsPanel);

		var direrror = GEvent.addListener(directions, "error", function() {
			if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				$("#map").html("<div style=padding:5px;><span style=color:red><span style=font-size: 16px;><strong>ERROR:</strong></span><br />No corresponding geographic location could be found for one of the specified addresses.<br />This may be due to the fact that the address is relatively new, or it may be incorrect.<br />Please return to the previous page and try removing one of your addresses.</span></div>");
			else
				$("#map").html("An unknown error occured while loading the directions");
			GEvent.removeListener(direrror);
		});
		
		directions.loadFromWaypoints(waypoints);
		
		// After directions load, add commenting boxes and remove last marker
		var dirfin = GEvent.addListener(directions, "addoverlay", function() {
			$("td[@jscontent='address']:not(:first):not(:last)").append("<br/><a href=\"#\" class=\"openDialog\">Add Remarks</a><p class=\"property-remarks\"></p>");
			$(".openDialog").bind("click", function(e) {
				e.preventDefault();
				e.stopPropagation();
				e.cancelBubble = true;
				updateTarget = this;
				var txt = $(updateTarget).next("p.property-remarks").html();
				document.getElementById("dlgEditText").value = txt;
				$("#dlg").dialog("open");
				$("#dlgEditText").focus();
			});
			
			if (startAddress() != "" && startAddress() == endAddress())
				map.removeOverlay(directions.getMarker(directions.getNumRoutes())); //remove final marker
			
			GEvent.removeListener(dirfin);
		});
	}
}


$(document).ready(function(){
	document.onunload = "GUnload()";
	
	// Create the dialog box for editing remarks about the property
	$("#dlg").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false
	});
	
	$("#waypoint-list").sortable();

	
	// Save command writes the edit text to a property-remark div
	$("#dlgSave").bind("click", function(e) {
		e.preventDefault();
		var txt = $("#dlgEditText").val();
		$(updateTarget).next("p.property-remarks").html(txt);
		$("#dlg").dialog("close");
	});
	
	$("#dlgCancel").bind("click", function(e) {
		e.preventDefault();
		$("#dlg").dialog("close");
	});
	
	$("#GoButton").bind("click", function(e) {
		e.preventDefault();
		
		var waypoints = Array();
		if (startAddress() != "") waypoints.push(startAddress());
		$("input[name='latlngs']").each(function(i) {
			waypoints.push(this.value);
		});
		if (endAddress() != "") waypoints.push(endAddress());
		
		loadDirections(waypoints);
	});
});