/* ----------------------------------------------------------
title: Outage Map

version: 1.1
author: M. Grimm – Lockheed Martin Energy / Lockheed Martin Energy
created: 12/18/07
last updated: 02/07/2008

TODO: polygon boundries, error checking, empty data check

History:
vml definition for IE was added to the outage page
added google's unload method (to stop browser memory leaks)
01/31/2008 Dharadheer Prathipati – Lockheed Martin Energy - add rec territory 
02/07/2008 Dharadheer Prathipati – Lockheed Martin Energy - per REC Request Modified to show OutageCause only When Entered
02/11/2008 Dharadheer Prathipati – Lockheed Martin Energy - Added the Counties to zoom to

---------------------------------------------------------- */
// Global Constants and Variables
var max_zoom_level = 14;
var min_zoom_level = 8;
var county_min_zoom = 10;
var map_center_lat = 38.4145; 
var map_center_lng = -77.585; 
var map;
var overviewControl;
var outageFile = "/outagexml/outagexmldata.xml";
var reckml= "/outagexml/REC_territory_pg.kml";
var xhr; // XMLHttpRequest object
var xmlData;
var reloadXMLInterval; // holds the setInterval so we can clear it
var xmlRefreshTime = 60000; // 60000 = 1 min   300000 = 5min   420000 =7min 600000=10 min


// google icon objects
var markerLow;
var markerElevated;
var markerMedium;
var markerHigh;
var markerSevere;

//
var queryObj = {};


var legendHTML = '<ul id="outage-map-legend">' +
					'<li><small>Customers Affected</small></li>' +
					'<li id="low-outage">1-10</li>' +
					'<li id="elevated-outage">11-50</li>' +
					'<li id="medium-outage">51-100</li>' +
					'<li id="high-outage">101-500</li>' +
					'<li id="severe-outage">500+</li>' +
                    '<li id="rec-territory">REC Territory</li>' +
					'</ul>';
					
var noOutageHTML = '<div id="no-outage" >Currently No Outages</div>';


var recocuntiesHTML = '<div id="map-county"> ' +
                      '<label for="County">Counties Served by REC </label>' +
                      '<SELECT NAME="recCounty" id="ddlrecCounties" onChange="gotoRecCounties(this);" >' +
                      '<Option value="">REC Service Territory</option>' +
                      '<Option value="38.000141,-78.523720">Albemarle County</option>' +
                      '<Option value="38.014500,-77.355904">Caroline County</option>' +
                      '<Option value="39.120000,-77.990000">Clarke County</option>' +
                      '<Option value="38.503601,-77.925163">Culpeper County</option>' +
                      '<Option value="37.944939,-76.925194">Essex County</option>' +
                      '<Option value="38.711521,-77.830879">Fauquier County</option>' +
                      '<Option value="39.230000,-78.280000">Frederick County</option>' +
                      '<Option value="37.733929,-77.887878">Goochland County</option>' +
                      '<Option value="38.330139,-78.475159">Greene County</option>' +
                      '<Option value="37.773121,-77.452637">Hanover County</option>' +
                      '<Option value="37.706219,-76.916183">King & Queen County</option>' +
                      '<Option value="37.716400,-77.061363">King William County</option>' +
                      '<Option value="37.940681,-77.996979">Louisa County</option>' +
                      '<Option value="38.428650,-78.267822">Madison County</option>' +
                      '<Option value="38.253761,-78.036362">Orange County</option>' +
                      '<Option value="38.633500,-78.466400">Page County</option>' +
                      '<Option value="38.691818,-78.137161">Rappahannock County</option>' +
                      '<Option value="38.520000,-78.860000">Rockingham County</option>' +
                      '<Option value="38.850000,-78.580000">Shenandoah County</option>' +
                      '<Option value="38.185101,-77.660927">Spotsylvania County</option>' +
                      '<Option value="38.417110,-77.460800">Stafford County</option>' +
                      '<Option value="39.151771,-77.982219">Town of Berryville</option>' + //39.120000,-77.990000
                      '<Option value="38.049579,-77.346647">Town of Bowling Green</option>' + //38.051575,-77.347619
                      '<Option value="39.093162,-78.059443">Town of Boyce</option>' + //39.120000,-77.990000
                      '<Option value="38.473181,-77.996664">Town of Culpeper</option>' + //38.473330,-77.996139
                      '<Option value="38.918167,-78.194445">Town of Front Royal</option>' + //38.890000,-78.190000
                      '<Option value="38.380406,-78.257504">Town of Madison</option>' + //38.428650,-78.267822
                      '<Option value="39.027608,-78.280558">Town of Middletown</option>' + //39.230000,-78.280000
                      '<Option value="38.297352,-78.440010">Town of Stanardsville</option>' + //38.330139,-78.475159
                      '<Option value="39.083440,-78.218057">Town of Stephens City</option>' + //39.230000,-78.280000
                      '<Option value="38.713452,-78.159444">Town of Washington</option>' + //38.691818,-78.137161
                      '<Option value="38.890000,-78.190000">Warren County</option>' +
                      '</SELECT>' +
                     '</div>' ;


var footerHTML = '<div id="outage-footer" >' + 
				 '<strong>This information is updated about every ten minutes. It is for information purposes only and is based on the best available information.</strong>' + 
				 '</div>';;

