var Hero = new Class({
    hold: false,
    delay: 0,
    item: [],
    ctr: {
        item: -1
    },
    options: {
        delay: 20,
        duration: 1250,
        transition: Fx.Transitions.Cubic.easeOut
    },

    initialize: function(win, el, options) {
        if (!$(win)) {
            return false;
        }
        this.hold = false;
        this.setOptions(this.options, options);
        this.ctr.item = 0;
        $$('.' + el).each(function(item) {
            item.addEvent('mouseover', this.holdOn.bind(this));
            item.addEvent('mouseout', this.holdOff.bind(this));
            item.setOpacity((this.ctr.item == 0) ? 1 : 0);
            item.setStyle('display', (this.ctr.item == 0) ? 'block' : 'none');
            item.removeClass('closed');
            this.item[this.ctr.item] = item;
            this.item[this.ctr.item].fx = new Fx.Tween(item, {
                wait: true,
                duration: this.options.duration,
                transition: this.options.transition
            });
            this.ctr.item++;
        }.bind(this));
        this.ctr.item = 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 if (!this.hold) {
            this.delay--;
        }
    },
    
    advance: function() {
        if (this.item.length && this.item.length > 1) {
            var ctr = this.ctr.item;
            this.item[ctr].fx.start('opacity', 0).chain(function() {
                this.item[ctr].setStyle('display', 'none');
            }.bind(this));
            this.ctr.item++;
            if (this.ctr.item == this.item.length) {
                this.ctr.item = 0;
            }
            this.item[this.ctr.item].fx.start('opacity', 1);
            this.item[this.ctr.item].setStyle('display', 'block');
        }
    },

    holdOn: function() {
        this.hold = true;
    },

    holdOff: function() {
        this.hold = false;
    }
});

Hero.implement(new Options);

