/**
 * Cycle V0.3.1
 */

var Cycle = new Class({
    
    currentIndex: 0,
    interval: '',
    elFade: null,
    elActive: null,
    
    options: {
        interval: 3000,
        fade: '',
        duration: 500,
        active: '',
        toggler: '',
        togglerStops: false
    },
    
    Implements: [Options, Events],
    
    initialize: function(container, options)
    {
        this.setOptions(options);
        
        this.container = $(container);
        
        if (this.options.fade == '') {
            this.elFade = $$('#' + container + ' > *');
        } else {
            this.elFade = $(container).getElements(this.options.fade);
        }
        
        this.elFade.each(function(el,i){
            if(i > 0) el.fade('hide');
        }, this);
        
        if (this.options.active != '') {
            
            this.elActive = $$(this.options.active);
            
            this.elActive.each(function(el,i){
                if(i > 0) el.removeClass('active');
                else el.addClass('active');
            }, this);
            
        }
        
        if (this.options.toggler != '') {
            
            $$(this.options.toggler).each(function(el, i){
                
                el.addEvent('click', function(e){
                    
                    if (e) e.preventDefault();
                    
                    if (i != this.currentIndex) {
                        if (this.options.togglerStops) this.stop();
                        this.show(i);
                    } else {
                        this.stop();
                        this.start();
                    }
                    
                }.bind(this));
                
            }.bind(this));
            
        }
    },
    
    show: function(to)
    {
        if (this.elFade.length == 1 && this.elActive.length == 1) {
            
            this.stop();
            
        } else {
            
            if (this.elFade && this.elFade.length > 1) {
                new Fx.Tween(this.elFade[this.currentIndex], {duration: this.options.duration}).start('opacity', 0);
            }
            
            if (this.elActive && this.elActive.length > 1) this.elActive.removeClass('active');
            
            var el = (this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elFade.length - 1 ? this.currentIndex+1 : 0)));
            
            if (this.elFade && this.elFade.length > 1) {
                new Fx.Tween(this.elFade[this.currentIndex], {duration: this.options.duration}).start('opacity', 1);
            }
            
            if (this.elActive && this.elActive.length > 1) this.elActive[el].addClass('active');
            
            this.stop();
            this.start();
            
        }
    },
    
    start: function()
    {
        if (this.options.interval > 0) {
            this.interval = this.show.bind(this).delay(this.options.interval);
        }
    },
    
    fadeIn: function()
    {
        new Fx.Tween(this.elFade[0], {
            duration: this.options.duration,
            onComplete: function(){
                this.start();
            }.bind(this)
        }).start('opacity', 0, 1);
    },
    
    stop: function()
    {
        $clear(this.interval);
    }
    
});