// load current stable version of google maps
google.load("maps", "2");

	
function loadMap () {
	map = new google.maps.Map2(document.getElementById("outage-map"));
	map.setCenter(new google.maps.LatLng(map_center_lat, map_center_lng), min_zoom_level);
	map.addControl(new google.maps.SmallMapControl());        
	map.addControl(new google.maps.MapTypeControl());
    overviewControl = new google.maps.OverviewMapControl(new google.maps.Size( 100, 100 ));
	map.addControl(overviewControl);
	addTerrirtoy(); 
	setUpMarkerIcons();
	setZoomLevel();
	importXML();
	getQueryString(); // check param from Outage Summary
	// bind google's unload function to window unloaad event to prevent memory leaks
	$(window).unload( google.maps.Unload );
}


// zooms to Counties basing on the selection 
function gotoRecCounties(ddlCountySelect){
	var latlong = (ddlCountySelect.options[ddlCountySelect.selectedIndex].value);
	if (latlong != "") {
		var latlong_array = latlong.split(",");   
		map.setCenter(new GLatLng(latlong_array[0],latlong_array[1]), county_min_zoom);          
		if (overviewControl != null) {
			overviewControl.hide(true);
		}
	} else {
		map.setCenter(new google.maps.LatLng(map_center_lat, map_center_lng), min_zoom_level);
		if (overviewControl != null) {
			overviewControl.show();
		}
	}
}


function setZoomLevel () {
	google.maps.Event.addListener(map, 'zoomend', function(previousZoomLevel, newZoomLevel) { 	
		
		if (previousZoomLevel > newZoomLevel) {
			if (newZoomLevel >= min_zoom_level) { 
				   map.setZoom(newZoomLevel); 
			} else {
				map.zoomIn();
			}
		} else {
			if (newZoomLevel <= max_zoom_level){ 
				map.setZoom(newZoomLevel); 
			} else {
			 map.zoomOut();
			} 
		}			
	});
}




function  setUpMarkerIcons () {

	
	var lowOutageIcon = new google.maps.Icon();
	lowOutageIcon.image = "/outage-center/img/low-outage-icon.png"
	lowOutageIcon.iconSize = new google.maps.Size(25, 25);
	lowOutageIcon.shadow = "/outage-center/img/shadow.png";
	lowOutageIcon.shadowSize = new google.maps.Size(36, 25);
	lowOutageIcon.iconAnchor = new google.maps.Point(11, 14);
	lowOutageIcon.infoWindowAnchor = new google.maps.Point(11, 13);
    lowOutageIcon.transparent = "/outage-center/img/transparent.png";
    lowOutageIcon.printImage = "/outage-center/img/low-outage-print.gif";
    lowOutageIcon.mozPrintImage = "/outage-center/img/low-outage-print.gif";
    lowOutageIcon.printShadow = "/outage-center/img/shadow.gif";
	markerLow = { icon:lowOutageIcon };
	

	var elevatedOutageIcon = new google.maps.Icon();
	elevatedOutageIcon.image = "/outage-center/img/elevated-outage-icon.png"
	elevatedOutageIcon.iconSize = new google.maps.Size(25, 25);
	elevatedOutageIcon.shadow = "/outage-center/img/shadow.png";
	elevatedOutageIcon.shadowSize = new google.maps.Size(36, 25);
	elevatedOutageIcon.iconAnchor = new google.maps.Point(11, 14);
	elevatedOutageIcon.infoWindowAnchor = new google.maps.Point(11, 13);
    elevatedOutageIcon.transparent = "/outage-center/img/transparent.png";
    elevatedOutageIcon.printImage = "/outage-center/img/elevated-outage-print.gif";
    elevatedOutageIcon.mozPrintImage = "/outage-center/img/elevated-outage-print.gif";
    elevatedOutageIcon.printShadow = "/outage-center/img/shadow.gif";
	markerElevated = { icon:elevatedOutageIcon };
	
	
	var mediumOutageIcon = new google.maps.Icon();
	mediumOutageIcon.image = "/outage-center/img/medium-outage-icon.png"
	mediumOutageIcon.iconSize = new google.maps.Size(25, 25);
	mediumOutageIcon.shadow = "/outage-center/img/shadow.png";
	mediumOutageIcon.shadowSize = new google.maps.Size(36, 25);
	mediumOutageIcon.iconAnchor = new google.maps.Point(11, 14);
	mediumOutageIcon.infoWindowAnchor = new google.maps.Point(11, 13);
    mediumOutageIcon.transparent = "/outage-center/img/transparent.png";
    mediumOutageIcon.printImage = "/outage-center/img/medium-outage-print.gif";
    mediumOutageIcon.mozPrintImage = "/outage-center/img/medium-outage-print.gif";
    mediumOutageIcon.printShadow = "/outage-center/img/shadow.gif";
	markerMedium = { icon:mediumOutageIcon };


	var highOutageIcon = new google.maps.Icon();
	highOutageIcon.image = "/outage-center/img/high-outage-icon.png"
	highOutageIcon.iconSize = new google.maps.Size(25, 25);
	highOutageIcon.shadow = "/outage-center/img/shadow.png";
	highOutageIcon.shadowSize = new google.maps.Size(36, 25);
	highOutageIcon.iconAnchor = new google.maps.Point(11, 14);
	highOutageIcon.infoWindowAnchor = new google.maps.Point(11, 13);
    highOutageIcon.transparent = "/outage-center/img/transparent.png";
    highOutageIcon.printImage = "/outage-center/img/high-outage-print.gif";
    highOutageIcon.mozPrintImage = "/outage-center/img/high-outage-print.gif";
    highOutageIcon.printShadow = "/outage-center/img/shadow.gif";
	markerHigh = { icon:highOutageIcon };
	
	
	var severeOutageIcon = new google.maps.Icon();	
	severeOutageIcon.image = "/outage-center/img/severe-outage-icon.png"
	severeOutageIcon.iconSize = new google.maps.Size(25, 25);
	severeOutageIcon.shadow = "/outage-center/img/shadow.png";
	severeOutageIcon.shadowSize = new google.maps.Size(36, 25);
	severeOutageIcon.iconAnchor = new google.maps.Point(11, 14);
	severeOutageIcon.infoWindowAnchor = new google.maps.Point(11, 13);
    severeOutageIcon.transparent = "/outage-center/img/transparent.png";
    severeOutageIcon.printImage = "/outage-center/img/severe-outage-print.gif";
    severeOutageIcon.mozPrintImage = "/outage-center/img/severe-outage-print.gif";
    severeOutageIcon.printShadow = "/outage-center/img/shadow.gif";
	markerSevere = { icon:severeOutageIcon };
	
	}


