//	event.js
//
//	Google Maps event script.
//	Rev. 8/31/2009.

var geo;
var map;
var ply;
var draw = false;


//	preset UA window.

window.onload = load;
window.onunload = GUnload;


//	load()
//
//	create oak park map after page load complete.

function load() {
	if (!GBrowserIsCompatible()) {
		alert("Incompatible browser.");
		return false;
	}

//	create map.

	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(41.886, -87.795), 14);
	map.addControl(new GSmallMapControl());
	map.disableDoubleClickZoom();
	GEvent.addListener(map, "click", mpck);
	document.getElementById("map").appendChild(document.getElementById("ctrl"));

//	create polyline.

	aply();
	dist();

//	create geocoder.

	geo = new GClientGeocoder();
}


//	map event section.


//	aply()
//
//	create polyline and overlay it onto the map.

function aply() {
	debug("aply");
	ply = new GPolyline([]);
	setTimeout("ply.enableDrawing()",0);
	draw = true;
	map.addOverlay(ply);
	GEvent.addListener(ply, "lineupdated", linu);
	GEvent.addListener(ply, "endline", eold);
	GEvent.addListener(ply, "cancelline", cnld);
	GEvent.addListener(ply, "mouseover", movr);
	GEvent.addListener(ply, "mouseout", mout);
}


//	amrk()
//
//	add markers over polyline vertices.

var vtx = [];

function amrk() {
	debug("amrk-"+ply.getVertexCount()+"-"+vtx.length);
	rmrk();
	for (var i=0; i<ply.getVertexCount(); i++) {
		vtx[i] = ply.getVertex(i);
		vtx[i].marker = new GMarker(vtx[i]);
		map.addOverlay(vtx[i].marker);
	}
}


//	cnld()
//
//	event "cancel line drawing".


function cnld() {
	debug("cnld-"+ply.getVertexCount()+"-"+vtx.length);
	rmrk();
	map.removeOverlay(ply);
	aply();
	dist();
}


//	debug(st)
//
//	send string to a debug window.

function debug(st) {
	document.getElementById("event").innerHTML = st+"\n"+document.getElementById("event").innerHTML;
}


//	dist()
//
//	display polyline distance and map mode.

function dist() {
	debug("dist");
	document.getElementById("dist").innerHTML = (ply.getLength()*0.0006213).toFixed(1) + " mi";
	document.getElementById("mode").innerHTML = draw? "Drawing": "Editing";
	document.getElementById("help").innerHTML = draw? "Click to create pedicab stops. Double-click the last one.": "Drag the stops around. Click the map to create more.";
}


//	eold()
//
//	event "end of line drawing".


function eold() {
	debug("eold-"+ply.getVertexCount()+"-"+vtx.length);
	draw = false;
	dist();
	amrk();
}


//	linu()
//
//	event "line updated".

function linu() {
	debug("linu-"+ply.getVertexCount());
	dist();
	amrk();
}


//	mout(pt)
//
//	event "mouse out" on polyline.

function mout(pt) {
	debug("mout");
	if (!draw) {
		setTimeout("ply.disableEditing()",0);
		amrk();
	}
}


//	movr(pt)
//
//	event "mouse over" on polyline.

function movr(pt) {
	debug("movr");
	if (!draw) {
		setTimeout("ply.enableEditing()",0);
		rmrk();
	}
}


//	mpck(ov, pt)
//
//	event "click" on map.

function mpck(ov, pt) {
	if (ov) debug("mpck-ov");
	else {
		debug("mpck-pt");
		if (!draw) {
			setTimeout("ply.enableDrawing()",0);
			draw = true;
			dist();
			rmrk();
		}
	}
}


//	rmrk()
//
//	remove markers over polyline vertices.

function rmrk() {
	debug("rmrk-"+ply.getVertexCount()+"-"+vtx.length);
	for (var i=0; i<vtx.length; i++) map.removeOverlay(vtx[i].marker);
	vtx = [];
}


//	DOM section.


//	clearmap()
//
//	button "Clear".

function clearmap() {
	debug("clearmap");
	rmrk();
	map.removeOverlay(ply);
	aply();
	dist();
	document.getElementById("event").innerHTML = "";
	return true;
}


//	markmap()
//
//	button "Add" to add markers at street address or route waypoints.
//	route looks like: "Route: lat,lng lat,lng ...".

function markmap() {
	var rt = document.getElementById("loc").value.split(" ");
	ply.disableEditing();
	draw = false;

//	if text begins with Route: then it's a list of waypoints.

	if (rt[0] == "Route:") {
		debug("markroute");
		var pt = false;
		for (var i=1; i<rt.length; i++) {
			if (rt[i]) {
				var wp = rt[i].split(",");
				pt = new GLatLng(parseFloat(wp[0], 10), parseFloat(wp[1], 10));
				ply.insertVertex(ply.getVertexCount(), pt);
			}
		}
		if (pt) document.getElementById("loc").value = "";
	}

//	otherwise, assume it's a street address.

	else {
		debug("markaddr");
		geo.getLatLng(document.getElementById("loc").value+",oak park,il", function (pt) {
			if (pt) {
				ply.insertVertex(ply.getVertexCount(), pt);
				document.getElementById("loc").value = "";
			}
		});
	}
	return true;
}


//	sendmap()
//
//	button "Send".

function sendmap() {
	debug("sendmap");
	var str = "Rick, please quote the custom tour shown below:\n\nRoute:";
	for (var i=0; i<vtx.length; i++) str += " "+vtx[i].lat()+","+vtx[i].lng();
	document.forms.post.comment.value = str;
	document.forms.post.submit();
	return true;
}

