/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.01
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($){  

    $.fn.s3Slider = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var moPause     = (vars.moPause != undefined) ? vars.moPause : true;
        var no          = 0;
        var mOver       = false;
        var next        = null;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");
        var tmp         = null;
        var trans       = false;
        
        //$(items[0]).css('display','block');
        //$(itemsSpan[0]).css('display','block');
            
        items.each(function(i) { 
            if(moPause) {
                $(items[i]).mouseover(function() {
                   mOver = true;
                });
                $(items[i]).mouseout(function() {
                    mOver   = false;
                });
            }
        });
        var fadeIn  = function(no) {
            no = no;
            trans = true;
            $(items[no]).fadeIn((timeOut/6), function() {
                if($(itemsSpan[no]).css('bottom') == 0) {
                    $(itemsSpan[no]).slideUp((timeOut/6), function() {
                        trans = false;
                        if(!mOver) tmp = setTimeout(function() {fadeOut(no);}, timeOut);
                        else triggerMe(no);
                    });
                } else {
                    $(itemsSpan[no]).slideDown((timeOut/6), function() {
                        trans = false;
                        if(!mOver) tmp = setTimeout(function() {fadeOut(no);}, timeOut);
                        else triggerMe(no);
                    });
                }
            });
        }
        
        var triggerMe = function(no) {
            if(mOver) {tmp = setTimeout(function() {triggerMe(no);}, (timeOut/4));}
            else {
                clearTimeout(tmp);
                tmp = setTimeout(function() {fadeOut(no);}, (timeOut/2));
            }
        }
        
        var fadeOut = function(no) {
            trans = true;
            if($(itemsSpan[no]).css('bottom') == 0) {
                $(itemsSpan[no]).slideDown((timeOut/6), function() {
                    $(items[no]).fadeOut((timeOut/6), function() {
                        fadeIn(calcNext(no));
                    });
                });
            } else {
                $(itemsSpan[no]).slideUp((timeOut/6), function() {
                $(items[no]).fadeOut((timeOut/6), function() {
                        fadeIn(calcNext(no));
                    });
                });
            }
        }
        
        var calcNext = function(no) {
            return Math.floor(Math.random() * items.length);
            //return ((no + 2) == items.length) ? 0 : (no + 1);
        }
        
        fadeIn(no);

    };  

})(jQuery);  
