var Hero = new Class({
    delay:                      0,
    item:                       [],
    news:                       [],
    ctr: {
        item:                   -1,
        news:                   -1
    },

    options: {
        delay:                  20,
        duration:               1250,
        transition:             Fx.Transitions.Cubic.easeIn
    },

    initialize: function(win, el, options) {
        if (!$(win)) {
            return false;
        }
        
        this.setOptions(this.options, options);

        this.item = $(win).getElements('.' + el);
        this.item.each(function(item, ctr) {
            item.setOpacity((ctr == 0) ? 1 : 0);
            item.removeClass('closed');
        }.bind(this));
        
        this.news = $('latest').getElements('.latest');
        this.news.each(function(item, ctr) {
            item.setStyle('display', 'block');
            item.removeClass('closed');
            item.fx = new Fx.Slide(item, {
                duration: 950,
                transition: Fx.Transitions.Expo.easeOut
            });
            if (ctr > 0) {
                item.fx.hide();
            }
        }.bind(this));
        
        this.ctr.item = 0;
        this.ctr.news = 0;

        this.delay = this.options.delay;
        this.pause.periodical(100, this);
    },
    
    pause: function() {
        if (this.delay == 0) { 
            this.delay = this.options.delay;
            this.advance();
        } else {
            this.delay--;
        }
    },
    
    advance: function() {
        if (this.item.length && this.item.length > 1) {
            this.item[this.ctr.item].tween('opacity', 0);
            this.ctr.item++;
            if (this.ctr.item == this.item.length) {
                this.ctr.item = 0;
            }
            this.item[this.ctr.item].tween('opacity', 1);
        }
        if (this.news.length && this.news.length > 1) {
            this.news[this.ctr.news].fx.slideOut();
            this.ctr.news++;
            if (this.ctr.news == this.news.length) {
                this.ctr.news = 0;
            }
            this.wipe.delay(960, this);
        }
    },

    wipe: function() {
        this.news[this.ctr.news].fx.slideIn();
    }
});

Hero.implement(new Options);
