
var ModuleSlideshow = new Class({
    options: {
        showControls: true,
        showDuration: 12000
    },
    Implements: [Options,Events],
    initialize: function(container,elements,options) {
        //settings
        this.container = $(container);
        this.elements = $$(elements);
        this.currentIndex = 0;
        this.interval = '';
    
        //assign
        this.elements.each(function(el,i){
            if(i > 0) el.set('opacity',0);
        },this);
    
        //next,previous links
        if(this.options.showControls) {
            this.createControls();
        }

        //events
        this.container.addEvents({
            mouseenter: function() { this.stop(); }.bind(this),
            mouseleave: function() { this.start(); }.bind(this)
        });
    },
    show: function(to) {
        this.elements[this.currentIndex].fade('out');
        this.elements[this.currentIndex = ((to != undefined) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
    },
    start: function() {
        this.interval = this.show.bind(this).periodical(this.options.showDuration);
    },
    stop: function() {
        clearInterval(this.interval);
    },

    //"private"
    createControls: function() {
        var next = new Element('a',{
            href: '#',
            'class': 'next',
            events: {
                click: function(e) {
                    if(e) e.stop();
                    this.stop(); 
                    this.show();
                }.bind(this)
            }
        }).inject(this.container);
        var previous = new Element('a',{
            href: '#',
            'class': 'previous',
            events: {
                click: function(e) {
                    if(e) e.stop();
                    this.stop(); 
                    this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
                }.bind(this)
            }
        }).inject(this.container);
    }
});


