var Slider = Class.create();
Slider.prototype = {
    options: {
        shift: 900,
		shift2: 25,
		duration: 1.0,
		frequency: 2
    },
    
    initialize: function(container, controlLeft, controlRight){
		this.handStopped = false;
        this.animating = false;
		this.direction  = 0;
        this.containerSize = {
            width: $(container).offsetWidth,
            height: $(container).offsetHeight
        },
        this.content = $(container).down();
        this.controlLeft = $(controlLeft);
        this.controlRight = $(controlRight);
        
        this.initControls();
    },
    
    initControls: function(){
        this.controlLeft.href = this.controlRight.href = 'javascript:void(0)';
        Event.observe(this.controlLeft,  'click', this.shiftLeft.bind(this));
        Event.observe(this.controlRight, 'click', this.shiftRight.bind(this));
        this.updateControls(1, 0);
		this.start();
    },
    
    shiftRight: function(){
        this.Effect.cancel();
        
        var left = isNaN(parseInt(this.content.style.left)) ? 0 : parseInt(this.content.style.left);
        
        if ((left + this.options.shift) < 0) {
            var shift = this.options.shift;
            this.updateControls(1, 1);
        } else {
            var shift = Math.abs(left);
            this.updateControls(1, 0);
        }
        this.moveTo(shift);
    },
    
    shiftLeft: function(){
        this.Effect.cancel();
        
        var left = isNaN(parseInt(this.content.style.left)) ? 0 : parseInt(this.content.style.left);
        
        var lastItemLeft = this.content.childElements().last().positionedOffset()[0];
        var lastItemWidth = this.content.childElements().last().getWidth();
        var contentWidth = lastItemLeft + lastItemWidth + 8;
        
        if ((contentWidth + left - this.options.shift) > this.containerSize.width) {
            var shift = this.options.shift;
            this.updateControls(1, 1);
        } else {
            var shift = contentWidth + left - this.containerSize.width;
            this.updateControls(0, 1);
        } 
        this.moveTo(-shift);
    },
    
    moveTo: function(shift){
		this.stop();
        var scope = this;
                     
        this.animating = true;
        
        this.Effect = new Effect.Move(this.content, {
            x: shift,
            duration: 0.4,
            delay: 0,
            afterFinish: function(){
                scope.animating = false;
				scope.start();
            }
        });
    },

	moveTo2: function(shift){
        var scope = this;
                     
        this.animating = true;
        
        this.Effect = new Effect.Move(this.content, {
            x: shift,
            duration: 2.2,
            delay: 0,
            afterFinish: function(){
                scope.animating = false;
            }
        });
    },
    
    
    updateControls: function(left, right){
        if (!left)
            this.controlLeft.addClassName('disabled');
        else
            this.controlLeft.removeClassName('disabled');
        
        if (!right)
            this.controlRight.addClassName('disabled');
        else
            this.controlRight.removeClassName('disabled');
    },

	stop: function()
	{
        this.handStopped = true;
		clearTimeout(this.timer);
	},
	
	start: function()
	{
        this.handStopped = false;
		this.periodicallyUpdate();
	},
    
    pause: function()
    {
      if (!this.handStopped) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    },
    
    resume: function()
    {
      if (!this.handStopped)
        this.periodicallyUpdate();
    },
		
	periodicallyUpdate: function()
	{ 
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.rotate();
		}
		//this.rotate();
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	},
	 
	rotate: function(){
		if (this.animating)
            return;

        var left = isNaN(parseInt(this.content.style.left)) ? 0 : parseInt(this.content.style.left);
        
        var lastItemLeft = this.content.childElements().last().positionedOffset()[0];
        var lastItemWidth = this.content.childElements().last().getWidth();
        var contentWidth = lastItemLeft + lastItemWidth + 8;
        
		if(this.direction == 0){
			if ((left + this.options.shift2) < 0) {
				var shift = this.options.shift2;
				this.updateControls(1, 1);
			} else {
				var shift = Math.abs(left);
				this.updateControls(1, 0);
				this.direction = 1; 
			}
			this.moveTo2(shift);
		}else{
			if ((contentWidth + left - this.options.shift2) > this.containerSize.width) {
				var shift = this.options.shift2;
				this.updateControls(1, 1);
			} else {
				var shift = contentWidth + left - this.containerSize.width;
				this.updateControls(0, 1);

				this.direction = 0;            
			} 
			this.moveTo2(-shift);
		}
	}
}

