$(document).ready(function() {

    $.fn.carousel = function(timeout, fadetime) {
        function delayedFadeOut(el, first) {
            setTimeout(function() {
                el.fadeOut(fadetime, function() {
                    var next = el.next();
                    if (!next.length) {
                        next = first;
                    }
                    next.fadeIn(fadetime, function() {
                        delayedFadeOut(next, first);
                    });
                });
            }, timeout);
        }

        function maxListHeight(list) {
            var height = 0;
            list.find('li').each(function(i, item) {
                var item_height = $(item).height();
                if (item_height > height) {
                    height = item_height;
                }
            });
            return height;
        }

        return this.each(function() {
            var current = $('li:first', $(this)).show();
            var height = maxListHeight($(this));
            $(this).height(height);
            delayedFadeOut(current, current);
        });

    };

    $('.carousel').carousel(3000, 1000);

});
