var map = null;
var biaAjax = new ajaxComm("bia.ajax.php");
var estMarkers = null;

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"}];

$(document).ready(function(){

	$("#participants").click(function(){
		$("#participantBox").show();
		$("#dialogBox").show();
		mapsLoaded();
		map.checkResize();
	});

	$("#closeMap").click(function(){
		$("#participantBox").hide();
		$("#dialogBox").hide();
	});
	
	$("#listCategory").change(function(){
		loadCategory($(this).val());
	});
	
	$(window).resize(resizeMap);
	resizeMap();
});

function loadCategory(catId) {
	$("#loading").show();
	biaAjax.postSynchronous({ "action" : "loadCategory", "catId"	: catId }, function(data) {
		if (data == false) {
			$("#loading").hide();
			return;
		}
		
		$("#theList").html(data.html);

		addMarkers(data.info, createIcon("images/gpIcon.png", [24, 38], [10, 36]));
		$("#theList .biaEst").click(showMarker);
		$("#loading").hide();
	})
}

function showMarker() {
	var i = this.id.replace("est_", "");
	GEvent.trigger(estMarkers[i], "click");
}

function mapsLoaded() {
	if (map != null) return;
	map = new GMap2(document.getElementById("gMap"));
	map.setCenter(new GLatLng(43.6788643, -79.3451472), 16);
	map.addControl(new GSmallMapControl());

	var bListBox = $("#listBox").get(0);
	var pos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	pos.apply(bListBox);
	map.getContainer().appendChild(bListBox);
	$("#listBox").show();

	map.checkResize();
	
	loadCategory($("#listCategory").val());
}

function resizeMap() {
	var wHeight = $(window).height();
	var wWidth = $(window).width();
	
	$("#dialogBox").css({
		"width"	: (wWidth - 100) + "px",
		"height"	: (wHeight - 100) + "px",
		"top"		: "50px",
		"left"	: "50px",
	});
	$("#gMap").height(wHeight - 170);
	$("#listBox").height(wHeight - 170 - 70);
	$("#listContents").height(wHeight - 170 - 152);
}

function addMarkers(markers, groupid, icon) {
	map.clearOverlays();
	estMarkers = [];
	for (i in markers) {
		var marker = createMarker(map, markers[i], icon);
		estMarkers.push(marker);
		map.addOverlay(marker);
	}
};


function createIcon(url, size, anchor, showShadow){
	if (typeof showShadow == "undefined") showShadow = true;
	var gIcon = new GIcon();
	gIcon.image = url;
	gIcon.iconSize = new GSize(size[0], size[1]);
	gIcon.iconAnchor = new GPoint(anchor[0], anchor[1]);
	if (showShadow) {
		gIcon.shadow = "images/gpshadow.png";
		gIcon.shadowSize = new GSize(37, 34);
	}
	gIcon.infoShadowAnchor = new GPoint(size[0]/2, size[1]/2);
	gIcon.infoWindowAnchor = new GPoint(size[0]/2, size[1]/2);
	return gIcon;
};

function createMarker(gMap, marker, gIcon) {
	// Set the marker options
	var hoverInfo = typeof marker.hoverText != "undefined";
	var clickInfo = typeof marker.infoText != "undefined";
	
	var options = { clickable : (hoverInfo || clickInfo) };
	if (typeof gIcon != "undefined") {
		options.icon = gIcon;
	}
	
	var latLon = new GLatLng(marker.lat, marker.lon);
	var gMarker = new GMarker(latLon, options);
	
	if (hoverInfo) {
		var tooltip = "<div>"+marker.hoverText+"</div>";
		gMarker.tooltip = new Tooltip(gMarker, tooltip, 0);
		gMarker.tooltip.initialize(map);

		// Bind hover events
		GEvent.addListener(gMarker, 'mouseover', tipHover);
		GEvent.addListener(gMarker, 'mouseout', tipUnHover);
	}
	if (typeof marker.infoText != "undefined") {
		gMarker.infoText = marker.infoText;
		// Bind click events
		GEvent.addListener(gMarker, 'click', markerClick);
	}
	
	return gMarker;
};

function tipHover() { this.tooltip.show(); this.tooltip.redraw(true); }
function tipUnHover() { this.tooltip.hide(); }
function markerClick() { this.openInfoWindowHtml(this.infoText); }
