// initialize the page on document ready
$(document).ready(function() {
	$.ajaxSetup({cache:false});
	gmapInit();
	addOutagePoints();
});

// initialize the map, set options
var map;
function gmapInit() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(39.80959097923673,  -86.15684509277344), 11);
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GLargeMapControl());
		map.enableScrollWheelZoom();
	}
}

//a little function to check if the var exists and is not empty
function isDefined(variable){
	return (!(!( variable||false )))
}

// parse the incoming xml, add markers to the map
function addOutagePoints () {
	
	var baseIcon = new GIcon();
	baseIcon.shadow = "./images/shadow50.png";
	baseIcon.iconSize = new GSize(15, 23);
	baseIcon.shadowSize = new GSize(30, 23); //shadow size should be twice the width and exactly the height
	baseIcon.iconAnchor = new GPoint(7, 23); //icon anchor should be 
	baseIcon.infoWindowAnchor = new GPoint(7, 23);
	baseIcon.infoShadowAnchor = new GPoint(14, 25);

	// set up out alternate icon objects
	var yellowicon 	= new GIcon(baseIcon,"./images/markeryellow.png");
	var redicon 	= new GIcon(baseIcon,"./images/markerred.png");
	var blueicon 	= new GIcon(baseIcon,"./images/markerblue.png");
	var greenicon 	= new GIcon(baseIcon,"./images/markergreen.png");
	//var orangeicon 	= new GIcon(baseIcon,"./images/markerorange.png");
	
	//alert(G_DEFAULT_ICON.infoWindowAnchor);

	var TotalOut = 0;
	
	//Overide Zoom event on map to stop zoom levels max is 15 min is 10
	GEvent.addListener(map, "zoomend", function(oldZoomLevel, newZoomLevel) {
		if(newZoomLevel <= 10){map.setZoom(10);} 
		if(newZoomLevel >= 15){map.setZoom(15);} 
	}); 

	
	$(function() {
	    $.ajax({
	        type: "GET",
	        url: "outages.xml",
	        dataType: "xml",
	        success: function(xml) {

//var polyline1 = new GPolyline([new GLatLng(39.9500, -86.2200), new GLatLng(39.6600, -86.2200)], "blue", 5, .20);
//var polyline2 = new GPolyline([new GLatLng(39.9500, -86.1000), new GLatLng(39.6600, -86.1000)], "blue", 5, .20);
//var polyline3 = new GPolyline([new GLatLng(39.8500, -86.3200), new GLatLng(39.8500, -86.0000)], "blue", 5, .20);
//var polyline4 = new GPolyline([new GLatLng(39.7600, -86.3200), new GLatLng(39.7600, -86.0000)], "blue", 5, .20);
//map.addOverlay(polyline1);
//map.addOverlay(polyline2);
//map.addOverlay(polyline3);
//map.addOverlay(polyline4);

	        	// iterate through each marker node
				$(xml).find('marker').each(function()
				{
					
					// grab the properties we'll work with below
					var llat	= Number($(this).attr('lat'));
					var llong	= Number($(this).attr('long'));
					var totalCust	= $(this).attr('totalcust');
					var totalInc	= $(this).attr('totalinc');
					var strInc	= 'incidents'
					if (totalInc == 1){strInc = 'incident'} 
					var areaname	= $(this).attr('areaname');
					
					

					// build the html string that goes into the marker's info window
					var infoWindowFragment = [];
					infoWindowFragment.push("<div class='outageInfoWindow'>");
					infoWindowFragment.push("<h2>Outage Area: " + areaname + " </h2>");
					infoWindowFragment.push("<p>" + totalCust + " customers are affected by outages<br> in this area. (" + totalInc + " " + strInc +")</p>");
					infoWindowFragment.push("</div>");
					var infoWindowFragment = infoWindowFragment.join("");

					// create the marker object, switch color based on severity, add the info window listener

					// set up an alternate icon if this point warrants
					var alternateIcon;
					var polygonColor;
					var polygonOpacity;
					var polygonBorderColor = 'black';
					var polygonBorderWidth = 1;
					var polygonBorderOpacity = '.40';
					

					if (totalCust <= 250) { alternateIcon = greenicon; polygonColor = 'green'; polygonOpacity = '.15'}
					else if (totalCust > 250 && totalCust <= 1000) { alternateIcon = blueicon;  polygonColor = 'blue'; polygonOpacity = '.15'}
					else if (totalCust > 1000 && totalCust <= 2500) { alternateIcon = yellowicon;  polygonColor = 'yellow'; polygonOpacity = '.20'}
					//else if (totalCust > 1000 && totalCust < 1001) { alternateIcon = orangeicon; }
					else if (totalCust > 2500) { alternateIcon = redicon;  polygonColor = 'red'; polygonOpacity = '.15'}

					if (areaname == 'North West')
					{
						var polygon0 = new GPolygon([new GLatLng(39.9400, -86.3400), new GLatLng(39.9400, -86.2200), new GLatLng(39.8500, -86.2200), new GLatLng(39.8500, -86.3400), new GLatLng(39.9400, -86.3400)], 
						polygonBorderColor, polygonBorderWidth, .5, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon0);
						
					}
					else if (areaname == 'North')
					{
						var polygon1 = new GPolygon([new GLatLng(39.9400, -86.2200), new GLatLng(39.9400, -86.1000), new GLatLng(39.8500, -86.1000), new GLatLng(39.8500, -86.2200), new GLatLng(39.9400, -86.2200)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon1);
					}
					else if (areaname == 'North East')
					{
						var polygon2 = new GPolygon([new GLatLng(39.9400, -86.1000), new GLatLng(39.9400, -85.9800), new GLatLng(39.8500, -85.9800), new GLatLng(39.8500, -86.1000), new GLatLng(39.9400, -86.1000)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon2);
					}
					else if (areaname == 'West')
					{
						var polygon3 = new GPolygon([new GLatLng(39.8500, -86.3400), new GLatLng(39.8500, -86.2200), new GLatLng(39.7600, -86.2200), new GLatLng(39.7600, -86.3400), new GLatLng(39.8500, -86.3400)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon3);					
					}
					else if (areaname == 'Central')
					{
						var polygon4 = new GPolygon([new GLatLng(39.8500, -86.2200), new GLatLng(39.8500, -86.1000), new GLatLng(39.7600, -86.1000), new GLatLng(39.7600, -86.2200), new GLatLng(39.8500, -86.2200)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon4);
					}
					else if (areaname == 'East')
					{
						var polygon5 = new GPolygon([new GLatLng(39.8500, -86.1000), new GLatLng(39.8500, -85.9800), new GLatLng(39.7600, -85.9800), new GLatLng(39.7600, -86.1000), new GLatLng(39.8500, -86.1000)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon5);					
					}
					else if (areaname == 'South West')
					{
						var polygon6 = new GPolygon([new GLatLng(39.7600, -86.3400), new GLatLng(39.7600, -86.2200), new GLatLng(39.6700, -86.2200), new GLatLng(39.6700, -86.3400), new GLatLng(39.7600, -86.3400)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon6);					
					}
					else if (areaname == 'South')
					{
						var polygon7 = new GPolygon([new GLatLng(39.7600, -86.2200), new GLatLng(39.7600, -86.1000), new GLatLng(39.6700, -86.1000), new GLatLng(39.6700, -86.2200), new GLatLng(39.7600, -86.2200)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon7);					
					}
					else if (areaname == 'South East')
					{
						var polygon8 = new GPolygon([new GLatLng(39.7600, -86.1000), new GLatLng(39.7600, -85.9800), new GLatLng(39.6700, -85.9800), new GLatLng(39.6700, -86.1000), new GLatLng(39.7600, -86.1000)], 
						polygonBorderColor, polygonBorderWidth, polygonBorderOpacity, polygonColor,polygonOpacity) ;
						map.addOverlay(polygon8);
					}					

					// if we set up an alternate above, then use it, if not
					// then declare the marker without a custom icon
					var marker;
					if (alternateIcon != "") { marker = new GMarker(new GLatLng(llat, llong), {clickable:true, icon: alternateIcon}); }
					else { marker = new GMarker(new GLatLng(llat, llong), {clickable:true}); }

					// add the click listener
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindow(infoWindowFragment);
					});

					// add the marker to the map
					map.addOverlay(marker);
				});
			}
		});
	});


}
