/*******************************************************************************

FILE: mud_NewsScroller.js
REQUIRES: mud_API.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 1.0 - initial public release
DATE: 07/22/2005

--------------------------------------------------------------------------------

This file is part of MudNewsScroller.

	MudNewsScroller is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	MudNewsScroller is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Foobar; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*******************************************************************************/

// LIST OF CONSTANTS
MudNewsScroller.ANIM_TIME = 10; // settimeout interval
MudNewsScroller.SLOW_ANIM_TIME = 50; // slower settime interval
MudNewsScroller.DELAY_TIME = 5000; // time of idle before ticker restarts

// CONSTRUCTOR
function MudNewsScroller(id, text, x, y, strLimit, link, linkClass, random) {
	this.id = id;
	// object attributes
	this.text = new Array();
	this.text = text;
	if (random == "random") {
		this.random = true;
		this.textIndex = Math.floor(Math.random() * text.length);
	}
	else {
		this.random = false;
		this.textIndex = 0;
	}
	this.showText = "";
	this.tempText = "";
	this.x = x;
	this.y = y;
	this.strLimit = strLimit;
	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";
	
	this.slow = false;
}

MudNewsScroller.prototype.addChar = function() {
	this.showText += this.text[this.textIndex].charAt(this.frame);
	// substring front character if longer than strLimit
	if (this.showText.length > this.strLimit) {
		this.showText = this.showText.substring(1);
		this.slow = true;
	}
}

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

MudNewsScroller.prototype.die = function() {
	// reset timer
	if (this.timerID) window.clearTimeout(this.timerID);
	
	if (this.textIndex < (this.text.length-1)) {
		if (this.random) this.textIndex = Math.floor(Math.random() * this.text.length);
		else this.textIndex++;
	}
	else {
		if (this.random) this.textIndex = Math.floor(Math.random() * this.text.length);
		else this.textIndex = 0;
	}
	// reset vars
	this.frame = 0;
	this.showText = "";
	this.slow = false;
	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) {
		if (this.slow) {
			this.timerID = window.setTimeout(this.id + ".update()", MudNewsScroller.SLOW_ANIM_TIME);
		}
		else {
			this.timerID = window.setTimeout(this.id + ".update()", MudNewsScroller.ANIM_TIME);
		}
	}
	else {
		this.timerID = window.setTimeout(this.id + ".die()", MudNewsScroller.DELAY_TIME);
	}
}