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

// LIST OF CONSTANTS
MudNewsScrollerFull.ANIM_TIME = 150; // settimeout interval
MudNewsScrollerFull.DEBUG = false;

// CONSTRUCTOR
function MudNewsScrollerFull(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.textTotal = 0;
	for (var i = 0; i < this.text.length; i++) {
		this.textTotal += this.text[i].length;
	}
	this.textIndex = 0;
	this.showText = "";
	this.innerText = "";
	this.x = x;
	this.y = y;
	if (link) {
		this.link = 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;
	
	// generate charpos array
	this.charpos = new Array();
	for (var i = 0; i < this.text.length; i++) {
		if (i > 0) {
			this.charpos[i] = this.charpos[i-1] + this.text[i].length;
		}
		else this.charpos[i] = this.text[i].length;
	}
	// move the news div to the new position
	getObject(this.id).left = this.x + "px";
	getObject(this.id).top = this.y + "px";

}

MudNewsScrollerFull.prototype.checkIndex = function(pos) {
	var index = 0;
	for (var i = 0; i < this.text.length; i++) {
		if (i > 0) {
			if (pos > this.charpos[i-1]) {
				index = i;
			}
		}
	}
	return index;
}

MudNewsScrollerFull.prototype.numCharBefore = function(index) {
	var count = 0;
	for (var i = 0; i < index; i++) {
		count += this.text[i].length;
	}
	return count;
}

MudNewsScrollerFull.prototype.setString = function(start) {
	// deal with broken one first
	var index = this.checkIndex(start);
	
	var startChar = start - this.numCharBefore(index);
	this.innerText = '<a href="' + this.link;//[index];
	if (this.linkClass) this.innerText += '" class="' + this.linkClass;
	this.innerText += '">';
	this.innerText += this.text[index].substring(startChar, this.text[index].length);
	this.innerText += '</a>';

	// append all the others
	for (var i = index+1; i < this.text.length; i++) {
		this.innerText += '<a href="' + this.link;//[i];
		if (this.linkClass) this.innerText += '" class="' + this.linkClass;
		this.innerText += '">';
		this.innerText += this.text[i];
		this.innerText += '</a>';
	}
	// appending the rest from 0
	for (var i = 0; i < index; i++) {
		this.innerText += '<a href="' + this.link;//[i];
		if (this.linkClass) this.innerText += '" class="' + this.linkClass;
		this.innerText += '">';
		this.innerText += this.text[i];
		this.innerText += '</a>';
	}
	
	// finally append the cut off portion
	this.innerText += '<a href="' + this.link;//[index];
	if (this.linkClass) this.innerText += '" class="' + this.linkClass;
	this.innerText += '">';
	if (startChar > 0) this.innerText += this.text[index].substring(0, startChar);
	this.innerText += '</a>';
	
	// DEBUG
	if (MudNewsScrollerFull.DEBUG == true) {
		var objdebug = getRawObject('debug');
		objdebug.innerHTML = "index: " + index + " startChar: " + startChar + " frame: " + this.frame;
		
	}
	
}

MudNewsScrollerFull.prototype.draw = function() {
	getRawObject(this.id).innerHTML = this.innerText;
}

MudNewsScrollerFull.prototype.update = function() {
	// reset timer
	if (this.timerID) {
		window.clearTimeout(this.timerID);
	}

	// updating stuff
	this.setString(this.frame);
	this.draw();
	this.frame++;
	
	// frame check
	if (this.frame < this.textTotal) {
		this.timerID = window.setTimeout(this.id + ".update()", MudNewsScrollerFull.ANIM_TIME);
	}
	else {
		this.frame = 0;
		this.timerID = window.setTimeout(this.id + ".update()", MudNewsScrollerFull.ANIM_TIME);
	}

}

MudNewsScrollerFull.prototype.updateIE = function() {
	
	this.setString(this.frame);
	this.draw();
			
	
}

