var tickers = new Array();
var ticker_count = 0;

function objTicker(p_Id, p_cursorDelay, p_deleteDelay, p_headlineDelay, p_headlines)
{
	this.Id = p_Id;
	this.cursorDelay = p_cursorDelay;
	this.deleteDelay = p_deleteDelay;
	this.headlineDelay = p_headlineDelay;
	this.headlines = p_headlines;

	this.typedCharCount = 0;
	this.currentHeadline = 0;
	this.currentHeadlineText = "";
	this.tickerLink;
	this.typing = true;
	this.timeoutId = null;

	this.Initialise = function()
	{
		if (document.getElementById) 
		{	
			this.tickerLink = document.getElementById("tick_" + this.Id);
			for (i=0; i<this.headlines.Length; i++) this.headlines[i][1] = this.headlines[i][1].replace(/&quot;/g,'"');
			
			tickers[ticker_count] = this;
			this.Instance = ticker_count;
			this.Go();
			ticker_count++;
		}
		else return true; // don't show the ticker in older browsers
	}

	this.Go = function()
	{
		var stepDelay;

		if(this.typedCharCount == 0 && this.typing) 
		{
			// load the next headline
			this.currentHeadline = this.currentHeadline >= this.headlines.length ? 0 : this.currentHeadline;
			this.currentHeadlineText = this.headlines[this.currentHeadline][0]
			this.tickerLink.href = this.headlines[this.currentHeadline][1];
			this.currentHeadline++;
		}		

		this.tickerLink.innerHTML = this.currentHeadlineText.substring(0,this.typedCharCount) + this.getCursor();

		if (this.cursorDelay>0)
		{
			if (this.typing)
			{
				if(this.typedCharCount != this.currentHeadlineText.length)
				{
					// still typing
					this.typedCharCount++;
					stepDelay = this.cursorDelay;
				}
				else
				{
					// just typed the last character
					this.typing = false;
					stepDelay = this.headlineDelay;
				}
			}
			else
			{
				if (this.typedCharCount != 0)
				{
					if (this.deleteDelay == null)
					{
						this.typedCharCount = 0;
						this.typing = true;
						stepDelay = 0;
					}
					else
					{
						this.typedCharCount--;
						stepDelay = this.deleteDelay;
					}
				}
				else
				{
					this.typing = true;
					stepDelay = this.cursorDelay;
				}
			}
		}
		else
		{
			this.tickerLink.innerHTML = this.currentHeadlineText;
			stepDelay = this.headlineDelay;
		}
		if (this.timeoutId!=null) clearTimeout(this.timeoutId);
		this.timeoutId = setTimeout("tickerStep(" + this.Instance + ")", stepDelay);
	}

	this.getCursor = function()
	{
		if(this.typedCharCount == this.currentHeadlineText.length) return "";
		else if((this.typedCharCount % 2) == 1) return "";
		else return "_";
	}

	this.Initialise();

}

function tickerStep(whichInstance) { tickers[whichInstance].Go(); }