// *********************************************************************
// * AlertEngine.js 2.0
// *
// * Copyright 2009 CCH, a Wolters Kluwer business
// *********************************************************************
function AlertEngine( )
{
	if (window[this.winId])
	{
		return window[this.winId];
	}
	window[this.winId] = this;
	this.alerts = new Array();
	this.pageReady = false;
	this.dataReady = false;
	this.alertDiv = null;
	this.alertTimer = null;
	this.alertIdx = 0;
	$(document).ready(new Function("window[\"" + this.winId + "\"].divInitialize();"));
	this.requestAlerts();
}
AlertEngine.prototype.divInitialize = function( )
{
	this.alertDiv = $("<div id=\"aleDiv\"></div>").appendTo("#" + this.displayAreaId);
	this.pageReady = true;
	if (this.dataReady)
	{
		this.startAlerts();
	}
}
AlertEngine.prototype.startAlerts = function( )
{
	if (this.alertTimer != null)
	{
		clearTimeout(this.alertTimer);
	}
	if (this.alerts.length > 0)
	{
		$("#" + this.displayAreaId).show();
	}
	this.changeAlert();
	this.alertTimer = setTimeout("window[\"" + this.winId + "\"].alertTimerEvent()", this.alertDisplayTime);
}
AlertEngine.prototype.changeAlert = function( )
{
	if (this.alerts.length > 0)
	{
		if (this.alertIdx >= this.alerts.length)
		{
			this.alertIdx = 0;
		}
		this.alertDiv.empty();
		this.alerts[this.alertIdx].appendTo(this.alertDiv);
		this.alertIdx++;
	}
}
AlertEngine.prototype.alertTimerEvent = function( )
{
	this.changeAlert();
	this.alertTimer = setTimeout("window[\"" + this.winId + "\"].alertTimerEvent()", this.alertDisplayTime);
}
AlertEngine.prototype.requestAlerts = function( )
{
	var sendParams = 
	{
		dataType: "xml",
		url: "/alerts.xml",
		success: new Function("data", "textStatus", "window[\"" + this.winId + "\"].requestSuccess(data, textStatus);")
	}
	$.ajax(sendParams);
}
AlertEngine.prototype.requestSuccess = function(data, textStatus)
{
	var rightNow = new Date();
	var alertArray = new Array();
	$("alert", data).each(function(i)
	{
		if ($("startDate", this).length > 0)
		{
			var startDate = new Date($("startDate", this).text());
			if (startDate >= rightNow)
			{
				return;
			}
		}
		if ($("stopDate", this).length > 0)
		{
			var stopDate = new Date($("stopDate", this).text());
			if (stopDate < rightNow)
			{
				return;
			}
		}
		var colorNumber = 1;
		var colorName = "red";
		if ($("color", this).length > 0)
		{
			colorName = $("color", this).text();
		}
		if (colorName == "yellow")
		{
			colorNumber = 2;
		}
		else if (colorName == "green")
		{
			colorNumber = 3;
		}
		var alertClass = "alertText" + colorNumber;
		var newAlert = $("<span></span>").append("<span class=\"" + alertClass + "\">Alert: </span>");
		if ($("url", this).length > 0)
		{
			$("<a href=\"" + $("url", this).text() + "\"></a>").text($("text", this).text()).appendTo(newAlert);
		}
		else
		{
			newAlert.append($("<span class=\"alertText\">" + $("text", this).text() + "</span>"));
		}
		alertArray.push(newAlert);
	});
	this.alerts = alertArray;
	this.dataReady = true;
	if (this.pageReady)
	{
		this.startAlerts();
	}
}
AlertEngine.prototype.winId = "CB656DEB431240CDA54828578A7F2CD7";
AlertEngine.prototype.displayAreaId = "alertDiv";
AlertEngine.prototype.alertTextId = "alertText";
AlertEngine.prototype.alertDisplayTime = 5000;
window.ae = new AlertEngine();