// e.g. Last updated: July 29, 11:33 AM
function formatDateStr (iDate){
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var aDate = new Date(iDate);
	var MON = m_names[aDate.getMonth()];
	var DD = aDate.getDate();
	var hh = aDate.getHours();
	var	mm = aDate.getMinutes();
	var	ap = "AM";
	if (hh < 12) {ap = "AM";}
	else {ap = "PM";}
	if (hh == 0) {hh = 12; }
	if (hh > 12) { hh = hh - 12; }
	mm = mm + "";
	if (mm.length == 1) { mm = "0" + mm; }
	return (MON + ' ' + DD + ', ' + hh + ':' + mm + ' ' + ap);
}

function trimNumber(s) { 
	return s.replace(/^0+/, ''); 
} 

function importXML () {
    var date = new Date();
    var timestamp = date.getTime();        
	xhr =  google.maps.XmlHttp.create();
	if (xhr) {	
		xhr.onreadystatechange = function() {
			processResponse(); 
		};			
		xhr.open("GET", outageFile+"?time="+timestamp, true);  //ddp added timestamp to overcome browser caching 
        xhr.send(null);// using get request method so we do not pass any data in the send method
	}
}	

function processResponse () {
		if (xhr.readyState == 4) {
			if (xhr.status == 200 || xhr.status == 304) {			
				xmlData = xhr.responseXML;
				processXML();
			}
		}		
	}//end processResponse	
		
