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

// Object Definition
function WBForecastData(loadCallback,errorCallback,errorString,shortErrorWaitTime,longErrorWaitTime,URL,prefs,pid,minUpdateRate,maxUpdateRate,widgetState,type)
{
	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.type = type;
	this.prefs = prefs;

	// WBDataRequest object. Does the work in loading data from the ISAPI.
	this.data = new WBDataRequestXML(this.url,this.dataFetched,this.prefs);
	
	this.fcthumb;
	this.numOfBlocks;
	this.titleArray = new Array(); // Holds the returned array of forecast Tites
	this.imageArray = new Array(); // Holds the returned array of forecast Images
	this.highArray = new Array();  // Holds the returned array of forecast High Values
	this.lowArray = new Array();   // Holds the returned array of forecast Low Values
	this.imageTypeArray = new Array();
	this.imageTypeArray[0] = "0";

	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 WBForecastData object.
// Used to reload current information in the WBForecastData object.
WBForecastData.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 WBForecastData object.
// Used to update that data regardless of the specified update rate.
WBForecastData.prototype.superReload = function () {
	this.data.load();
}

// Do not call this directly, only from an initialized WBForecastData object.
// Used to parse the information from the WBDataRequest object into the object variables.
WBForecastData.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); };
    
		//try
		//{
			// Remove any initialized arrays from memory
			delete this.titleArray;
			delete this.imageArray;
			delete this.highArray;
			delete this.lowArray;
			delete this.imageTypeArray;
			
			// Declare new arrays for storing returnes XML Data
			this.titleArray = new Array();
			this.imageArray = new Array();
			this.highArray = new Array();
			this.lowArray = new Array();
			this.imageTypeArray = new Array();
	
			// find the root of the DOM object, then get the forecast children
			var mainNode = this.data.request.firstChild.nextSibling;
			this.imageArray = mainNode.getElementsByTagName("aws:icon");
			this.highArray = mainNode.getElementsByTagName("aws:hi");
			this.lowArray = mainNode.getElementsByTagName("aws:lo");
			this.titleArray = mainNode.getElementsByTagName("aws:title");
			this.imageTypeArray[0] = this.imageArray[0].getAttribute("dayornight");
		//}
		//catch(e)
		//{
		//	this.errorCount++;
		//	this.loadErrorCallback(this.errorString,this.shortErrorWaitTime);
		//	this.dataLoadedCorrectly = false;
		//	return;
		//}
		this.dataLoadedCorrectly = true;
		this.lastReload = this.time.currentTime(); // set date of last update to now
		this.loadCallback();
}
