(function( $ ){
    $.fn.makeSlideshow = function(time) {
        var time = time;
        var slideshow = this;
        var images = $('.portletImage', slideshow);
        var size = images.length;
        var current = 0;

        if (size > 0) {
            $(images[current]).css({'display':'block'});
            if (size > 1) {
                setInterval(function(){
                    fade();
                }, time);
            }
        }

        var fade = function() {
            var currentItem = $(images[current]);
            var nextItem = $(images[current+1]);

            if (current == size-1) {
                nextItem = $(images[0]);
                current = -1;
            }

            currentItem.css({'z-index':'3'});
            $(images[current+1]).css({'z-index': '5'}).fadeIn(300, function(){
                currentItem.css({'display':'none'});
            });

            current++;
        };
    };

    $(function(){
        var slideshows = $('.portletFrontpageCollection');
        for (i=0; i < slideshows.length; i++) {
            $(slideshows[i]).makeSlideshow(8000);
        }
    });
})( jQuery );

