// Copyright (c) GeoWise, 2010. http://www.instantatlas.com
function selectReport(list) 
{
	var fid = list.options[list.selectedIndex].value;
	if (fid != "") window.location.href = "report_" + fid + ".html";
}

function initMap() 
{
	// Google map
	map.enableScrollWheelZoom();
	map.addControl(new GSmallZoomControl());
	
	addOverlays(g);
	zoomToBBox(bbox);
}

function addListeners() 
{
	GEvent.addListener(map, "click", function(overlay, latlng)
	{
		window.location.href = "report_" + overlay.id + ".html";
	});

	GEvent.addListener(map, 'mouseover', function(overlay, latlng)
	{

	});
	
	GEvent.addListener(map, 'mouseout', function(overlay, latlng) 
	{

	});
}

function addOverlays(gArray) 
{
	for (var i = 0; i < gArray.length; i++) 
	{
		// Handle multi polygon.
		if(gArray[i].coords instanceof Array) 
		{
			for (var j = 0; j < gArray[i].coords.length; j++) 
			{
				var gPolygon = getGPolygon(gArray[i].coords[j]);
				gPolygon.id = gArray[i].id;
				map.addOverlay(gPolygon);
				updateBBox(gPolygon);
			}
		}
		// Handle single polygon.
		else
		{
			var gPolygon = getGPolygon(gArray[i].coords);
			gPolygon.id = gArray[i].id;
			map.addOverlay(gPolygon);
			updateBBox(gPolygon);
		}		
	}
};

var bbox = null;
function updateBBox(gPolygon) 
{
	if (bbox == null) 
	{
		bbox = gPolygon.getBounds();
	}
	else 
	{
		bbox.extend(gPolygon.getBounds().getNorthEast());
		bbox.extend(gPolygon.getBounds().getSouthWest());
	}
}

function getGPolygon(coordArray)
{
	var coords = coordArray.split(" ");
	var latLngArray = new Array(coords.length);

	for (var k = 0; k < coords.length; k++) 
	{
		var latLng = coords[k].split(',');
		latLngArray[k] = new GLatLng(parseFloat(latLng[1]), parseFloat(latLng[0]));
	}
	
	return new GPolygon(latLngArray, strokeColour, strokeThickness, 1, fillColour, fillAlpha);
};

function zoomToBBox(bBox) 
{
	// Position map.
	map.setCenter(bBox.getCenter(), map.getBoundsZoomLevel(bBox));
}
