// Class Itemsticker
// Author: Franz Knipp
// (c) 2006 m-otion GmbH

function Itemsticker(_items, _element) {

	this.items = _items;
	this.element = _element;

	this.posstep = 2;	// number of characters per step
	this.pause = 40; 	// pause in intervals
	
	this.num = this.items.length;
	this.index = 0;		// index of actual item
	this.state = 0;		// 0 = write, 1 = pause
	this.pos = 0;   		// position in actual item
	this.item = null;	// actual item

	this.pausecount = 0;	// counter for the pause

	if(this.num) {			// set starting conditions
		this.item = this.items[0];
		this.title = this.item.title;
	}

	this.step = function() {
		if(! this.element) return;
		if(this.state == 0) {	// writing out the text
		
			this.pos += this.posstep;	// advancing end of string
			if(this.pos >= this.title.length) {
				this.pos = this.title.length;
				this.state = 1;	// switching to pause state
				this.pausecount = this.pause;
			}
			this.element.value = this.title.substr(0, this.pos);
			
		} else if(this.state == 1) {	// waiting to print the next item
			this.pausecount--;
			if(this.pausecount <= 0) {
				this.index = (this.index + 1) % this.num;
				this.item = this.items[this.index];
				this.title = this.item.title;

				this.state = 0;
				this.pos = 0;
			}
		}
	}

	this.openLink = function() {
		var item = this.items[this.index];
		if(this.item && this.item.link) {
			var newWin = window.open(this.item.link, "_blank");
		}
	}

}

