var greenP = [{lat:43.679483,lon:-79.345237,hoverText:"<b>716 Pape Avenue</b><br/>Type: Garage"},{lat:43.677858,lon:-79.344637,hoverText:"<b>670 Pape Avenue</b><br/>Type: Garage"},{lat:43.677318,lon:-79.357954,hoverText:"<b>35 Erindale Avenue</b><br/>Type: Garage"},{lat:43.678243,lon:-79.351078,hoverText:"<b>14 Arundel Avenue</b><br/>Type: Garage"},{lat:43.67908,lon:-79.348419,hoverText:"<b>25 Ferrier Avenue</b><br/>Type: Garage"},{lat:43.680039,lon:-79.343528,hoverText:"<b>20 Eaton Avenue</b><br/>Type: Garage"},{lat:43.68006,lon:-79.343151,hoverText:"<b>17 Eaton Avenue</b><br/>Type: Garage"},{lat:43.679624,lon:-79.346243,hoverText:"<b>77 Gough Avenue</b><br/>Type: Garage"},{lat:43.680569,lon:-79.340686,hoverText:"<b>31 Langford Avenue</b><br/>Type: Garage"},{lat:43.680094,lon:-79.342158,hoverText:"<b>12 Woodycrest Avenue</b><br/>Type: Garage"},{lat:43.678818,lon:-79.348709,hoverText:"<b>18 Ferrier Avenue</b><br/>Type: Garage"}];
var ttc = [{ lat:43.676237, lon: -79.358851, hoverText : "Broadview Subway Station"},{ lat:43.677560, lon: -79.351957, hoverText : "Chester Subway Station"},{ lat:43.678934, lon: -79.344882, hoverText : "Pape Subway Station"},{ lat:43.680443, lon: -79.337275, hoverText : "Donlands Subway Station"}];
var danforthMarker = { lat:43.677983, lon: -79.349720, hoverText : "Taste of the Danforth" };

$(document).ready(function(){
	$("#map").dtoMap(danforthMarker, 15);

});

function showParticipants() {
	toggleCover(true);
	$("#listFrame").show();
	$("#listMap").dtoMapRefresh();
	$("#listMap").dtoMapCenter(1);
}

function mapDirections(response) {
	if (!response || response.Status.code != 200) {
		alert("Can't find your address.");
	} else {
		if (response.Placemark.length > 0) {
			// Display the extended map with directions
			var place = response.Placemark[0];
			displayModalMap(place.address, place.Point.coordinates[1], place.Point.coordinates[0]);
		}
	}
}

function toggleCover(show) {
	if (show) {
		$("#modalCover").show();
	} else {
		$("#modalCover").hide();
	}
}

function displayModalMap(source, lat, lon) {
	toggleCover(true);
	var width = ($(window).width() - 100);
	var height = ($(window).height() - 100);
	$("#dirHeading").html("Direction from <b>"+source+"</b> to the <b>Taste of the Danforth</b>");
	$("#dirFrame").show()
		.width(width)
		.height(height);
	$("#dirMap").dtoMapRefresh();
	$("#dirText").html("");
	$("#dirText").width(width-$("#dirMap").width()-30);
	var dirMap = $("#dirMap").dtoMapGetgMap();
	dirMap.clearOverlays();
	var dirObj = new GDirections(dirMap, $("#dirText").get(0));

	GEvent.addListener(dirObj, "error", handleErrors);
	
	var query = source+" @"+ lat +", "+ lon + " to Taste of the Danforth @"+ danforthMarker.lat + ", " + danforthMarker.lon;
	dirObj.load(query);
}

function markParticipant() {
	$this = $(this);
	var markerId = $this.attr("id").replace("mark", "");
	var marker = $("#listMap").dtoMapGetMarker(1,markerId);
	GEvent.trigger(marker, "click");
}

function handleErrors(gdir) {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("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.\nError code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("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.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
		alert("There is no route available between the two points, or no data for routing in that region.\n Error code: " + gdir.getStatus().code);
	else alert("An unknown error occurred.");
}











