var m10 = initializeIcons("images/purple.png", 40, 40);
var m50 = initializeIcons("images/blue.png", 16, 16);
var m100 = initializeIcons("images/green.png", 18, 18);
var m200 = initializeIcons("images/yellow.png", 20, 20);
var m500 = initializeIcons("images/orange.png", 22, 22);
var mplus = initializeIcons("images/red.png", 24, 24);
var detail;



function OutageMarkers(doc, opts) {

	this.markers_ = new Array();
	this.opts_ = opts;
	this.handles = new Array();
	var totalAffected = 0;
	var totalOutages = 0;
	
	var points = doc.getElementsByTagName("outage");
	
	for( var i=0; i < points.length; i++ ) {
    	var point = new GLatLng(points[i].getAttribute("lat"),
                           	   points[i].getAttribute("lng"));
        var numOutages = points[i].getAttribute("affected");
        totalOutages = totalOutages + numOutages;
        var string4 = points[i].getAttribute("string4");
        var string5 = points[i].getAttribute("string5");
        var string6 = points[i].getAttribute("string6");
        var string7 = points[i].getAttribute("string7");
        var string8 = points[i].getAttribute("string8");
        var string9 = points[i].getAttribute("string9");
        var string10 = points[i].getAttribute("string10");
        var marker = this.createMarker(this, point,this.getIcon(numOutages), numOutages, string4, string5, string6, string7, string8,string9,string10);

        this.markers_.push(marker);
        totalAffected += parseInt(numOutages);
	}
	/*var totals = getElementsByClassName(document, "td", "totalText");
	for( var i in totals ) {
		totals[i].innerHTML = "Total affected: " + totalAffected;
	} */
	
		
	if( totalOutages < 1 ) {
		document.getElementById("side").style.display="none";
		noOutageMsgDiv.style.display="block";
	} else {
		noOutageMsgDiv.style.display="none";
		document.getElementById("side").style.display="block";
	}	
	

}



OutageMarkers.prototype = new Object;


	   


// Creates a marker at the given point with the given number label
OutageMarkers.prototype.createMarker = function(p, point, icon, number, string4, string5, string6, string7, string8,string9,string10) {
	if( detail == undefined ) {
		detail = outages.getElementsByTagName("detail")[0].firstChild.nodeValue;
	}
	
	var marker = new GMarker(point,icon);
          
          /* this is optional mouseOverInfoWindow==true */
    if( p.opts_ != undefined && p.opts_.onMouseOverShowInfoWindow == true ) {
    	var handle = GEvent.addListener(marker,"mouseover", function() {
          	showInfoWindow(marker, number, string4, string5, string6, string7, string8,string9,string10);
        });  
        // create a list of handlers for later freeing
        this.handles.push(handle);
     }
        
    
      	function showInfoWindow(marker, number, string4, string5, string6, string7, string8,string9,string10) {
      		var text = detail.replace(/\$3/g, number);
      		text = text.replace(/\$4/g, string4);
                text = text.replace(/\$5/g, string5);
                text = text.replace(/\$6/g, string6);
                text = text.replace(/\$7/g, string7);
                text = text.replace(/\$8/g, string8);
                text = text.replace(/\$9/g, string9);
                text = text.replace(/\$10/g, string10);
        	marker.openInfoWindowHtml(text);
		}
		
    return marker;	
}
     
OutageMarkers.prototype.getIcon = function(outages) {
	if( outages == undefined || outages < 2 ) return m10;
	else if( outages < 11 ) return m50;
	else if( outages < 51 ) return m100;
	else if( outages < 101 ) return m200;
	else if( outages < 501 ) return m500;
	else return mplus;
}

OutageMarkers.prototype.clean = function() {
	// Remove the markers from the map overlay
	while(this.markers_.length > 0 ) {
		map.removeOverlay(this.markers_.pop());
	}
	delete(this.markers_);
	
	// Remove handlers, bad leak issue
	while(this.handles.length > 0 ) {
		GEvent.removeListener(this.handles.pop());
	}
}

function initializeIcons(png, width, height) {
	var icon = new GIcon();
	icon.image = png;
	icon.iconSize = new GSize(width, height);
	icon.iconAnchor = new GPoint(width/2,height/2);
	icon.infoWindowAnchor = new GPoint(width/2, height/2);
	return icon;
}

OutageMarkers.prototype.setVisibility = function(vis) {
	if( map == undefined ) return;
	if( vis) {
		for( var i=0; i < this.markers_.length; i++ ) {
			map.addOverlay(this.markers_[i]);
		}			
	} else {
		for( var i=0; i < this.markers_.length; i++ )
			map.removeOverlay(this.markers_[i]);
	}
}