/*
    FADE IN FADE OUT PLUGIN
    - animuje img elementy v divku
    
    <div id="flash">
        <img src="" alt=""/>
        <img src="" alt=""/>
        <img src="" alt=""/>
        <img src="" alt=""/>
        <img src="" alt=""/>
    </div>
    
    CSS STYLE
    #flash {
        position:relative;
    }
    #flash img {
        position:absolute;
        top:0px;
        left:0px;
        display:none;
    }
*/

jQuery.fn.fadeAnimation = function (timeout) {
    if(timeout === undefined){
        timeout = 3000;
    }
    timeout = parseInt(timeout);
    
    this.each(function () {
        var self = $(this);
        jQuery.fadeInit(self);
        window.setTimeout(
            function () {
                jQuery.fadeAnimate(self,0,timeout)
            }
           ,timeout
        );
    });
    return this;
};

jQuery.fadeInit = function (obj) {
    obj = jQuery(obj);
    count = $('img',obj).length;
    $('img',obj).each(function(){
        $(this).css("z-index",count--);
    });
    $('img:eq(0)',obj).show();
}

jQuery.fadeAnimate = function (obj,index,timeout) {
    obj = jQuery(obj);
    count = $('img',obj).length;
    
    if(index + 1 == count){
        $('img:eq(' + index + ')' , obj).css("z-index",count + 1);
        $('img:eq(0)',obj).fadeIn(0,function(){
            $('img:eq(' + index + ')',obj).fadeOut(1500,function(){
                $('img:eq(' + index + ')',obj).css("z-index",1);
                window.setTimeout(
                    function () {
                        jQuery.fadeAnimate(obj,0,timeout)
                    }
                    ,timeout
                );
            });
        });
    } else {
        $('img:eq(' + (index + 1) + ')',obj).fadeIn(0,function(){
            $('img:eq(' + (index++) + ')',obj).fadeOut(1500,function(){
                window.setTimeout(
                    function () {
                        jQuery.fadeAnimate(obj,index,timeout)
                    }
                    ,timeout
                );
            });
        });
    }
}

