//AccessibleTicker.js

function AccessibleTicker(aid, ulid, options)
{
	//Do nothing if incompatible browser
	if(!document.getElementById || !document.createElement) return;
	
	//Account for no options being set
	if (!options){options={};}
	
	//Hide standard display, unhide ticker
	this.anchorObject = document.getElementById(aid);
	document.getElementById(ulid).style.display = "none";
	this.anchorObject.style.display = "";
	this.atags = document.getElementById(ulid).getElementsByTagName("a");
	
	//Setup options
	this.characterTimeout = (options.characterTimeout || 50);
	this.storyTimeout = (options.storyTimeout || 3000);
	this.widgetNone = (options.widgetNone || "");
	this.widgetOne = (options.widgetOne || "_");
	this.widgetTwo = (options.widgetTwo || "-");
	this.header = (options.header || "&nbsp;");
	this.headerClass = (options.headerClass || "tickerHeader");

	//Init counters
	this.currentStory = -1;
	this.currentPos = 0;

	//Start timer
	var self = this;
	this.timer = setTimeout(function(){self.run()},50);
}

AccessibleTicker.prototype = {

	run: function()
	{
		var self = this;
		var myTimeout;  
		if(this.currentPos == 0)
		{
			this.currentStory++;
			if (!this.atags[this.currentStory]){ this.currentStory = 0; }
			this.anchorObject.style.color = this.atags[this.currentStory].style.color;
			this.anchorObject.style.textDecoration = this.atags[this.currentStory].style.textDecoration;
			this.currentStory = this.currentStory % this.atags.length;
			this.theStorySummary = this.atags[this.currentStory].innerHTML.replace(/&quot;/g,'"');
			this.anchorObject.href = this.atags[this.currentStory].href;
			this.thePrefix = "<span class=\"" + this.headerClass + "\">" + this.header + "</span>";
		}
		this.anchorObject.innerHTML = this.thePrefix + this.theStorySummary.substring(0,this.currentPos) + this.whatWidget();
		if(this.currentPos != this.theStorySummary.length)
		{
			this.currentPos++;
			this.timer = setTimeout(function() {self.run()}, this.characterTimeout);
		}
		else
		{
			this.currentPos = 0;
			this.timer = setTimeout(function() {self.run()}, this.storyTimeout);
		}		
	},
	
	whatWidget: function()
	{
		if(this.currentPos == this.theStorySummary.length) { return this.widgetNone; }
		if((this.currentPos % 2) == 1) { return this.widgetOne; }
		else { return this.widgetTwo;	}
	}
}
