var beDiapo = new Class({
    Implements:[Events],

    options:{
        loopDelay:5000,
        curButtonClass:'curButton',
        noButtons:false
    },

    initialize: function(options) {
        this.options = Object.merge(this.options,options);
        this.bediapo = $('bediapo');
        this.diapos = this.bediapo.getElements('div.diapo');
        this.buttons = this.bediapo.getElements('div.button');
        this.curDiapo = null;
        this.loopTimeout = null;

        this.nb_diapos = this.diapos.length;

        this.buildElements();

        if (this.nb_diapos > 0) {
            this.fadein(this.diapos[0]);
            this.bediapo.fade('hide').show().fade(1);

            if (this.nb_diapos > 1) {
                this.startLoop(1);
            }
        }
    },

    buildElements: function() {
        var self = this;
        self.diapos.each(function(el,index) {
            el.index = index;
            if (!self.options.noButtons)
                el.button = self.buttons[index];
        });

        if (!self.options.noButtons && self.buttons.length > 0) {
            var buttonW = (self.bediapo.getComputedSize().width / self.buttons.length).floor() - ( (15 * (self.buttons.length - 1)) / self.buttons.length ).floor();
            self.buttons.each(function(el,index) {
                el.index = index;
                el.diapo = self.diapos[index];
                el.link = el.get('data-link');
                el.addEvent('mouseenter',function() {
                    self.pauseLoop();
                    if (!self.curDiapo || (self.curDiapo && self.curDiapo.index != index))
                        self.fadein(this.diapo);
                });
                el.addEvent('mouseleave',function() {
                    self.startLoop(index+1);
                });
                el.setStyle('width',buttonW);
                if (el.link) {
                    el.addEvent('click',function() {
                        new URI(el.link).go();
                    });
                }
            });
            self.buttons[self.buttons.length-1].setStyle('margin-right',0);
        }

        self.bediapo.getElement('div.diapos').addEvents({
            'mouseenter': function() {
                self.pauseLoop();
            },
            'mouseleave': function() {
                var index = ((self.curDiapo)?self.curDiapo.index:0);
                self.startLoop(index+1);
            }
        });

    },

    startLoop: function(index) {
        var self = this;
        var delayFading = function(index) {
            if (self.loopTimeout) {
                if (self.nb_diapos > index) {
                    self.fadein(self.diapos[index]);
                    self.loopTimeout = delayFading.delay(self.options.loopDelay,self,index+1);
                }
                else {
                    self.fadein(self.diapos[0]);
                    self.loopTimeout = delayFading.delay(self.options.loopDelay,self,1);
                }
            }
        }
        self.loopTimeout = delayFading.delay(self.options.loopDelay,self,index)
    },

    pauseLoop: function() {
        var self = this;
        if (self.loopTimeout) {
            clearTimeout(self.loopTimeout);
            self.loopTimeout = null;
        }
    },


    fadein: function(element) {
        var self = this;
        if (self.curDiapo) {
            self.fadeout(self.curDiapo);
            if (!self.options.noButtons)
                self.curDiapo.button.removeClass(self.options.curButtonClass);
        }
        self.curDiapo = element;
        element.fade('hide').show().fade(1).setStyle('z-index',3);
        if (!self.options.noButtons)
            element.button.addClass(self.options.curButtonClass);
    },

    fadeout: function(element) {
        element.fade(0);
        element.setStyle('z-index',2);
    }

});
