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

// Object Definition
function WBMoreTabData(loadCallback,errorCallback,errorString,shortErrorWaitTime,longErrorWaitTime,URL,preferences,minUpdateRate,maxUpdateRate,widgetState)
{
	this.time = new WBTime(); // New time object
	this.lastReload; // Last time the data was successfully updated
	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)
	
	this.tabContent;  // Content for the More tab

	this.prefs = preferences; // Holds the current WBPrefernces object

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

	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 WBMoreTabData object.
// Used to reload current information in the WBMoreTabData object.
WBMoreTabData.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 WBMoreTabData object.
// Reloads the data without checking for update rates.
WBMoreTabData.prototype.superReload = function () {
	this.data.load();
}

// Do not call this directly, only from an initialized WBMoreTabData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBMoreTabData.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)
	{
		try
		{
			var mainNode = this.data.request.firstChild.nextSibling;
			this.tabContent = mainNode.getElementsByTagName(awscontentpane);
		}
		catch(e)
		{
			alert(e);
			this.errorCount++;
			this.loadErrorCallback(this.errorString,this.shortErrorWaitTime);
			this.dataLoadedCorrectly = false;
			return;
		}
		this.dataLoadedCorrectly = true;
		this.lastReload = this.time.currentTime();
		this.loadCallback();
	}
}
