var ImageSlideshow = Class.create({
    holdDuration: 6,
    fadeDuration: .75,
    current: 0,
    options: {
        returnToDefault: false
    },

    initialize: function(wrapper) {
        var wrapper = $(wrapper);

        this.images = wrapper.select('img');
        this.images.each(function(image, i) {
            if (!image.hasClassName('default')) {
                image.hide();
            } else {
                this.current = i;
            }
        }.bind(this));

        window.setTimeout(this.show.bind(this), this.holdDuration*1000);
    },

    fade: function(outgoingElement, incomingElement, callback) {
        new Effect.Fade(outgoingElement, {
            duration: this.fadeDuration/2,
            afterFinish: function() {
                new Effect.Appear(incomingElement, {
                    duration: this.fadeDuration/2,
                    afterFinish: callback
                });
            }.bind(this)
        });
    },

    show: function() {
        var currentImage = this.images[this.current];
        var nextImage = this.images[this.current+1];

        if (!nextImage && this.options.returnToDefault) {
            this.fade(currentImage, this.images[0], null);
        } else if (nextImage) {
            this.fade(currentImage, nextImage, this.didShow.bind(this));
        }
    },

    didShow: function() {
        this.current++;
        window.setTimeout(this.show.bind(this), this.holdDuration*1000);
    }

});



Event.onDOMReady(function() {
    new ImageSlideshow('herorotation');
});

