var OpacitySlideshow;
(function($) {
    OpacitySlideshow = function(container) {
        var _this = this;

        this.$container = $(container);

        if(arguments.length > 1)
            $.extend(this.options, arguments[1]);

        this.$items = this.$container.children();
        this.index = this.options.start;

        $.each(this.$items, function(index, item) {
            if(index != _this.index)
                $(item).css({ opacity: 0, display: 'none' });
        });

        this.next();
    };

    OpacitySlideshow.prototype.options = {
        speed: 1000,
        start: 0,
        interval: 8000
    };

    OpacitySlideshow.prototype.next = function() {
        var _this = this;
        this.timeoutHandler = setTimeout(function() { _this.onTimeout(); },
                                         this.options.interval);
    };

    OpacitySlideshow.prototype.onTimeout = function() {
        var _this = this;

        var $current = $(this.$items[this.index]);
        this.index = (this.index == this.$items.length - 1) ? 0 : this.index + 1;
        var $next = $(this.$items[this.index]);
        $current.animate({ opacity: 0 }, this.options.speed,
                         function() { $current.css('display', 'none')});
        $next.css('display', 'block')
            .animate({ opacity: 1 }, this.options.speed,
                     function() { _this.next(); });
    };
})(jQuery);

