var TextFader = new Class({
    
    Implements: [Options, Events],
    
    options: {
        pause: 10000,
        duration: 1000,
        loop: true,
        onComplete: Class.empty,
        onStart: Class.empty
    },
    
    
    initialize: function(container, options) {
        this.setOptions(options);
        this.container = $(container);
        this.divs = this.container.getElements('div.swap');
        
        this.current = 0;
        this.last = this.divs.length - 1;
        this.first = 0;
        this.next = 1;
        this.prev = this.last;
        
        if(this.divs.length > 0) {
            
            this.divs.setStyles({
                'opacity': 0,
                'display': 'none'
            });
            
            this.divs[this.current].setStyles({
                'opacity': 1,
                'display': 'block'
            });
            
            this.divs.set('tween', {
                'property': 'opacity',
                'duartion': 'long'
            });
            
        }
    },
    
    
    start: function() {
        if(this.divs.length > 1) {
            this.periodical = this.show_next.bind(this).periodical(this.options.pause);
        }
    },
    
    
    stop: function() {
        $clear(this.periodical);
    },
    
    
    show_next: function() {
        if(this.divs.length > 1) {
            if(!this.options.loop && next == last) this.stop();
            
            var fadeout     = new Fx.Tween(this.divs[this.current]);
            var fadein      = new Fx.Tween(this.divs[this.next]);
            
            fadeout.start('opacity', 0).chain(function() {
                this.set('display', 'none');
                this.callChain();
            }).chain(function() {
                fadein.subject.setStyle('display', 'block');
                fadein.start('opacity', 1);
            });
            
            
            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);
        }
    }
    
    
});