function processXML() {
	//	
	var markers = xmlData.getElementsByTagName("Markers");
	var outageLegend = $('#outage-map-legend');
	var outageNone = $('#no-outage');
    //add total customers affected count
    var totcustout = xmlData.getElementsByTagName("TotCustAffected");      
	//check if markers element exists in xml and there should be only one
	if (markers.length == 1) {
		if ($('#map-county').length >0) {
        	// do nothing
        } else {
			// hide county dropdown on 2010/05/31 --enabled ddp
            $('#outage-map').before(recocuntiesHTML);
        }	             
        if (outageNone.length > 0) {
       		// do nothing
        } else { 
            // no outages message
            $('#outage-map').before(noOutageHTML);
        }

        // some jquery to generate the legend
		if (outageLegend.length > 0) {
			// do nothing we already have a legend
		} else {
			$('#outage-map').before(legendHTML);
		}
		
        if (totcustout.length > 0) {
        	// check if element exists
            var totcustoutval = totcustout[0].firstChild.nodeValue;
            if (totcustoutval != "0") {
            	$('#no-outage')[0].innerHTML="Total Customer Outages:  <span style=\"color:#d11242;\">" + totcustoutval + "</span>";
            } else {
				$('#no-outage')[0].innerHTML="Currently No Outages";	
			}
        }
		
		
		//////////////////////////////////
		var xmlTS = new Date(xhr.getResponseHeader("Last-Modified")).toLocaleString();		
		$('#outage-footer').text('');
		footerHTML = '<div id="outage-footer" >' + 
					 '<strong>This information is updated about every ten minutes. It is for information purposes only and is based on the best available information.</strong>' + 
					 ' (Last updated: ' + formatDateStr(xmlTS) + ')' +
			 		 '</div>';
		$('#outage-map').after(footerHTML);
		//////////////////////////////////


		// Outage Marquee Message
		$("#outage-marquee").remove(); 
		var marquee = xmlData.getElementsByTagName("OutageMarquee");
		if (marquee.length == 1) {
			var isActive = xmlData.getElementsByTagName("Active")[0].firstChild.nodeValue;
			if (isActive == 'Y') {
				var omCreateDt = xmlData.getElementsByTagName("CreatedDT")[0].firstChild.nodeValue;		
				var omText1 = xmlData.getElementsByTagName("MarqueeText1")[0].firstChild.nodeValue;
				var omText2 = xmlData.getElementsByTagName("MarqueeText2")[0].firstChild.nodeValue;
				var omText3 = xmlData.getElementsByTagName("MarqueeText3")[0].firstChild.nodeValue;
				var omDt = xmlData.getElementsByTagName("ExpiredDT")[0].firstChild.nodeValue;
				var omHr = xmlData.getElementsByTagName("ExpiredHR")[0].firstChild.nodeValue;
				var omMi = xmlData.getElementsByTagName("ExpiredMI")[0].firstChild.nodeValue;
				// var omExDt = omDt + ' ' + omHr + ':' + omMi;
				var temp = new Array();
				temp = omDt.split('/');
				var expireDT = new Date(trimNumber(temp[2]), (trimNumber(temp[0])-1), trimNumber(temp[1]), trimNumber(omHr), trimNumber(omMi), 0)
				var currentDate = new Date();
				if(expireDT > currentDate) {
					var marqueeHTML = '<div id="outage-marquee" style="text-align:center;color:#993300;background-color:#FFFFCC;font-size:12px;font-weight:bold;margin-bottom:10px;">' + 
									  '<MARQUEE SCROLLDELAY=200 SCROLLAMOUNT=10 width="90%">' +
									  '[' + formatDateStr(omCreateDt) + '] REC is experiencing outages ' + omText1 + ' caused by ' + omText2 + 
									  '. We are working to restore service ' + omText3 + '.' + '</MARQUEE>' +
									  '<br/>' + 'Please call 1-800-552-3904 to report an outage.' + '</div>';	
					$('#page-title').after(marqueeHTML);	
				}
			}
		}



		// District Outage Summary Message
		//var distOutMag = xmlData.getElementsByTagName("DistSummMsg");
		//alert(distOutMag);


		//set interval to call new xml after certain amount of time passes
		reloadXMLInterval = setInterval(loadNewXML, xmlRefreshTime);
		
		// use getElementsByTagName instead of childNodes because browsers calculate number of children differently - this way we only return the element nodes and that's what we are after anyway
		var markerElements = markers[0].getElementsByTagName("Marker");
		var numberOfMarkerElements = markerElements.length;
		
		for (i=0; i < numberOfMarkerElements; i++) {
			var elementsInMarker = markerElements[i].getElementsByTagName("*");
			var numberOfElementsInMarker = elementsInMarker.length;
			for (j=0; j < numberOfElementsInMarker; j++) {
				var currentElement = elementsInMarker[j];
				// make var names the exact same as the xml element name to keep it simple
				// can check if text node firstChild exists - dont woory about false or 0 values giving a false return to the if statement because the values in firstChild are considered strings
				switch (currentElement.tagName) {
					case "OutageId":
					var OutageId = currentElement.firstChild.nodeValue;
					break;
					
					case "Latitude":
					var Latitude = parseFloat(currentElement.firstChild.nodeValue);
					break;
					
					case "Longitude":
					var Longitude = parseFloat(currentElement.firstChild.nodeValue);
					break;
					
					case "OutageType":
						if (currentElement.firstChild) {
							var OutageType = currentElement.firstChild.nodeValue;
						} else {
							var OutageType = '';	
						}
					break;
					
					case "EstCustAffected":
					var EstCustAffected = parseFloat(currentElement.firstChild.nodeValue);
					break;
					
					case "OutageStartTime":
					var OutageStartTime = currentElement.firstChild.nodeValue;
					break;
					
					case "EstRestorationTime":
					var EstRestorationTime = currentElement.firstChild.nodeValue;
					break;
					
					case "OutageStatus":
					var OutageStatus = currentElement.firstChild.nodeValue;
					break;

					case "OutageCause":
					var OutageCause = currentElement.firstChild.nodeValue;
					break;

					case "OutageFeeder":
					var OutageFeeder = currentElement.firstChild.nodeValue;
					break;

					case "OutageDeviceName":
					var OutageDeviceName = currentElement.firstChild.nodeValue;
					break;
					/*case "Message":
					var Message = currentElement.firstChild.nodeValue;
					break;*/
					/*case "InternalMessage":
					var InternalMessage = currentElement.firstChild.nodeValue;
					break;*/
					default: //do nothing
					}
			}
			var htmlText = '<div class="google-maps-info-window"><h4>' + 
					   "<strong>Outage ID</strong>: " +	OutageId + "</h4><br/>" +
					   "<strong>Estimated Customers Affected</strong>: " + EstCustAffected + "<br/>" +
					   "<strong>Time of Outage</strong>: " + OutageStartTime + "<br/>";
			// requested by Billy Carter, 11/18/2008
			// We would like the Estimated Time of Restoration to show on the Web Outage Map only when an ETR has been entered into the system.  
			// It is now showing “Undetermined” on all outage events.  Thanks for your help.
			if (EstRestorationTime != "UnDetermined") {
				htmlText=htmlText + "<strong>Estimated Restoration</strong>: " + EstRestorationTime + "<br/>";
			} 
			htmlText=htmlText + "<strong>Outage Status</strong>: " + OutageStatus;		   
			if (OutageCause != "UnDetermined") {
				htmlText=htmlText + "<br/>" + "<strong>Outage Cause</strong>: " + OutageCause;
			}
			//+ "<br/>" +
			//"<strong>Message</strong>: " + Message + 
			//"<br/>" +
			//"<strong>Internal Message</strong>: " + InternalMessage + 
			htmlText=htmlText + '</div>';	
					
			placeMarkers(OutageType, EstCustAffected, Latitude, Longitude, htmlText, i);
			
		}
		
	} else {			
			// some jquery magic to show no markers found
			if (outageNone.length > 0) {
				//do nothing we already printed the message
				}
			else if (outageLegend.length > 0) {
				$('#outage-map-legend').remove();
				$('#outage-map').before(noOutageHTML);
			} else {
			    $('#outage-map').before(noOutageHTML);
			}		
			reloadXMLInterval = setInterval(loadNewXML, xmlRefreshTime);		
	}
}






