var	ROLLOVER = 1;
var	SCROLLING = 2;
var	TICKING = 3;

function NewsTicker() {
	this.items = new Array();
	this.name = "Ticker";
	this.itemcount = 0;
	this.currentspeed = 0;
	this.scrollspeed = 50;
	this.pausedelay = 1000;
	this.pausemouseover = false;
	this.stop = false;
	this.type = 1;
	this.height = 100;
	this.width = 200;
	this.displayWidth = 400;
	this.stopHeight=0;
	this.i=0;

	this.add = function () {
		var text = arguments[0];
		var item = new Item(text);
		this.items[this.itemcount] = item;
		this.itemcount = this.itemcount + 1;
	};

	this.start = function () {
		if ( this.itemcount == 1 ) {
			this.add(this.items[0].toString());
		}
		this.display();
		this.currentspeed = this.scrollspeed;
		if ( this.type == ROLLOVER || this.type == SCROLLING ) {
			this.stop = true;
			window.setTimeout(this.name + ".scroll()",this.currentspeed);
			window.setTimeout(this.name + ".stop = false", this.pausedelay);
		} else if ( this.type == TICKING ) {
			this.stop = true;
			window.setTimeout(this.name + ".rolling()",this.currentspeed);
			window.setTimeout(this.name + ".stop = false", this.pausedelay);
		}
	};

	this.display = function () {
		document.write("<div id='newsTicker' style='overflow:hidden;margin-top:-2px;width:" + this.width + "px;height:" + this.height + "px;' OnMouseOver='" + this.name + ".onmouseover();' OnMouseOut='" + this.name + ".onmouseout();'>");
		for(i = 0; i < this.itemcount; i++) {
			if ( this.type == ROLLOVER ) {
				document.write("<span id='newsItem" + i + "' style='left:0px;top:" + (this.height * i) + "px;width:" + this.width + "px;position:absolute;'>");
				document.write(this.items[i]);
				document.write("</span>");
			} else if ( this.type == SCROLLING || this.type == TICKING ) {
				document.write("<span id='newsItem" + i + "' style='left:" + (this.width * i) + "px;top:0px;width:" + this.displayWidth + "px;position:absolute;'>");
				document.write(this.items[i]);
				document.write("</span>");
			}
		}
		document.write("</div>");
	};

	this.scroll = function () {
		var obj;
		if ( this.pause == true ) {
			window.setTimeout(this.name + ".scroll()", this.pausedelay);
			this.pause = false;
		} else {
			this.currentspeed = this.scrollspeed;
			if ( !this.stop ) {
				for (i = 0; i < this.itemcount; i++) {
					obj = document.getElementById("newsItem" + i).style;
					if ( this.type == ROLLOVER ) {
						obj.top = (parseInt(obj.top) - 1) + 'px';
						if ( parseInt(obj.top) <= this.height * (-1) ) obj.top = this.height * (this.itemcount-1) + 'px';
						if ( parseInt(obj.top) == 0 ) this.currentspeed = this.pausedelay;
					} else if ( this.type == SCROLLING ) {
						obj.left = (parseInt(obj.left) - 1) + 'px';
						if ( parseInt(obj.left) <= this.displayWidth * (-1) ) obj.left = this.width * (this.itemcount-1) + 'px';
						if ( parseInt(obj.left) == 0 ) this.currentspeed = this.pausedelay;
					}
				}
			}
			window.setTimeout(this.name + ".scroll()", this.currentspeed);
		}
	};

	this.rolling = function () {
		if ( this.stop == false  ) {
			this.next();
		}
		window.setTimeout(this.name + ".rolling()", this.scrollspeed);
	};

	this.onmouseover = function () {
		if ( this.pausemouseover ) {
			this.stop = true;
		}
	};

	this.onmouseout = function () {
		if ( this.pausemouseover ) {
			this.stop = false;
		}
	};

	this.next = function() {
		var obj;
		for (i = 0; i < this.itemcount; i++) {
			obj = document.getElementById("newsItem" + i).style;
			if ( parseInt(obj.left) < 1 ) { 
				width = this.width + parseInt(obj.left);
				break;
			}
		}

		for (i = 0; i < this.itemcount; i++) {
			obj = document.getElementById("newsItem" + i).style;
			if ( parseInt(obj.left) < 1 ) { 
				obj.left = this.width * (this.itemcount-1);
			} else {
				obj.left = parseInt(obj.left) - width;
			}
		}

	};


	this.unext = function () {
		this.onmouseover();
		this.next();
		window.setTimeout(this.name + ".onmouseout()", this.pausedelay);
	};
}

function Item(text) {
	this.init(text);
}

Item.prototype = {
	init: function(text) {
		this.text = text;
	},

	toString: function() {
		return this.text;
	}
}