function IncidentViewer(incident) {
    this.incident = incident;
}

IncidentViewer.prototype.parseIncidentDetails=function(detailsXML) {
    var details = new Object();

    var elements = detailsXML.getElementsByTagName('incident');
    for (var i=0; i < elements.length; i++) {
        details.type = elements[i].getAttribute('type');
        details.subtype = elements[i].getAttribute('subtype');
        details.description = elements[i].getAttribute('description');
        details.subtype = elements[i].getAttribute('subtype');
        details.start = elements[i].getAttribute('start');
        details.stop = elements[i].getAttribute('stop');

        var persistenceXML = elements[i].getElementsByTagName('persistence');
        for (var j=0; j<persistenceXML.length; j++) {
            details.persistence = new Object();
            var dayset = persistenceXML[j].getAttribute('dayset');
            if (dayset) {
                details.persistence.dayset = dayset.split(',');
            } else {
                details.persistence.dayset = new Array(7);
                for (var k=0; k<7; k++) {
                    details.persistence.dayset[k] = k+1;
                }
            }

            details.persistence.start =
                persistenceXML[j].getAttribute('startHour');
            details.persistence.start =
                persistenceXML[j].getAttribute('stopHour');
        }
    }

    return details;
}

IncidentViewer.prototype.requestIncidentDetails=function() {
    var ajaxComponent = new Object();
    ajaxComponent.viewer = this;
    ajaxComponent.ajaxUpdate =
        function(request)
        {
            var details = this.viewer.parseIncidentDetails(request.responseXML);
            this.viewer.drawIncidentPopup(details);
        };

    ajaxComponent.handleError =
        function(request)
        {
				alert( 'Server returned the following error: ' + request.status + ' -- ' + request.statusText );
        };

    var ajaxRequest = new AjaxHelper(ajaxComponent, '/accma/incident?id=' + this.incident.id);
    ajaxRequest.sendRequest();
}

IncidentViewer.prototype.formatDate = function(date) {
    var formatted = "";
    if (date) {
        var units = date.split('/');

        formatted = units[1] + '/' + units[2] + '/' + units[0];
    }
    return formatted;
}

IncidentViewer.prototype.formatHour = function(hour) {
    var formatted = "";

    if (hour) {
        var units = hour.split(':');

        var formatted = units[0] + ':' + units[1];
        if (units[0] < 12) {
            formatted += ' AM';
        } else {
            formatted += ' PM';
        }
    }

    return formatted;
}

IncidentViewer.prototype.formatPersistence = function(persistence) {
    var formatted = '<span style="float: left">';
    if (persistence) {
        for (var i=0; i < persistence.dayset.length; i++) {
            if (1 == persistence.dayset[i]) formatted += 'Sun';
            else if (2 == persistence.dayset[i]) formatted += 'M';
            else if (3 == persistence.dayset[i]) formatted += 'T';
            else if (4 == persistence.dayset[i]) formatted += 'W';
            else if (5 == persistence.dayset[i]) formatted += 'Th';
            else if (6 == persistence.dayset[i]) formatted += 'F';
            else if (7 == persistence.dayset[i]) formatted += 'Sat';
            else alert(persistence.dayset[i]);

            if (i != persistence.dayset.length-1) formatted += ', '
        }

        if (persistence.start) {
            formatted += '<br/> from ' + this.formatHour(persistence.start);
        }

        if (persistence.stop) {
            formatted += ' to ' + this.formatHour(persistence.stop);
        }
        formatted += '</div>';
    } else {
        formatted = '24/7';
    }

    return formatted;
}

function hideIncidentPopup()
{    
   var d = document.getElementById('radarPopup');
   d.style.visibility = "hidden";
}

IncidentViewer.prototype.drawIncidentPopup = function(details){
    var d = document.getElementById('radarPopup');
	 d.innerHTML = "";
    d.style.visibility = "visible";
    var html = 
        '<h2>Incident Info</h2>'        
        +'<a class="closeBtn"  onclick="hideIncidentPopup()" ><img src="images/btn_close.gif" alt="close popup"/></a>'        
        +'<div class="popupBody">'        
        + '<table border="0" cellpadding="0" cellspacing="0">'
        +'<tr><th> TYPE </th></tr>'
        +'<tr><td><span class="label">' + details.type + '</span>';

    if (details.subtype) {
        html+= ': <div>' + details.subtype + '</div>';
    }

    html+= '</td></tr>';

    var start = details.start.split(' ');
    var stop = details.stop.split(' ');
    html +=
        '<tr><th> DESCRIPTION </th></tr>'
        +'<tr><td>' + details.description + '<br/><p>'
		  +'<span class="label">ID: </span>'
		  + this.incident.id + '<br/>'
        +'<span class="label">Start Date: </span>'
        + this.formatDate(start[0]) + ' ' + this.formatHour(start[1]) + '<br/>'
        +'<span class="label">End Date: </span>'
        + this.formatDate(stop[0]) + ' ' + this.formatHour(stop[1]) + '<br/>'
        +'<span class="label">Persistence: </span>'
        + this.formatPersistence(details.persistence) + '</p></td></tr>'
        +'</table></div>';
		  
		  if ( $F( "authenticated" ) == "true" )
		  {
		     html += '<img id="expire" src="images/expire_incident_btn.gif" onclick="IncidentViewer.prototype.expireIncident(' + this.incident.id + ')"/></a>'
		     + '</table></div>';
		  }
	
    d.innerHTML = html;
	 
}

function clearIconByID( id )
{
	if(typeof(oIncidents) !='undefined')
    {
        var i = 0;
	for ( ; i < oIncidents.length; i++ )
	{
        if (typeof(oIncidents[i]) !='undefined')
        {
            if (oIncidents[i].id == id)
            {
                myKaMap.removeObject(oIncidents[i].img);
                oIncidents.splice(i, 1);
                hideIncidentPopup();
                break;
            }
        }
	}
    }
}

IncidentViewer.prototype.expireIncident = function( id ) {
	
	var opt = 
	{
		method: 'get',		
		parameters: 'id=' + id,
		asynchronous: false
		/*
      // Handle successful response
      onSuccess: function( t ) 
		{
			// FIXME
			// This sucks, need to just remove the one incident from the map.
			// S.D.
			//clearStoredIcons();
			//getIncidents();
			clearIconByID( id );
	
			hidePopup();
      },
		
      // Handle 404
      on404: function( t ) 
		{
		   alert( 'Error 404: location "' + t.statusText + '" was not found.' );
		},
		
      // Handle other errors
      onFailure: function(t) 
		{
		   alert( 'Error ' + t.status + ' -- ' + t.statusText );
		}
		*/
	};
	
	// remove the incident
   new Ajax.Request( '/accma/removeIncident', opt );
	clearIconByID( id );
	hidePopup();
}