function placeMarkers(OutageType, EstCustAffected, Latitude, Longitude, htmlText, seq) {
	var point = new google.maps.LatLng(Latitude, Longitude);
	
	switch(EstCustAffected <= 100) {
		case true:
			if (EstCustAffected <=10) {
				
				eval('overlay'+seq+' = new google.maps.Marker(point, markerLow)');
				map.addOverlay(eval('overlay'+seq));
				google.maps.Event.addListener(eval('overlay'+seq), "click", function(){ eval('overlay'+seq).openInfoWindowHtml(htmlText, {maxWidth:350}); });
				
				} 
				
				else if (EstCustAffected <=50) {
					
				eval('overlay'+seq+' = new google.maps.Marker(point, markerElevated)');
				map.addOverlay(eval('overlay'+seq));
				google.maps.Event.addListener(eval('overlay'+seq), "click", function(){ eval('overlay'+seq).openInfoWindowHtml(htmlText, {maxWidth:350}); });
				
					}
				
				else {
					
				eval('overlay'+seq+' = new google.maps.Marker(point, markerMedium)');
				map.addOverlay(eval('overlay'+seq));
				google.maps.Event.addListener(eval('overlay'+seq), "click", function(){ eval('overlay'+seq).openInfoWindowHtml(htmlText, {maxWidth:350}); });				
				
				}
			break;
		
		case false:
			if (EstCustAffected <=500) {
				
				eval('overlay'+seq+' = new google.maps.Marker(point, markerHigh)');
				map.addOverlay(eval('overlay'+seq));
				google.maps.Event.addListener(eval('overlay'+seq), "click", function(){ eval('overlay'+seq).openInfoWindowHtml(htmlText, {maxWidth:350}); });				
				
				
				} else {
					
				eval('overlay'+seq+' = new google.maps.Marker(point, markerSevere)');
				map.addOverlay(eval('overlay'+seq));
				google.maps.Event.addListener(eval('overlay'+seq), "click", function(){ eval('overlay'+seq).openInfoWindowHtml(htmlText, {maxWidth:350}); });				
			
				
				}
			break;
		
		default:// do nothing
		
		}
	
	
	}


