﻿var Ticker = new Class({
    Implements: Options,
    options: {
        speed: 1000,
	    delay: 5000,
	    direction: 'horizontal',
		onComplete: Class.empty,
		onStart: Class.empty
    },
	initialize: function(el,options){
	    this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		this.items.each(function(li,index) {
			w += li.getSize().x
		});
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w
		});
		this.fx = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
			var i = (this.current==0)?this.items.length:this.current;
			this.items[i-1].injectInside(this.el);
			if ( this.options.direction == 'vertical' )
			{
			    this.el.setStyle('top',0);
			}
			else /* For Horizontal */
			{
			    this.el.setStyle('left',0);
			}
		}.bind(this)});
		this.current = 0;
		this.next();
	},
	next: function() {
		this.current++;
		if (this.current >= this.items.length) this.current = 0;
		if ( this.options.direction == 'vertical' ) 
		{
		    this.fx.start({
			    top: -this.items[this.current].offsetTop,
			    left: this.items[this.current].offsetLeft
		    });
		}
		else /* For Horizontal */
		{
		    this.fx.start({
			    top: this.items[this.current].offsetTop,
			    left: -this.items[this.current].offsetLeft
		    });
		}
		this.timerId = this.next.bind(this).delay(this.options.delay);
	},
	pause: function() {
	    clearTimeout(this.timerId);
	    this.fx.pause();
	},
	resume: function() {
	    this.timerId = this.next.bind(this).delay(this.options.delay);
	    this.fx.resume();
	}
});
