/**
 * Todo: Add ajax features to load content form an URL set.
 */
var sliderAF83 = new Class({
    initialize: function(name, direction, options){
        this.totIncrement = 0;  // counter
        this.nbItems = 4;       // items visible at once
        this.stepBy  = 4;       // By default       
        this.unitSize = 100;    // size of one cell

        this.options = {};
        this.options.sliderName     = name; 
        this.options.direction      = direction; 
        this.options.previousButton = 'previous-' + name;
        this.options.nextButton     = 'next-' + name;
        this.options.startButton    = 'start-' + name;
        this.options.endButton      = 'end-' + name;
        this.options.itemClassName  = 'item-' + name;

        if(options)
        {
            if($defined(options.previousButton))
                this.options.previousButton = options.previousButton;
            if($defined(options.nextButton))
                this.options.nextButton = options.nextButton;
            if($defined(options.startButton))
                this.options.startButton = options.startButton;
            if($defined(options.endButton))
                this.options.endButton = options.endButton;
            if($defined(options.itemClassName))
                this.options.itemClassName = options.itemClassName;
            if($defined(options.nbItems))
                this.nbItems = options.nbItems;
            if($defined(options.unitSize))
                this.unitSize = options.unitSize;
            if($defined(options.stepBy))
            {
                this.stepBy = options.stepBy;
            }
            else
            {
                this.stepBy = this.nbItems;
            }
        }

        this.fx = this.getFx();
        this.cleanStart();

        if($(this.options.nextButton))
        $(this.options.nextButton).addEvent(
            'click', 
            function(e){
                this.next();
                new Event(e).preventDefault();
            }.bind(this));

        if($(this.options.previousButton))
        $(this.options.previousButton).addEvent(
            'click',
            function(e){
                this.previous();
                new Event(e).preventDefault();
            }.bind(this));
        
        if($(this.options.startButton))
        $(this.options.startButton).addEvent(
            'click',
            function(e){
                this.goToStart();
                new Event(e).preventDefault();
            }.bind(this));
        if($(this.options.endButton))
        $(this.options.endButton).addEvent(
            'click',
            function(e){
                this.goToEnd();
                new Event(e).preventDefault();
            }.bind(this));
    },

    cleanStart: function(){
        if(this.getItemsAmount() <= this.nbItems)
        {
            if($(this.options.nextButton))
                $(this.options.nextButton).style.visibility = 'hidden';
        }
    },

    /**
     * Compute how many items are into container
     */
    getItemsAmount: function(){
        return $$('.' + this.options.itemClassName).length;
    },

    /**
     * Compute step shift in px
     */
    computeIncrementSize: function(){
        return this.stepBy * this.unitSize;
    },

    /**
     * Compute max increment value into px
     */
    computeMaxIncrement: function(){
                             /*this.maxIncrement = this.computeIncrementSize() * (this.nbItems - this.getItemsAmount())/this.nbItems;*/
	    this.maxIncrement = this.computeIncrementSize() * (this.nbItems - this.getItemsAmount())/this.stepBy;
    },

    getFx: function(){
        if(this.options.direction == 'horizontal')
        {
            var sliderDirection = 'margin-left';
        }
        else
        {
            var sliderDirection = 'margin-top';
        }

	    return new Fx.Style(this.options.sliderName, sliderDirection, {
				duration: 1000,
				transition: Fx.Transitions.Back.easeInOut,
				wait: true
	   });
    },

    previous: function(e){
        if(this.totIncrement < 0)
        {
            if($(this.options.nextButton))
            $(this.options.nextButton).style.visibility = 'visible';
            this.totIncrement = this.totIncrement + this.computeIncrementSize();
            this.fx.stop()
            this.fx.start(this.totIncrement);
            
            if(this.totIncrement == 0)
            {
                if($(this.options.previousButton))
                $(this.options.previousButton).style.visibility = 'hidden';
            }
        }
        else
        {
                if($(this.options.previousButton))
            $(this.options.previousButton).style.visibility = 'hidden';
        }
        
    },
	   
    next: function(e){
        this.computeMaxIncrement();
        
        if(this.totIncrement >= this.maxIncrement)
        {
                if($(this.options.previousButton))
            $(this.options.previousButton).style.visibility = 'visible';
            this.totIncrement = this.totIncrement - this.computeIncrementSize();
            this.fx.stop()
            this.fx.start(this.totIncrement);

            if(this.totIncrement == this.maxIncrement || (this.totIncrement - this.computeIncrementSize() / this.nbItems) < this.maxIncrement)
            {
                if($(this.options.nextButton))
                $(this.options.nextButton).style.visibility = 'hidden';
            }
        }
        else
        {
                if($(this.options.nextButton))
                $(this.options.nextButton).style.visibility = 'hidden';
        }
        
    }, 

    goToPosition: function(idx){
        var position = this.unitSize * idx * -1;
        
        this.computeMaxIncrement();
        
        var translateTo = position - this.maxIncrement;


                if($(this.options.previousButton) && $(this.options.nextButton))
                {
        if(translateTo <= this.maxIncrement)
        {
            $(this.options.nextButton).style.visibility = 'hidden';
            $(this.options.previousButton).style.visibility = 'visible';
        }
        
        if(translateTo > 0)
        {
            $(this.options.nextButton).style.visibility = 'visible';
            $(this.options.previousButton).style.visibility = 'visible';
        }
        else
        {
            $(this.options.previousButton).style.visibility = 'visible';
            $(this.options.nextButton).style.visibility = 'hidden';
        }
        
        if(position == 0)
        {
            $(this.options.nextButton).style.visibility = 'visible';
            $(this.options.previousButton).style.visibility = 'hidden';
        }
                }

        var shift = 0;

        if(idx == 0 && this.totIncrement != 0)
        {
            this.fx.start(0);
            this.totIncrement = 0;
            return;
        }
        
        
        if(position == this.totIncrement)
        {
            return;
        } 

        if(position > 0)
        {
            // before
            shift = Math.floor(position / this.computeIncrementSize()) * this.computeIncrementSize();
        }
        

        if(position < 0)
        {
            // after
            shift = Math.ceil(position / this.computeIncrementSize()) * this.computeIncrementSize();
        }

        this.fx.start(shift);
        this.totIncrement = shift;
    },

    goToStart: function(){
        this.goToPosition(0);
    },

    goToEnd: function(){
        this.goToPosition($$('.' + this.options.itemClassName).length - 1);
    }
});
