/* A class that implements some kind of news ticker.
 *
 * Create a new instance, set the contents and start ticking with startTicking().
 */

/* Constructor
 * string id: id of the element that will contain the ticker
 * int tickDelay: delay in milliseconds until next content is shown (default 3000)
 * int highlightSpeed: speed of the highlight effect (1 = slow, 20 = fast, default 10)
 * string[] contents: the news entries (can be HTML formatted - i.e. anchors) (default empty)
 */
function Ticker(id, tickDelay, highlightSpeed, maxWidth, contents) {
	this.tickDelay = tickDelay || 3000; //delay btw messages
	this.highlightSpeed = highlightSpeed || 10; //number of pixels at a time.
	this.maxWidth = maxWidth == null ? 0 : maxWidth;
	this.contents = contents || new Array();

	// private stuff
	this.highlightPixelCount = this.highlightSpeed;
	this.highlightDelay = 200 / this.highlightSpeed;
	this.currentMessage = 0;
	this.clipWidth = 0;
	this.crosstick = document.getElementById ? document.getElementById(id) : eval("document.all." + id);
	this.crosstickParent = this.crosstick.parentNode ? this.crosstick.parentNode : this.crosstick.parentElement;
}
function Ticker_changeContent() {
	this.crosstick.style.clip = "rect(0px 0px auto 0px)";
	this.crosstick.innerHTML = this.contents[this.currentMessage];
	this.highlightMsg();
}
function Ticker_highlightMsg() {
	if (this.clipWidth <= this.crosstick.offsetWidth && (this.maxWidth == 0 || this.clipWidth < this.maxWidth)) {
		this.clipWidth += this.highlightPixelCount;
		this.crosstick.style.clip = "rect(0px " + this.clipWidth + "px auto 0px)";
		this.beginclip = window.setTimeout(Ticker.bindFunction(this.highlightMsg, this), this.highlightDelay);
	}
	else	{
		this.clipWidth = 0;
		window.clearTimeout(this.beginclip);
		if (this.currentMessage >= this.contents.length - 1) this.currentMessage = 0;
		else this.currentMessage++;
		window.setTimeout(Ticker.bindFunction(this.changeContent, this), this.tickDelay);
	}
}
function Ticker_startTicking() {
	////do we need this?
	//if (parseInt(this.crosstick.offsetHeight) > 0)
	//	this.crosstickParent.style.height = crosstick.offsetHeight + 'px';
	//else
	//	setTimeout("crosstickParent.style.height=crosstick.offsetHeight+'px'",100) //delay for Mozilla's sake;
	if (this.contents.length > 0) this.changeContent();
}

function Ticker_argsToArray(args) {
  var arr = new Array();
  for (var i = 0; i < args.length; i++) arr[arr.length] = args[i];
  return arr;
}
function Ticker_bindFunction() {
  var args = Ticker.argsToArray(arguments), __method = args.shift(), object = args.shift();
  return function() {
    return __method.apply(object, args.concat(Ticker.argsToArray(arguments)));
  }
}
Ticker.prototype.changeContent = Ticker_changeContent;
Ticker.prototype.highlightMsg = Ticker_highlightMsg;
Ticker.prototype.startTicking = Ticker_startTicking;
Ticker.argsToArray = Ticker_argsToArray;
Ticker.bindFunction = Ticker_bindFunction;
