/**
 * Gestion du diaporama
 */
var Scroll = new Class({
	
	Implements: [Options],	

	// init
    initialize: function(container, options) {
	           
        this.slides = [];
        this.buttons = [];
          
        // maj options
        this.setOptions({
            wrapper: ".wrapper",
            slides: ".slide",
            buttons: ".button",
            buttonNext: ".button-next",
            buttonPrev: ".button-prev",
            duration: 250,
            transition: "scroll",
            start: 0,
            buttonOnClass: "selected",                
            buttonsAction: "none",               
            auto: "on",
            autoInterval: 5000
            }, options);
        
        this.element = $(container).getElement(this.options.wrapper);
	    this.slides = $(container).getElements(this.options.slides);
	    this.buttons = $(container).getElements(this.options.buttons);
	    this.buttonNext = $(this.options.btnContainer).getElement(this.options.buttonNext);
        this.buttonPrev = $(this.options.btnContainer).getElement(this.options.buttonPrev);
		
	    // init diapo
	    this.showSlide(this.options.start);
	    
	    // init action boutons
	    this.setupAction(this.options.buttonsAction);	    
	    
	    // mode auto 
	    if (this.options.auto == "on" || this.options.auto == "once") 
	        this.auto();    
        
    },
    
    // gestion action boutons
    setupAction: function(action) {
	
        if (this.options.buttonsAction != 'none') {
            this.buttons.each(function(button, i) {
                $(button).addEvent(action, function(){
                    if (this.currentSlide != i) 
                        this.showSlide(i);
                    this.stop()
                }.bind(this))
            }, this)
        }
        
        if (this.buttonNext && this.buttonPrev) {
            this.buttonNext.addEvent('click', function(){
                if (this.currentSlide + 1 >= this.slides.length) {
                    next = 0
                }
                else {
                    next = this.currentSlide + 1
                };
                this.showSlide(next);
                this.stop()
            }
.bind(this));
            this.buttonPrev.addEvent('click', function(){
                if (this.currentSlide - 1 < 0) {
                    next = this.slides.length - 1
                }
                else {
                    next = this.currentSlide - 1
                };
                this.showSlide(next);
                this.stop()
            }
.bind(this))
        }
        
    },
    
    // affichage de la diapo no
    showSlide: function(no) {
	
        this.slides.each(function(slide, i) {
        	
            var button = $(this.buttons[i]);
            
            if (i == no && i != this.currentSlide) {
                if (button) 
                    button.addClass(this.options.buttonOnClass);
            }
            else {
                if (button) 
                    button.removeClass(this.options.buttonOnClass);
            }
        }, this);
                
        this.currentSlide = no;
        
        if (this.options.transition == 'fade')
            this.fadeSlide()        
        else
            this.fxScroll()
        
    },
    
    // scroll entre 2 diapo
    fxScroll: function() {
        new Fx.Scroll(this.element, {duration: this.options.duration}).toElement(this.slides[this.currentSlide])
    },
    
    // effet de transparence entre 2 diapo
    fadeSlide: function() {
	
        var fade = new Fx.Tween(this.element, {'property':'opacity',
            duration: this.options.duration,
            onComplete: function(){
                new Fx.Scroll(this.element, {
                    duration: 1,
                    onComplete: function(){
                        new Fx.Tween(this.element, {'property':'opacity',
                            duration: this.options.duration
                        }).start(0.01, 1)
                    }.bind(this)
                }).toElement(this.slides[this.currentSlide])
            }.bind(this)
        });
        
        fade.start(1, 0.01)
    },
    
    // diaporama auto
    auto: function() {
        this.intervalID = this.rotate.periodical(this.options.autoInterval, this);       
    },
    
    // changement de diapo pour le mode auto
    rotate: function() {
	
        if (this.currentSlide + 1 >= this.slides.length)
            next = 0;
        else
            next = this.currentSlide + 1;
                                
        if ( (this.options.auto == "once" && next == 0) || this.slides.length == 1)
            this.stop();
        else
        	this.showSlide(next);       
    },
    
    // suppression mode auto
    stop: function() {    	
        $clear(this.intervalID);
    }    
    
});
