// tickerManager v0.1
// simple functions to manage rotating news tickers
// Unitron, Matt 090107

// tickerManager
// creates manager object
function tickerManager()
{
	// create array to store tickers and set config
	this._tickers       = Array();
	this._ident         = "Userve News Ticker v0.1";
	this._updateFreq    = 5000;

	// add function references
	this.setFrequency   = setFrequency;
	this.addTicker      = addTicker;
	this.addData        = addData;
	this._isValidTicker = _isValidTicker;
	this.startAll       = startAll;
	this.doUpdate       = doUpdate;

	// we store a reference to ourselves in the window object to allow
	// us to call our object in a setTimeout call
	// first we make sure the variable is an array
	// (just incase we ever want mulitple copies of this class, maybe with different freqs)
	if( !window.tickerManagerList )
		window.tickerManagerList = Array();

	// get the next available slot and save our reference
	this._windowId      = window.tickerManagerList.length;
	window.tickerManagerList[this._windowId] = this;

	// return objecy
	return this;	
}

// setFrequency
// sets the update frequency
// iFreq: number of milliseconds
function setFrequency(iFreq)
{
	// update
	this_updateFreq = iFreq;
}

// addTicker
// adds a new ticker to the list
// tickId: a name to reference this by in subsequent tickerManager calls
// tickElement: id of the element we pass the text into
// tickPrefix: a string to prefix the text with
function addTicker(tickId, tickElement, tickPrefix)
{
	// create array
	var tickData = Array();

	// save data
	tickData[0] = tickId;		// id
	tickData[1] = tickElement;	// element id
	tickData[2] = tickPrefix;	// text prefix
	tickData[3] = Array();		// array of text
	tickData[4] = false;		// running?
	tickData[5] = 0;		// next entry of text array to display (0 = first entry)

	// add this to the ticker array
	this._tickers[tickId] = tickData;
}

// addData
// adds text to a tickers data array
// tickId: name of the ticker to add data to
// strData: string of text to add
function addData(tickId, strData, strUrl)
{
	// check its a valid ticker
	if( this._isValidTicker(tickId) )
	{
		// push data onto array
		this._tickers[tickId][3][this._tickers[tickId][3].length] = Array(strData, strUrl);
	}
}

// _isValidTicker
// checks there is a ticker in the list with the name specified
// tickId: name of the ticker to check
function _isValidTicker(tickId)
{
	// check the variable exists and that it is an array	
//	if( !this._tickers[tickId] || this._tickers[tickId].constructor.toString().indexOf("Array") < 0 )
	if( !this._tickers[tickId] )
		return false;

	// return ok
	return true;
}

// startAll
// starts all the tickers
function startAll()
{
	// list all tickers
	for(var tickId in this._tickers)
	{
		// set 'running' flag
		this._tickers[tickId][4] = true;
	}

	// do an update
	this.doUpdate();
}

// doUpdate
// updates the text of any running tickers
function doUpdate()
{
	// list all tickers
	for(var tickId in this._tickers)
	{
		// check its in the running state
		if( this._tickers[tickId][4] )
		{
			// get entries array and ref to html element
			var tickEntries = this._tickers[tickId][3];
			var displayObj  = document.getElementById(this._tickers[tickId][1]);

			// check for any entries
			if( tickEntries.length > 0 )
			{
				// get the entry to display
				var displayData = tickEntries[this._tickers[tickId][5]];

				// if we have the html element, display the text
				if( displayObj && displayObj.innerHTML )
					displayObj.innerHTML = "<a href=\"" + displayData[1] + "\" target=\"_blank\"><b>" + this._tickers[tickId][2] + ":</b> " + displayData[0] + "</a>";

				// move to next entry
				this._tickers[tickId][5]++;

				// if we have reached the array end, go back to 0
				if( this._tickers[tickId][5] == tickEntries.length )
					this._tickers[tickId][5] = 0;
			}
			else
			{
				// no entries!
				// if element is available, write error string
				if( displayObj && displayObj.innerHTML )
					displayObj.innerHTML = "No News Headlines Available.";
			}
		}
	}

	// rerun this function after the specified frequency
	// (this is where the reference in the window object is required)
	setTimeout("window.tickerManagerList[" + this._windowId + "].doUpdate();", this._updateFreq);
}