function addTerrirtoy(){
// Add a polygon  points
var points = [];
points.push(new google.maps.LatLng(38.2393051385386,-78.5104294564556));
points.push(new google.maps.LatLng(38.2394991461017,-78.5112719999688));
points.push(new google.maps.LatLng(38.2156528317721,-78.4725281796254));
points.push(new google.maps.LatLng(38.1439132795062,-78.4354408430011));
points.push(new google.maps.LatLng(38.1360949350053,-78.3741709595585));
points.push(new google.maps.LatLng(38.1177733900941,-78.2995989236267));
points.push(new google.maps.LatLng(38.1948750784049,-78.1613255404895));
points.push(new google.maps.LatLng(38.1371170070211,-78.0424349934718));
points.push(new google.maps.LatLng(38.1330057788145,-78.073406374895 ));
points.push(new google.maps.LatLng(38.1293906335864,-78.1197170520495));
points.push(new google.maps.LatLng(38.0514053406093,-78.0008766760412));
points.push(new google.maps.LatLng(38.0288540324709,-77.9642902350059));
points.push(new google.maps.LatLng(38.0449459510191,-77.9158688214211));
points.push(new google.maps.LatLng(38.0094334851401,-77.8954459734432));
points.push(new google.maps.LatLng(37.9827675561669,-77.9359795070751));
points.push(new google.maps.LatLng(38.0208592554399,-77.99559542873  ));
points.push(new google.maps.LatLng(37.9140442117875,-78.0714585487251));
points.push(new google.maps.LatLng(37.834538986776,-77.9404776830213));
points.push(new google.maps.LatLng(37.7941655388869,-77.9560212094123));
points.push(new google.maps.LatLng(37.7687348222581,-77.9269114490604));
points.push(new google.maps.LatLng(37.770715122586,-77.9003473123419));
points.push(new google.maps.LatLng(37.7257306107884,-77.8271501818295));
points.push(new google.maps.LatLng(37.6896724327815,-77.7477809009813));
points.push(new google.maps.LatLng(37.6918862357647,-77.6977356891946));
points.push(new google.maps.LatLng(37.7128476264576,-77.6384950009988));
points.push(new google.maps.LatLng(37.7434152645878,-77.5829361312318));
points.push(new google.maps.LatLng(37.7447450856362,-77.5634954906419));
points.push(new google.maps.LatLng(37.7915973157823,-77.5165929053723));
points.push(new google.maps.LatLng(37.794193918018,-77.4322745002509));
points.push(new google.maps.LatLng(37.7804676669926,-77.4287114317058));
points.push(new google.maps.LatLng(37.7760877893072,-77.3738840438537));
points.push(new google.maps.LatLng(37.7966133178304,-77.3613129166393));
points.push(new google.maps.LatLng(37.7132313065965,-77.2940237345253));
points.push(new google.maps.LatLng(37.688864941949,-77.2130325134614));
points.push(new google.maps.LatLng(37.6976859494597,-77.2057030499631));
points.push(new google.maps.LatLng(37.7137362972483,-77.1566761334018));
points.push(new google.maps.LatLng(37.6873331781558,-77.1477614853095));
points.push(new google.maps.LatLng(37.6355621850298,-77.1189575976719));
points.push(new google.maps.LatLng(37.586395928389,-77.0332895305617));
points.push(new google.maps.LatLng(37.5895730615051,-76.9966696852388));
points.push(new google.maps.LatLng(37.6610305969067,-76.9793526359949));
points.push(new google.maps.LatLng(37.6886433439964,-76.9395845760024));
points.push(new google.maps.LatLng(37.6933272399877,-76.907182188103 ));
points.push(new google.maps.LatLng(37.7494972337154,-76.8975746227085));
points.push(new google.maps.LatLng(37.7347462200071,-76.8676077500243));
points.push(new google.maps.LatLng(37.7019299094804,-76.8277572564324));
points.push(new google.maps.LatLng(37.6675076336756,-76.7841165125138));
points.push(new google.maps.LatLng(37.622972991252,-76.7838761162114));
points.push(new google.maps.LatLng(37.5561252243186,-76.7075812453423));
points.push(new google.maps.LatLng(37.5731701325372,-76.6670452171436));
points.push(new google.maps.LatLng(37.6009156445535,-76.6541782892009));
points.push(new google.maps.LatLng(37.7423734048624,-76.7546984005394));
points.push(new google.maps.LatLng(37.7570748242447,-76.724070248624 ));
points.push(new google.maps.LatLng(37.7985927343633,-76.6876735245465));
points.push(new google.maps.LatLng(37.8466838038816,-76.74754551077  ));
points.push(new google.maps.LatLng(37.8715058384596,-76.7727773846423));
points.push(new google.maps.LatLng(37.8511411035037,-76.8177537442521));
points.push(new google.maps.LatLng(37.8717512657979,-76.8511454947386));
points.push(new google.maps.LatLng(38.0651336557565,-76.925025295704 ));
points.push(new google.maps.LatLng(38.1774049766323,-77.1912629047689));
points.push(new google.maps.LatLng(38.2290347694325,-77.250833084171 ));
points.push(new google.maps.LatLng(38.2397071609613,-77.2233835006603));
points.push(new google.maps.LatLng(38.2507741513234,-77.3365467772317));
points.push(new google.maps.LatLng(38.2121946302562,-77.4015655776987));
points.push(new google.maps.LatLng(38.2554538578291,-77.4419555071073));
points.push(new google.maps.LatLng(38.2486225566499,-77.4589851417061));
points.push(new google.maps.LatLng(38.249156426774,-77.4617783913338));
points.push(new google.maps.LatLng(38.2333570226644,-77.5163659803669));
points.push(new google.maps.LatLng(38.2108255786707,-77.5270395761545));
points.push(new google.maps.LatLng(38.1662774266208,-77.5862340044035));
points.push(new google.maps.LatLng(38.1728541522857,-77.6075440696636));
points.push(new google.maps.LatLng(38.1822325716798,-77.6121576153421));
points.push(new google.maps.LatLng(38.198517247058,-77.5976771182479));
points.push(new google.maps.LatLng(38.2201869493396,-77.5924497054118));
points.push(new google.maps.LatLng(38.2501182101068,-77.5492628873681));
points.push(new google.maps.LatLng(38.2667643536298,-77.5771899185558));
points.push(new google.maps.LatLng(38.2671294717528,-77.5877191638596));
points.push(new google.maps.LatLng(38.2542108672819,-77.6128204763546));
points.push(new google.maps.LatLng(38.268097156392,-77.6086955681295));
points.push(new google.maps.LatLng(38.2751370257162,-77.6185018058973));
points.push(new google.maps.LatLng(38.274536140465,-77.6295146286227));
points.push(new google.maps.LatLng(38.3509905234979,-77.6924123462291));
points.push(new google.maps.LatLng(38.367037641145,-77.6179727838746));
points.push(new google.maps.LatLng(38.5153388935831,-77.6162706277584));
points.push(new google.maps.LatLng(38.5526131001893,-77.668694529859));
points.push(new google.maps.LatLng(38.5770425607999,-77.6198321509127));
points.push(new google.maps.LatLng(38.6033740938933,-77.6244265239532));
points.push(new google.maps.LatLng(38.5915719834438,-77.6351611719441));
points.push(new google.maps.LatLng(38.5933721359575,-77.6426513954034));
points.push(new google.maps.LatLng(38.5970416811275,-77.6432500996585));
points.push(new google.maps.LatLng(38.5970201648069,-77.681348275209));
points.push(new google.maps.LatLng(38.603766760702,-77.6925184576566));
points.push(new google.maps.LatLng(38.5911323387751,-77.7172217622721));
points.push(new google.maps.LatLng(38.5863669187718,-77.7157700216441));
points.push(new google.maps.LatLng(38.5795124013718,-77.7378211350327));
points.push(new google.maps.LatLng(38.5819777038681,-77.7495570784309));
points.push(new google.maps.LatLng(38.5661706041896,-77.7579376880762));
points.push(new google.maps.LatLng(38.5637461108731,-77.7302364469589));
points.push(new google.maps.LatLng(38.543660102889,-77.7103520286582));
points.push(new google.maps.LatLng(38.495223717479,-77.6905917108974));
points.push(new google.maps.LatLng(38.4880776124666,-77.6914707744917));
points.push(new google.maps.LatLng(38.4892683787266,-77.6989015292446));
points.push(new google.maps.LatLng(38.4991002685166,-77.6994956951911));
points.push(new google.maps.LatLng(38.5281516616097,-77.7415808005842));
points.push(new google.maps.LatLng(38.5185660980536,-77.749681765343 ));
points.push(new google.maps.LatLng(38.5105923949094,-77.7634651349276));
points.push(new google.maps.LatLng(38.528650244464,-77.7605301556823));
points.push(new google.maps.LatLng(38.5403357778074,-77.7700137394993));
points.push(new google.maps.LatLng(38.5382899645209,-77.7770362540554));
points.push(new google.maps.LatLng(38.5225411954236,-77.7835248791599));
points.push(new google.maps.LatLng(38.5204525826805,-77.7757010801443));
points.push(new google.maps.LatLng(38.5120512073629,-77.7691769464198));
points.push(new google.maps.LatLng(38.5118440126026,-77.7790941919371));
points.push(new google.maps.LatLng(38.506405841957,-77.7768857708882));
points.push(new google.maps.LatLng(38.4942658118199,-77.7891002558397));
points.push(new google.maps.LatLng(38.5008524895471,-77.7981280746927));
points.push(new google.maps.LatLng(38.5147920741995,-77.8023005805792));
points.push(new google.maps.LatLng(38.5081705435107,-77.823256138433 ));
points.push(new google.maps.LatLng(38.5148877230966,-77.8398084052147));
points.push(new google.maps.LatLng(38.5063226512531,-77.867292727366 ));
points.push(new google.maps.LatLng(38.5421515484061,-77.8627106308779));
points.push(new google.maps.LatLng(38.5527393038607,-77.8663089507049));
points.push(new google.maps.LatLng(38.5521188837066,-77.8735317067785));
points.push(new google.maps.LatLng(38.5335158291521,-77.9016847230774));
points.push(new google.maps.LatLng(38.5542368412761,-77.9086703445876));
points.push(new google.maps.LatLng(38.5666137552298,-77.8942141324625));
points.push(new google.maps.LatLng(38.5665565329367,-77.8620518524944));
points.push(new google.maps.LatLng(38.5840866374842,-77.8383531548374));
points.push(new google.maps.LatLng(38.5714689140424,-77.8152125762244));
points.push(new google.maps.LatLng(38.5756799717718,-77.7757173023728));
points.push(new google.maps.LatLng(38.5978387573704,-77.7423427290349));
points.push(new google.maps.LatLng(38.64826407596,-77.7568142926271));
points.push(new google.maps.LatLng(38.6372138859329,-77.8017203464683));
points.push(new google.maps.LatLng(38.661504487061,-77.8269111484869));
points.push(new google.maps.LatLng(38.6494428731132,-77.8543544531933));
points.push(new google.maps.LatLng(38.6426721376175,-77.8600079743326));
points.push(new google.maps.LatLng(38.6221671747444,-77.8611909164214));
points.push(new google.maps.LatLng(38.635580866514,-77.8791723506769));
points.push(new google.maps.LatLng(38.6595018648764,-77.8787924404364));
points.push(new google.maps.LatLng(38.6570220140139,-77.8992840216779));
points.push(new google.maps.LatLng(38.7033183477763,-77.8717139772406));
points.push(new google.maps.LatLng(38.7083885647195,-77.8326888867998));
points.push(new google.maps.LatLng(38.7295298740086,-77.834628485175 ));
points.push(new google.maps.LatLng(38.7434482322662,-77.8627848541963));
points.push(new google.maps.LatLng(38.7787945408641,-77.8562783002743));
points.push(new google.maps.LatLng(38.8175283224978,-77.8605773152836));
points.push(new google.maps.LatLng(38.8584704478074,-77.8651265368749));
points.push(new google.maps.LatLng(38.8991431784372,-78.0377693376374));
points.push(new google.maps.LatLng(39.1322027052748,-77.8284376240756));
points.push(new google.maps.LatLng(39.2478391540363,-78.0076941262977));
points.push(new google.maps.LatLng(39.213882745034,-78.1009389901893));
points.push(new google.maps.LatLng(39.2577149050563,-78.1346557549603));
points.push(new google.maps.LatLng(39.2287017825651,-78.1910227538218));
points.push(new google.maps.LatLng(39.165682848441,-78.1477357722304));
points.push(new google.maps.LatLng(39.1582835531942,-78.1643646319767));
points.push(new google.maps.LatLng(39.1357212475918,-78.2086668917454));
points.push(new google.maps.LatLng(39.0960597179191,-78.3476468107684));
points.push(new google.maps.LatLng(38.9646965306771,-78.3367507542035));
points.push(new google.maps.LatLng(38.9398693762624,-78.3050790977553));
points.push(new google.maps.LatLng(38.8766074968905,-78.3475178866741));
points.push(new google.maps.LatLng(38.8076210130809,-78.2561738406479));
points.push(new google.maps.LatLng(38.2708195816012,-78.6771135109695));
points.push(new google.maps.LatLng(38.2448745675622,-78.5089495573913));
points.push(new google.maps.LatLng(38.2393051385386,-78.5104294564556)); 
var polycolor= "#FF9933" //"#FEED72" "#E1F72E" "#9900CC" "#FF9933" 
var plinecolor= "#0033CC"
var polygon = new GPolygon(points,plinecolor,1,.5,polycolor,.4);
map.addOverlay(polygon);


}

