/*  * The class that runs the whole map.  This is integrally related to the html
 * document and bad things will happen if the elements of the map.html are not
 * named correctly.
 *
 * client - a name that is used to pull the correct xml data files for each customer
 * mapCenter - GLatLng() that tells the map where to center
 * options -
 *		enableDoubleClickZoom (if true, double click zoom and center allowed): default true
 *		minZoom (1 - 18) : default 1
 *		maxZoom (1 - 18) : default 18
 *		wheelZoom : default true
 *		polyMgr : default true
 */
 	var delay = 60000;				// reload every 60 seconds
	var map;						// google map var
	var outages = null;				// parsed xml data file containing outage and poly information
	var outageMarkers = null;		// object loads and stores all outage markers
	var crewMarkers = null;			// object loads and stores all crew markers				
	var noOutageMsgDiv = null;
	var loadHash = "000";
	var data = [];
	var outageTables = [];
	var toggleState = 1;
	
	loadMap();

function reloadMap() {		
	// Get new xml and then load markers
	map.closeInfoWindow();
	var os = outageFile + dateHash();
	GDownloadUrl(os, getOutages );
}

function loadMap() {
	if( ! GBrowserIsCompatible() ) {
		alert("Your browser is not capable of viewing this page, please upgrade");
		return;
	}
	
	// Creat the google map
	map = new GMap2(document.getElementById("map"));
		
	customerSetup(map);

	noOutageMsgDiv = document.createElement("div");
  	noOutageMsgDiv.className = "noOutageMsg";
  	noOutageMsgDiv.style.display = "none";
  
 	 var img = document.createElement("img");
  	img.src="images/No_Outages.png";
  	noOutageMsgDiv.appendChild(img);
  	document.getElementById("mapBlock").appendChild(noOutageMsgDiv); 
	
	/* If their browser is good then asynchronously load up outages and zip info */
	if( ! GBrowserIsCompatible() )
		alert("Your browser is not capable of viewing this page, please upgrade");
	else {
		var os = outageFile + dateHash();
		GDownloadUrl(os, getOutages );
	}
}

function dateHash() {
	return new Date().getTime();
}

/* asynchronously called to load the actual outage data */
function getOutages(data, responseCode) {
	outages = GXml.parse(data);
	ot = outages.documentElement.getElementsByTagName("outages");
	if( ot.length > 0 && ( ot[0].getAttribute("hash") != loadHash) ) {
		clean();
		loadHash = ot[0].getAttribute("hash");
		loadMarkers();
	}
	loadGenDate();
	timerID = setTimeout("reloadMap()", delay);
}

	
function loadGenDate() {
	var gen = outages.documentElement.getElementsByTagName("generated");
	var tm = new Date();
	var date = gen[0].getAttribute("date");
	var dt = tm.getMonth() + "/" + tm.getDate() + "/" + tm.getFullYear();

	var hours = tm.getHours()
	if( hours > 11 )
		var m = "PM";
	else
		var m = "AM";
		
	if( hours > 12 )
		hours = hours-12;
		
	var minutes = tm.getMinutes()
	if (minutes < 10)
		minutes = "0" + minutes

		
	document.getElementById("lastUpdate").innerHTML =  "Map last updated " + dt + " " + hours + ":" + minutes + " " + m;
}
		

function loadMarkers() {
		
	loadPolygons();		
	writeOutageTable(outages);
		
	// Draws the outage markers
	outageMarkers = new OutageMarkers(outages.documentElement, markerOpts );
	setOutageVisibility(document.getElementById('outageCheck'));
	
	// Initializes the crew makers
	crewMarkers = new CrewMarkers(outages.documentElement);
	setCrewVisibility(document.getElementById('crewCheck'));

}

function setCrewVisibility(checkbox) {
	if( crewMarkers != undefined)
		crewMarkers.setVisibility(checkbox.checked);
}


function setOutageVisibility(checkbox) {
	if( outageMarkers != undefined)
		outageMarkers.setVisibility(checkbox.checked);
}

function setRegionVisibility(checkbox) {
	regionVisibility = checkbox.checked;
	drawRegions();
}

function onLoad() {
	document.getElementById('loading').style.display = "none";
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function clean() {
	
	// Attempt to clean up memory and remove markers and polys
	if( outageMarkers != null ) {
 		outageMarkers.clean();
		outageMarkers = null;
	}

	
	if( crewMarkers != null ) {
		crewMarkers.clean();
		crewMarkers = null;
	}
	
	cleanOutageTable();
}

function setColor(outages) {
	var style;
	if( outages == undefined ) style = "out0";
        else if(outages < 1 ) style = "out0";
	else if(outages < 11 ) style = "out10";
	else if( outages < 51 ) style = "out50";
	else if( outages < 101 ) style = "out100";
	else if( outages < 201 ) style = "out200";
	else if( outages < 501 ) style = "out500";
	else style = "outplus";

	// Bug alert.  If it can't find the style sheet, it returns null and fillColor blows up.
	var sheets=document.styleSheets;
	for( var j=0; j < sheets.length; j++ ) {
		var myrules=sheets[j].cssRules ? sheets[j].cssRules: sheets[j].rules;
		for (i=0; i<myrules.length; i++){
			if(myrules[i].selectorText.toLowerCase()=="div." + style) {
				with (myrules[i].style) {
					var fillColor = new Object;
					fillColor.backgroundColor = backgroundColor;
					fillColor.opacity = .4;
					fillColor.borderColor = 'black';
					return fillColor;
				/*	return  { 	backgroundColor: backgroundColor,
								opacity: (opacity == undefined || opacity == "") ? .4 : opacity,
								borderColor: (borderColor == undefined) ? 'black' : borderColor.split(' ')[0]
							}*/
				}
			}
		}
	}
	return null;
}

