
var Ticker = new Class({

	initialize: function(containerID, options){
		this.container = $(containerID);
		this.options = { // set options
			duration: 500,
			stay: 800,
			step: 3,
			backJump: false
		};
		Object.extend(this.options, options || {});
		this.interval = this.options.interval;
 
		this.messages = $A($ES('li',this.container));
		if (this.messages.length == 0){
			return false;
		}

		this.scroll = new Fx.Scroll(this.container.parentNode, {
			wait: true,
			duration: this.options.duration,
			transition: Fx.Transitions.linear,
			wheelStops: false
		});

                this.container.addEvent('mouseover', this.pause.bind(this));
                this.container.addEvent('mouseout', this.unpause.bind(this));


		this.doScroll(this.options.step);
	},

	isPause: false,
	pause: function() {
		this.isPause = true;
	},
	unpause: function() {
		this.isPause = false;
	},

	doScroll: function(i) {
		if (!this.isPause) {
			if (i >= this.messages.length) {
				i = 0;
				if (this.options.backJump) {
					this.container.parentNode.scrollLeft = 0;
					this.container.parentNode.scrollTop = 0;
		        		this.doScroll.delay(this.options.stay, this, (i + this.options.step) );
					return;
				}
			}
			this.scroll.toElement(
				this.messages[i]
			).chain(function(){
		        	this.doScroll.delay(this.options.stay, this, (i + this.options.step) );
		  	}.bind(this)
			);
		} else {
			this.doScroll.delay(this.options.stay, this, i);
		}
	}
});
