/*******************************************************************************/
/* mud_NewsScroller.js                                                         */
/* REQUIRES mud_API.js
/* author: Takashi Okamoto mud(tm) - http://mudcorp.com
/*******************************************************************************/

// LIST OF CONSTANTS
MudNewsScroller.ANIM_TIME = 70; // settimeout interval
MudNewsScroller.DELAY_TIME = 500; // time of mouse idle before the nav moves back up

// CONSTRUCTOR
function MudNewsScroller(id, text, x, y, link, linkClass) {
	this.id = id;
	// object attributes
	this.text = new Array();
	if ((typeof text == "object") && (text.constructor == Array)) {
		this.text = text;
	}
	else {
		this.text[0] = text;
	}
	this.textIndex = 0;
	this.showText = "";
	this.innerText = "";
	this.x = x;
	this.y = y;
	if (link) {
		this.link = new Array();
		if ((typeof link == "object") && (link.constructor == Array)) {
			this.link = link;
		}
		else {
			this.link[0] = link;
		}
	}
	if (linkClass) {
		this.linkClass = linkClass;
	}
	// animation frame counter
	this.frame = 0;
	this.timerID = null;
	
	// move the news div to the new position
	getObject(this.id).left = this.x + "px";
	getObject(this.id).top = this.y + "px";
}

MudNewsScroller.prototype.addChar = function() {
	this.showText += this.text[this.textIndex].charAt(this.frame);
}

MudNewsScroller.prototype.draw = function() {
	// build innerHTML content
	if (this.link[this.textIndex]) {
		this.innerText = '<a href="../oldsite/javascript/' + this.link[this.textIndex];
		if (this.linkClass) this.innerText += '" class="' + this.linkClass;
		this.innerText += '">';
		this.innerText += this.showText;
		this.innerText += '</a>';
	}
	else {
		this.innerText = this.showText;
	}
	// draw innerHTML
	getRawObject(this.id).innerHTML = this.innerText;
}

MudNewsScroller.prototype.die = function() {
	// reset timer
	if (this.timerID) window.clearTimeout(this.timerID);
	
	if (this.textIndex < (this.text.length-1)) {
		this.textIndex++;
	}
	else {
		this.textIndex = 0;
	}
	// reset vars
	this.frame = 0;
	this.showText = "";
	this.update();
}

MudNewsScroller.prototype.update = function() {
	// reset timer
	if (this.timerID) window.clearTimeout(this.timerID);
	
	// updating stuff
	this.addChar();
	this.draw();
	this.frame++;
	// frame check
	if (this.frame < this.text[this.textIndex].length) {
		this.timerID = window.setTimeout(this.id + ".update()", MudNewsScroller.ANIM_TIME);
	}
	else {
		this.timerID = window.setTimeout(this.id + ".die()", MudNewsScroller.DELAY_TIME);
	}
}