// $Id: Widget_Ticker.js,v 1.1 2006/07/11 16:22:38 dpeck Exp $

function Widget_Ticker_Initialize()
{
	this.pTickerBox = document.getElementById( this.sContainerID );
	this.pTicker    = document.getElementById( this.sTickerID );

	if ((typeof(this.pTickerBox) != 'undefined') && (this.pTickerBox !== null)
		&& (typeof(this.pTicker) != 'undefined') && (this.pTicker !== null))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function Widget_Ticker_start_ticker()
{
	if (this.iTickerTimerID === null) // Seems we need a new one
	{
		this.iTickerTimerID = setInterval(

			'window.gaWidget_Ticker[\''
				+  this.sName.replace(/\'/,'\\\'')
				+ '\'].ticker_tick()',

			this.iTickTime
		);
	}
}

function Widget_Ticker_stop_ticker()
{
	if (this.iTickerTimerID !== null)
	{
		clearInterval(this.iTickerTimerID);
	}
	this.iTickerTimerID = null;
}

function Widget_Ticker_ticker_pause()
{
	this.stop_ticker();
}

function Widget_Ticker_ticker_resume()
{
	this.start_ticker();
}

function Widget_Ticker_ticker_tick()
{
	var iNewLeft, bProperlyInitialized;

	this.iTickerPass++;	

	// Initialize, if necessary
	bProperlyInitialized = true;
	if (this.iTickerCurrLeft === null)
	{
		if ((typeof(this.pTickerBox) == 'undefined') || (this.pTickerBox === null))
		{
			bProperlyInitialized = this.init();
		}
		if (bProperlyInitialized)
		{
			this.iTickerCurrLeft = this.pTickerBox.offsetWidth;
		}
	}

	if (bProperlyInitialized)
	{
		iNewLeft = (this.iTickerCurrLeft - this.iTickDistance);
		// Reset position, if we've gone all the way through message
		if ((iNewLeft + this.pTicker.offsetWidth) < 0)
		{
			iNewLeft = this.pTickerBox.offsetWidth;
		}

		//window.status = 'Tick ' + this.iTickerPass + ',' + iNewLeft + '(' + this.sName + ')'; // For testing

		this.iTickerCurrLeft    = iNewLeft;
		this.pTicker.style.left = iNewLeft + 'px';

	//	if (this.iTickerPass > 3000) // For testing
	//	{
	//		this.stop_ticker();
	//	}
	}
}


// CLASS Ticker
//
function Widget_Ticker( sName, sContainerID, sTickerID )
{
	this.sName        = sName;
	this.sContainerID = sContainerID;
	this.sTickerID    = sTickerID;
}
Widget_Ticker.prototype.iTickTime		= 50; // 25 (with 1) is good (?)
Widget_Ticker.prototype.iTickDistance	= 2; // 1 (smooth/more CPU intensive) ... jittery/higher performance
Widget_Ticker.prototype.iTickerCurrLeft= null;
Widget_Ticker.prototype.iTickerTimerID	= null;
Widget_Ticker.prototype.iTickerPass		= 0;
Widget_Ticker.prototype.sName				= "";
Widget_Ticker.prototype.sContainerID	= "";
Widget_Ticker.prototype.sTickerID		= "";
Widget_Ticker.prototype.pTickerBox		= null;
Widget_Ticker.prototype.pTicker			= null;
Widget_Ticker.prototype.init				= Widget_Ticker_Initialize;
Widget_Ticker.prototype.start_ticker	= Widget_Ticker_start_ticker;
Widget_Ticker.prototype.stop_ticker		= Widget_Ticker_stop_ticker;
Widget_Ticker.prototype.ticker_pause	= Widget_Ticker_ticker_pause;
Widget_Ticker.prototype.ticker_resume	= Widget_Ticker_ticker_resume;
Widget_Ticker.prototype.ticker_tick		= Widget_Ticker_ticker_tick;


function createNewWidget_Ticker( sName, sContainerID, sTickerID )
{
var pTicker;

	pTicker = new Widget_Ticker( sName, sContainerID, sTickerID );

	window.gaWidget_Ticker.push( pTicker );
	window.gaWidget_Ticker[ sName ] = pTicker;

	return pTicker;
}

function onload_Widget_Ticker()
{
var iCurrTicker;

	for (iCurrTicker=0; iCurrTicker<window.gaWidget_Ticker.length; iCurrTicker++)
	{
		window.gaWidget_Ticker[ iCurrTicker ].init();
		window.gaWidget_Ticker[ iCurrTicker ].start_ticker();
	}
}

// AutoCreate Global Array
//
if (typeof(window.gaWidget_Ticker) == 'undefined')
{
	if (typeof(onload_Callback_addCall) == 'undefined')
	{
		alert('WARNING: onload_Callback_addCall() is undefined!  Has OnLoadCallback.js been included?\nWidget_Ticker will not initialize properly without it!');
	}
	else
	{
		onload_Callback_addCall( 'onload_Widget_Ticker();' );
	}

	window.gaWidget_Ticker = new Array(0);
}
