/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBState.js
 *
 * This scripts contains the WBState resuable class. A WBState object takes care
 * of the active/inactive state of the widget. This allows us to control the
 * updating of widget data depending on user interaction with the interface (ex:
 * clicking, mouseover, mouseout, etc...)
 */

// Object Definition
function WBState(activeArray,inactiveArray,toggleStateUpdateRate)
{
	this.state = true;
	this.inactiveUpdateRate = findMinimumInArray(inactiveArray);
	this.activeUpdateRate = findMinimumInArray(activeArray);
	this.loadingFunctionsArray;
	this.activeStartTime;
	this.time = new WBTime();
	this.toggleTime = toggleStateUpdateRate;
	this.timer;
}

// Do not call this function directly, only from an initialized WBState object.
// This sets the functions that you want to have called when an active/inactive
// reload is done.
WBState.prototype.setAllObjects = function (passedArray)
{
	if(this.loadingFunctionsArray)
	{
		delete this.loadingFunctionsArray;
	}

	this.loadingFunctionsArray = passedArray;
}

// Do not call this function directly, only from an initialized WBState object.
// This sets the state to active before doing an active reload. Will only accept
// calls that are at least 4000 miliseconds apart.
WBState.prototype.setActiveReload = function(state)
{
	this.state = state;
	if(this.time.checkForUpdate(this.activeStartTime,4000))
	{
		this.activeStartTime = this.time.currentTime();
		this.activeReload(false);
	}
}
WBState.prototype.setSuperReload = function()
{
	this.state = true;
	if(this.time.checkForUpdate(this.activeStartTime,4000))
	{
		this.activeStartTime = this.time.currentTime();
		this.activeReload(true);
	}
}

// Do not call this function directly, only from an initialized WBState object.
// Clears the interval timer. Used when the dashboard interface is hidden and we
// no longer want to update.
WBState.prototype.setNoReload = function()
{
	if(this.timer)
	{
		clearInterval(this.timer);
	}
	this.state = false;
}

// Do not call this function directly, only from an initialized WBState object.
// Does the active reload. Also handles switching the state to inactive after
// this.toggleTime number of miliseconds.
WBState.prototype.activeReload = function (superReload)
{
	if(this.timer)
	{
		clearInterval(this.timer);
	}
	var i;
	for(i=0;i<this.loadingFunctionsArray.length;i++)
	{
		if(this.loadingFunctionsArray[i].dataLoadedCorrectly == true || superReload == true){
			this.loadingFunctionsArray[i].load();
		}
	}
	if( this.time.checkForUpdate(this.activeStartTime,this.toggleTime) )
	{
		this.state = false;
		this.timer = setInterval('g_stateObject.activeReload(false)',this.inactiveUpdateRate);
	}
	else
	{
		this.timer = setInterval('g_stateObject.activeReload()',this.activeUpdateRate);
	}
}

// Finds the minimum in an array.
function findMinimumInArray(array)
{
	var value = array[0];
	var i;
	for(i=1;i<array.length;i++)
	{
		if(array[i] < value)
		{
			value = array[i];
		}
	}
	return value;
}