var Fader = new Class({
    
    Implements: [Options, Events],
    
    options: {
        pause: 5000,
        duration: 1000,
        loop: true,
        onComplete: Class.empty,
        onStart: Class.empty
    },
    
    
    initialize: function(container, options) {
        this.setOptions(options);
        this.container = $(container);
        this.imgs = this.container.getElements('img');
        
        this.current = 0;
        this.last = this.imgs.length - 1;
        this.first = 0;
        this.next = 1;
        this.prev = this.last;
        
        if(this.imgs.length > 0) {
            this.imgs.setStyles({
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'opacity': 0
            });
            this.imgs[this.current].setStyle('opacity', 1);
            this.el = new Element('div',{'styles': {
                'position':'relative'
            }});
            this.el.injectInside(this.container);
            this.el.adopt(this.imgs);
        }
    },
    
    
    start: function() {
        if(this.imgs.length > 1) {
            this.periodical = this.show_next.bind(this).periodical(this.options.pause);
        }
    },
    
    
    stop: function() {
        $clear(this.periodical);
    },
    
    
    show_num: function(index) {
        if(this.imgs.length > 1) {
            if(!this.options.loop && this.next == this.last) this.stop();
            
            if(index > this.last) index = this.last;
            if(index < this.first) index = 0;
            
            this.imgs[index].fade('in');
            this.imgs[this.current].fade('out');
            
            this.prev = this.current == this.first ? this.last : this.current - 1;
            this.next = this.current == this.last ? this.first : this.current + 1;
            this.current = index;
            
            this.fireEvent('onChange', this);
        }
    },
    
    
    show_next: function() {
        if(this.imgs.length > 1) {
            if(!this.options.loop && next == last) this.stop();
            
            this.imgs[this.next].fade('in');
            this.imgs[this.current].fade('out');
            
            this.current = this.next;
            this.prev = this.current == this.first ? this.last : this.current - 1;
            this.next = this.current == this.last ? this.first : this.current + 1;
            
            this.fireEvent('onChange', this);
        }
    },
    
    show_prev: function() {
        if(this.imgs.length > 1) {
            if(!this.options.loop && this.next == this.last) this.stop();
            
            this.imgs[this.prev].fade('in');
            this.imgs[this.current].fade('out');
            
            this.current = this.prev;
            this.prev = this.current == this.first ? this.last : this.current - 1;
            this.next = this.current == this.last ? this.first : this.current + 1;
            
            this.fireEvent('onChange', this);
        }
    }
    
    
    
    
});