function loadNewXML() {
	map.clearOverlays();
	clearInterval(reloadXMLInterval);
    addTerrirtoy();
	importXML();
	getQueryString(); // check param from Outage Summary
}

// parse querystring
function getQueryString() {
	var querystring = location.search.replace( '?', '' ).split( '&' );
	var idx = 0;
	for ( var i=0; i<querystring.length; i++ ) {
		  var name = querystring[i].split('=')[0];
		  var value = querystring[i].split('=')[1];
		  queryObj[name] = value;
	}
	if ( queryObj["ll"] ) {
		if ( queryObj["i"] && queryObj["i"]>0 && queryObj["i"]<33 ) {
			idx = queryObj["i"];
		} 
		gotoRecCountiesX(queryObj["ll"], idx);
	}
}


// zooms to Counties basing on the selection from Outage Summary
function gotoRecCountiesX(uuid){
	var latlong1 = "";
	var idx = 0;
	switch(uuid)
	{
	case "629404a3-8598-4330-9961-02122ff5a6a8": latlong1="38.000141,-78.523720"; idx=1;  break;
	case "fa5c4daa-bd53-4c50-a762-1a62d85c249d": latlong1="38.014500,-77.355904"; idx=2;  break;
	case "0a5867f3-2a64-451e-98ce-e52a19064ef4": latlong1="39.120000,-77.990000"; idx=3;  break;
	case "8c8fec19-dcc2-4481-9859-1789973e4df3": latlong1="38.503601,-77.925163"; idx=4;  break;
	case "b79c5355-a618-4272-822d-6c3d000768df": latlong1="37.944939,-76.925194"; idx=5;  break;
	case "231af318-17bd-4bff-9b68-b06c9285d04d": latlong1="38.711521,-77.830879"; idx=6;  break;
	case "2f0e9f60-e7ec-411a-a810-964cffd3017f": latlong1="39.230000,-78.280000"; idx=7;  break;
	case "50fba687-e16d-4869-acb2-5313f28828f4": latlong1="37.733929,-77.887878"; idx=8;  break;
	case "fadcbee1-cb65-4400-849c-0c8d09ff4e66": latlong1="38.330139,-78.475159"; idx=9;  break;
	case "c7029bdb-503c-4463-b6b0-8fa590ec0498": latlong1="37.773121,-77.452637"; idx=10;  break;
	case "259644bc-b66f-4662-b369-b9da0ab20f8d": latlong1="37.706219,-76.916183"; idx=11;  break;
	case "aaf9463c-e45c-4414-be92-89910122180c": latlong1="37.716400,-77.061363"; idx=12;  break;
	case "3fbc5e80-7981-4528-b589-5ded682f6d2c": latlong1="37.940681,-77.996979"; idx=13;  break;
	case "fe2a9eff-5691-4c42-b3e2-7eb8b3b3000f": latlong1="38.428650,-78.267822"; idx=14;  break;
	case "46596c45-66da-46a2-acb2-897f36250561": latlong1="38.253761,-78.036362"; idx=15;  break;
	case "30fbba3d-d3ee-4bfc-afad-2280ef599be6": latlong1="38.633500,-78.466400"; idx=16;  break;
	case "70ddcdce-ac8e-4d6e-847f-ab53470e1a1e": latlong1="38.691818,-78.137161"; idx=17;  break;
	case "b8bdb1c8-8e6c-4279-9811-e96585fbac59": latlong1="38.520000,-78.860000"; idx=18;  break;
	case "c820c0fd-c34b-47b3-a52d-eba956be7cfb": latlong1="38.850000,-78.580000"; idx=19;  break;
	case "a2a1a752-336a-456f-9137-11651d4f6287": latlong1="38.185101,-77.660927"; idx=20;  break;
	case "89a5dff3-66bd-43c9-940d-912f2ca120bc": latlong1="38.417110,-77.460800"; idx=21;  break;
	case "8ea41eb1-d8df-4bce-b7e3-562d4ff99f03": latlong1="39.151771,-77.982219"; idx=22;  break; //39.120000,-77.990000
	case "749603d8-6e12-4cf1-8a9b-dc4187daf209": latlong1="38.049579,-77.346647"; idx=23;  break; //38.051575,-77.347619
	case "b2b9d1e5-9087-4284-ab5c-2a07990da91a": latlong1="39.093162,-78.059443"; idx=24;  break; //39.120000,-77.990000
	case "273c8139-4856-4de3-8302-7abd0dbfcdaf": latlong1="38.473181,-77.996664"; idx=25;  break; //38.473330,-77.996139
	case "b7d0f33c-a027-4627-a517-ffaecab4bc3a": latlong1="38.918167,-78.194445"; idx=26;  break; //38.890000,-78.190000
	case "938253d2-0c22-4c5c-82aa-cf0a85b3f130": latlong1="38.380406,-78.257504"; idx=27;  break; //38.428650,-78.267822
	case "8cfa719b-9d72-4b5e-a995-c27d2dfad1dc": latlong1="39.027608,-78.280558"; idx=28;  break; //39.230000,-78.280000
	case "90d52bf6-6b2b-4d1c-960a-70a9956edff5": latlong1="38.297352,-78.440010"; idx=29;  break; //38.330139,-78.475159
	case "6a5e5a3c-d5a6-4b2f-b141-ad8d70c67f39": latlong1="39.083440,-78.218057"; idx=30;  break; //39.230000,-78.280000
	case "36582ef2-3d8b-4b6f-a97f-1e7bca01af55": latlong1="38.713452,-78.159444"; idx=31;  break; //38.691818,-78.137161
	case "9f19b8d9-bb7b-423f-b338-b80ae260c41f": latlong1="38.890000,-78.190000"; idx=32;  break;
	default: latlong1=""; idx=0;
	}
	if (latlong1 != "") {
		var latlong_array = latlong1.split(",");   
		map.setCenter(new GLatLng(latlong_array[0],latlong_array[1]), county_min_zoom);	
		// if (overviewControl != null) { overviewControl.hide(true); } 
		var x=document.getElementById("ddlrecCounties");
		if (x != null) {
			// do nothing
		} else {
			$('#outage-map').before(recocuntiesHTML);
		}
		$("select#ddlrecCounties").attr('selectedIndex', idx); 
	} else {
		map.setCenter(new google.maps.LatLng(map_center_lat, map_center_lng), min_zoom_level);
		if (overviewControl != null) {
			overviewControl.show();
		}
	}
}


//when page loads create google map 
google.setOnLoadCallback(loadMap);


