jQuery.fn.fadeAnimation = function (timeout) {
    if(timeout === undefined){
        timeout = 3000;
    }
    timeout = parseInt(timeout);
    
    this.each(function () {
        var self = $(this);
        jQuery.fadeInit(self);
        
        if($('div',self).length > 1){
            window.setTimeout(
                function () {
                    jQuery.fadeAnimate(self,0,timeout)
                }
               ,timeout
            );
        }
    });
    return this;
};

jQuery.fadeInit = function (obj) {
    obj = jQuery(obj);
    count = $('div',obj).length;
    $('div',obj).each(function(){
        $(this).css("z-index",count--);
    });
    $('div:eq(0)',obj).show();
}

jQuery.fadeAnimate = function (obj,index,timeout) {
    obj = jQuery(obj);
    count = $('div',obj).length;
    
    if(index + 1 == count){
        $('div:eq(' + index + ')' , obj).css("z-index",count + 1);
        $('div:eq(0)',obj).fadeIn(0,function(){
            $('div:eq(' + index + ')',obj).fadeOut(1500,function(){
                $('div:eq(' + index + ')',obj).css("z-index",1);
                window.setTimeout(
                    function () {
                        jQuery.fadeAnimate(obj,0,timeout)
                    }
                    ,timeout
                );
            });
        });
    } else {
        $('div:eq(' + (index + 1) + ')',obj).fadeIn(0,function(){
            $('div:eq(' + (index++) + ')',obj).fadeOut(1500,function(){
                window.setTimeout(
                    function () {
                        jQuery.fadeAnimate(obj,index,timeout)
                    }
                    ,timeout
                );
            });
        });
    }
}

$(function(){
    $('#panelFade').fadeAnimation(4000);
});
