/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBAlertsData.js
 *
 * This scripts contains the WBAlertsData resuable class. A WBAlertsData object
 * takes care of loading and holding XML data from the XML_Alerts call.
 */

// Object Definition
function WBAlertsData(loadCallback,errorCallback,errorString,shortErrorWaitTime,longErrorWaitTime,URL,prefs,pid,minUpdateRate,maxUpdateRate,widgetState)
{
	this.time = new WBTime(); // New WBTime object
	this.lastReload; // Last time the data was successfully updated
	this.minUpdateRate = minUpdateRate; // Rate at which the data should be updated in miliseconds (min boundary)
	this.maxUpdateRate = maxUpdateRate; // Rate at which the data should be updated in miliseconds (max boundary)

    this.prefs = prefs;
	this.url = URL;
	this.minUpdateRate = this.url.minupdaterate; // Rate at which the data should be updated in miliseconds (min boundary)
	this.maxUpdateRate = this.url.maxupdaterate; // Rate at which the data should be updated in miliseconds (max boundary)

	// WBDataRequest object. Does the work in loading data from the ISAPI.
	this.data = new WBDataRequest(this.url,this.dataFetched,this.prefs);

	this.numberOfAlerts = 0; // initialize the number of alerts to zero
	this.county; // County for respective alerts
	this.titleArray = new Array(); // Array of alert titles
	this.postedArray = new Array(); // Array of alert posting times
	this.expiresArray = new Array(); // Array of alert expiring times
	this.alertIdArray = new Array(); // Array of alert Id's

	this.request; // Holds the XMLHttpRequest Object

	this.loadCallback = loadCallback;		      // Data Loaded Callback
	this.loadErrorCallback = errorCallback;       // Data Error Callback
	this.errorString = errorString;               // String passed to the reload timer
	this.shortErrorWaitTime = shortErrorWaitTime; // Timeout for short error
	this.longErrorWaitTime = longErrorWaitTime;   // Timeout for long error
	this.errorCount = 0;                          // Counts the number of internal loading errors
	this.dataLoadedCorrectly = false;             // BOOL to see if we have correct data in the object or not
	
	this.widgetState = widgetState;
	
	this.dataFetched.currentScope = this; // Used when the dataFetched function has lost scope
}

// Do not call this directly, only from an initialized WBAlertsData object.
// Used to load information into the WBAlertsData object.
WBAlertsData.prototype.load = function ()
{
	if(g_preferencesObject.getType() == false)
	{
		if(this.errorCount > 2)
		{
			this.errorCount = 0;
			this.loadErrorCallback(this.errorString,this.longErrorWaitTime);
			this.dataLoadedCorrectly = false;
			return;
		}

		// Check to see if we are allowed to reload the object based on the update rate.
		// state = 1 uses the active min update rate
		// otherwise use the max update rate
		if(this.widgetState.state)
		{
			if(this.time.checkForUpdate(this.lastReload,this.minUpdateRate)) { this.data.load(); }
		}
		else
		{
			if(this.time.checkForUpdate(this.lastReload,this.maxUpdateRate)) { this.data.load(); }
		}
	}
}

// Do not call this directly, only from an initialized WBAlertsData object.
// Used to reload information into the WBAlertsData object without checking for update rates.
WBAlertsData.prototype.superReload = function () {
	this.data.load();
}

// Do not call this directly, only from an initialized WBAlertsData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBAlertsData.prototype.dataFetched = function ()
{
	// make sure 'this' is in scope, if not call it again with the correct scope
    if(this != arguments.callee.currentScope){ return arguments.callee.apply(arguments.callee.currentScope, arguments); }

	if(this.data.request.readyState == 4 && this.data.request.status == 200)
	{
		// Find the root of the DOM object, then get all the alerts sub nodes
		try
		{
			var mainNode = this.data.request.responseXML.firstChild.nextSibling;
			var alertsnode = findChild(mainNode,"aws:alerts");
			this.numberOfAlerts = parseInt(alertsnode.getAttribute("count")); // store the number of alerts
		}

		catch(e)
		{
			this.errorCount++;
			this.loadErrorCallback(this.errorString,this.shortErrorWaitTime);
			this.dataLoadedCorrectly = false;
			return;
		}
		this.dataLoadedCorrectly = true;
		this.lastReload = this.time.currentTime(); // set the last successful update time to now
		this.loadCallback();
	}
}
