// JQUERY
$(function(){

// OUTPUT JSON


function loadCommSites()
{
	$.getJSON("Data/GetCommSites.cfm",  function(json){
		CommSiteJSON = json;
	});
}

/*function loadSeabirdData(){
	$.getJSON("Data/GetSeabirdData.cfm", {SeabirdSpeciesID: $("#SeabirdSpeciesID").val() }, function(json){
		seabirdJSON  = json;
	}); 
}*/



// LOAD MAP
loadCommSites();
load('testMap');

showMapData();

$.getJSON("Data/GetCommSiteBounds.cfm", function(json){
	var minLat = json["DATA"]["MINLAT"][0];
	var maxLat = json["DATA"]["MAXLAT"][0];
	var minLng = json["DATA"]["MINLNG"][0];
	var maxLng = json["DATA"]["MAXLNG"][0];
	mapCenter = new GLatLng( (minLat + maxLat)/2, (minLng + maxLng)/2);
	mapLatLngBounds = new GLatLngBounds( new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng) );
	map.setCenter(mapCenter, map.getBoundsZoomLevel( mapLatLngBounds) );		
});







$(document).bind("unload", GUnload);
$(window).bind("resize", setMapSize);


// CONTEXT MENU
/*GEvent.addListener(map, "singlerightclick", showContextMenu);
$("#contextMenu").bind("mouseout", hideContextMenu);
$("#contextMenu").css("position","absolute");
$("#contextMenu").css("display","none");
$("#contextMenuCommand1").bind("click", menuCommand1);
$("#contextMenuCommand2").bind("click", menuCommand2);
$("#contextMenu li").bind("mouseout",contextMenuNoBubble);*/



/*function setMapSize(){
	var browserWidth = $(document).width();
	$("#testMap").css("width", browserWidth - 250);
	//$("#loadingMap").css("width", $("#testMap").css("width") );
	
	$("div#loadingMap")
	.css({
		 position: "relative"
		,top: $("#testMap").offset().top + ( $("#testMap").height()/2 - $("#loadingMap").height()/2)
		,left: $("#testMap").offset().left + ($("#testMap").width()/2 - $("#loadingMap").width()/2)	
	})
	
	map.checkResize();
}*/

function menuCommand1(){
	alert('I\'m command 1');
	hideContextMenu();
}

function menuCommand2(){
	alert('I\'m command 2');
	hideContextMenu();
}

function showInfoWindow(){
	var infoHTML = '<div id="infoWindowDiv" class="nomargin">';

	// show site data
	for (var i = 0; i < CommSiteJSON.ROWCOUNT - 1; i++)
	{
		if ( CommSiteJSON.DATA.COMMUNITYID[i]  == this.CommunityID )
		{

			infoHTML = infoHTML + '<h3>';
			infoHTML = infoHTML + CommSiteJSON.DATA.COMMUNITY[i];
			//infoHTML = infoHTML + ' [' + this.SeabirdSiteID + ']'; //show ID, take out later
			infoHTML = infoHTML + '</h3>';
			/*infoHTML = infoHTML + '<p>Region: ' + CommSiteJSON.DATA.REGION[i] + '</p>';*/
		
			infoHTML = infoHTML + '<p><strong>Coordinates:</strong> ';
			infoHTML = infoHTML + CommSiteJSON.DATA.LAT[i];
			infoHTML = infoHTML + ', ';
			infoHTML = infoHTML + CommSiteJSON.DATA.LNG[i];
			infoHTML = infoHTML + '</p>';
			infoHTML = infoHTML + '<p><strong>Population:</strong> ';
			infoHTML = infoHTML + CommSiteJSON.DATA.POP[i];
			infoHTML = infoHTML + '</p>';
			
			
		}
	}

	/*infoHTML = infoHTML + '<h4 class="nomargin">Observations:</h4>';
	infoHTML = infoHTML + '<table>';
	infoHTML = infoHTML + '<tr>';
	infoHTML = infoHTML + '<th>Date</th>';	
	infoHTML = infoHTML + '<th>Species</th>';
	infoHTML = infoHTML + '<th>Population</th>';
	infoHTML = infoHTML + '<th>Data Quality</th>';
	infoHTML = infoHTML + '</tr>';	
				
	// show observation data
	for (var i = 0; i < seabirdJSON.ROWCOUNT - 1; i++)
	{
		if ( seabirdJSON.DATA.SEABIRDSITEID[i] == this.SeabirdSiteID )
		{
			var obsDate = new Date( seabirdJSON.DATA.OBSERVATIONDATE[i] );
			infoHTML = infoHTML + '<tr>';
			infoHTML = infoHTML + '<td>' + (obsDate.getMonth() + 1) + '-' + obsDate.getDate() + '-' + obsDate.getFullYear() + '</td>';
			infoHTML = infoHTML + '<td>' + seabirdJSON.DATA.SEABIRDSPECIES[i] + '</td>';
			infoHTML = infoHTML + '<td>' + seabirdJSON.DATA.POPULATION[i] + '</td>';
			infoHTML = infoHTML + '<td>' + seabirdJSON.DATA.DATAQUALITY[i] + '</td>';
			infoHTML = infoHTML + '</tr>';	
		}
	}

	infoHTML = infoHTML + '</table>';*/
	infoHTML = infoHTML + '</div>';
	
	this.openInfoWindowHtml(infoHTML);
}







// CONTEXT MENU BEHAVIORS
/*function showContextMenu(point, src, overlay)
{
	$("#contextMenu").css("left", point.x);
	$("#contextMenu").css("top", point.y);
	$("#contextMenu").slideDown();
}

function hideContextMenu(event)
{		
	$("#contextMenu").slideUp();
}

function contextMenuNoBubble(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}*/


function showMapData()
{
	$("#loadingMap").fadeIn(1000, function(){
		map.clearOverlays();
	
		// CUSTOM ICON
		var testIcon = new GIcon();
		testIcon.image = "makeIcon.cfm?size=11&opacity=80&color=yellow&fakey=.png";
		testIcon.shadow = "";
		testIcon.iconSize = new GSize(11, 11);
		testIcon.shadowSize = new GSize(0, 0);
		testIcon.iconAnchor = new GPoint(5, 5);
		testIcon.infoWindowAnchor = new GPoint(10, 0);
		
		// MARKER OPTIONS
		var mkOpt = {
			icon: testIcon	
		}
			
		var markerLat;
		var markerLng;
		
		// SHOW SITES
		
		
		
		
		for (var i = 0; i < CommSiteJSON.ROWCOUNT - 1; i++)
		{
			markerLat = CommSiteJSON.DATA.LAT[i];
			markerLng = CommSiteJSON.DATA.LNG[i];
			
			CommMarker = new GMarker( new GLatLng( markerLat, markerLng), mkOpt);
			CommMarker.CommunityID = CommSiteJSON.DATA.COMMUNITYID[i];
			GEvent.addListener(CommMarker, "click", showInfoWindow);
		
			map.addOverlay(CommMarker);
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		$("#loadingMap").fadeOut(1000);	
	});

}

});