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

// Object Definition
function WBCameraData(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 WBDataRequestXML(this.url,this.dataFetched,this.prefs);

	this.image; // image source
	this.nextCamId = 'AWSHQ';
	
	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 WBCameraData object.
// Used to load information into the WBCameraData object.
WBCameraData.prototype.load = function ()
{
	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 WBCameraData object.
// Used to reload information into the WBCameraData object without checking for update rates.
WBCameraData.prototype.superReload = function () {
	this.data.load();
}

// Do not call this directly, only from an initialized WBCameraData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBCameraData.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); }

		// Find the root of the DOM object, then get all the alerts sub nodes
		try
		{
			var mainNode = this.data.request.firstChild.nextSibling;
			if(this.prefs.getType() == false)
			{
				this.image = mainNode.getElementsByTagName("aws:camera")[0].getAttribute("standard-src");
				this.nextCamId = mainNode.getElementsByTagName("aws:camera")[0].getAttribute("next-cam-id");
			}
			else
			{
				this.image = mainNode.getElementsByTagName("aws:featuredcamera")[0].getAttribute("standard-src");
				this.nextCamId = mainNode.getElementsByTagName("aws:featuredcamera")[0].getAttribute("next-cam-id");
			}
			this.prefs.SetCurrentNextCameraId(this.nextCamId);
		}
		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();

}
