/*****************************************************************

	NewsTicker javascript class by Peter Bailey - Copyright (c) 2003
	Website: http://www.peterbailey.net/site/dev/jsclasses/

	Main Features:
	-	Attach to any block-level element with an id
	-	Supports Rich HTML for news items
	-	Uses cool-looking typing display
	-	Toogle message looping
	-	Toggle message overwriting
	-	OO design allows multiple tickers per page

	Compatibility:
	-	Tested on IE6 and Mozilla 1.1
	-	DOM compliant browsers only

	Requires:
	-	Timer class
	-	Number methods 
		found at http://www.peterbailey.net/dhtml/js/number.methods.js

	Note: This document was created with a tab-spacing of four (4)

******************************************************************/

// Constructor
function NewsTicker( data, targetId, tDelay, mDelay, loop, overwrite )
{
	this.data        = data;
	this.elem        = document[( 'getElementById' || 'all' )]( targetId );
	this.tDelay      = tDelay;
	this.mDelay      = mDelay;
	this.timer       = new Timer( this );
	this.loop        = Boolean( loop );
	this.overwrite   = Boolean( overwrite );
	this.mCount      = 0;
	this.count       = 0;
	this.message     = "";
	this.typePortion = "";
	this.cursorChar  = "";
}

// Plays ticker
NewsTicker.prototype.play = function()
{
	this.setMessage();
	this.interval = this.timer.setInterval( "type", this.tDelay );
	this.existing = ( this.mCount == 0 || this.overwrite ) ? '' : this.elem.innerHTML;
}

// Sets message
NewsTicker.prototype.setMessage = function( )
{
	if ( this.wrapper )
		this.message = this.wrapper.pre + this.data[this.mCount] + this.wrapper.post;
	else
		this.message = this.data[this.mCount];
}

// Recurring function - this does the typing.
NewsTicker.prototype.type = function()
{
	if ( this.count == this.message.length )
	{
		this.timer.clearInterval( this.interval );
		this.elem.innerHTML = this.existing + this.message;
		this.nextMessage();
	}
	else
	{
		if ( this.count == 0 )
			this.typedPortion = "";
		else
			this.typedPortion = this.message.substring( 0, this.count );
		this.cursorChar = this.message.charAt( this.count );
		if ( this.cursorChar == "<" )
		{
			this.typedPortion += this.getHTMLtag();
		}
		else
		{
			this.elem.innerHTML = this.existing + this.getHTML();
			this.count++;
		}
	}
}

// Go to next news item
NewsTicker.prototype.nextMessage = function()
{
	this.count = 0;
	this.mCount++;
	if ( this.mCount == this.data.length ) 
		if ( this.loop )
		{
			this.mCount   = 0;
			this.existing = '';
		}
		else
		{
			this.quit();
			return;
		}
	this.timeout = this.timer.setTimeout( "play" , this.mDelay );
}

// Quit ticker
NewsTicker.prototype.quit = function()
{
	this.timer.clearTimeout( this.timeout );
}

// Add wrapper to each news item before display
NewsTicker.prototype.addWrapper = function( pre, post )
{
	this.wrapper = new Object();
	this.wrapper.pre = pre;
	this.wrapper.post = post;
}

// Helper for type() method
NewsTicker.prototype.getHTMLtag = function()
{
	var html = '';
	do
	{
		this.workChar = this.message.charAt( this.count );
		html += this.workChar;
		this.count++;
	}	while( this.workChar != '>' )
	return html;
}

// Helper for type() method
NewsTicker.prototype.getHTML = function()
{
	var html = this.typedPortion;
	if ( this.count != this.message.length - 1 )
		html += '<span class="cursorChar">' + this.cursorChar + '</span>';
	return html;
}