/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.01 (Modifyed version)
	
	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 prevBtn     = (vars.prevBtn != undefined) ? vars.prevBtn : '';
        var nextBtn     = (vars.nextBtn != undefined) ? vars.nextBtn : '';
        var useNav      = (vars.useNav != undefined) ? vars.useNav : false;
        var useThumbs   = (vars.useThumbs != undefined) ? vars.useThumbs : false;
        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.each(function(i) { 
            if(moPause) {
                $(items[i]).mouseover(function() {
                   mOver = true;
                });
                $(items[i]).mouseout(function() {
                    mOver   = false;
                });
            }
        });
        if(useNav) $("#" + element[0].id + "Navigation").append("<div id='" + element[0].id + "Prev'><a href='javascript:void(0);'>" + prevBtn + "</a></div><div id='" + element[0].id + "Next'><a href='javascript:void(0);'>" + nextBtn + "</a></div>");
       // if(useNav) $("#" + element[0].id + "Navigation").append("<div id='" + element[0].id + "Next'><a href='javascript:void(0);'>" + nextBtn + "</a></div><div id='" + element[0].id + "Prev'><a href='javascript:void(0);'>" + prevBtn + "</a></div>");
        var fadeIn  = function(no) {
            no = no;
            var itemsThumb  = $("#" + element[0].id + "Thumbs a.thumb img");
            itemsThumb.each(function(i) {
                $(itemsThumb[i]).hover(function() {
                    next = i;
                    if(trans == false) {
                        clearTimeout(tmp);
                        tmp = setTimeout(function() {fadeOut(no);},30);
                    }
                    mOver = true;
                }, function() {
                    mOver = false;
                });
            });
            if(useNav) {
                $("#" + element[0].id + "Next").click(function() {
                    next = ((no - 1) < 0) ? (items.length - 2) : (no - 1);
                    if(trans == false) {
                        clearTimeout(tmp);
                        tmp = setTimeout(function() {fadeOut(no);},30);
                    }
                });
                
                $("#" + element[0].id + "Prev").click(function() {
                    next = ((no + 2) == items.length) ? 0 : (no + 1);
                    if(trans == false) {
                        clearTimeout(tmp);
                        tmp = setTimeout(function() {fadeOut(no);},30);
                    }
                });
            }
            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() {
                        if(next != null) {
                            no      = next;
                            next    = null;
                            fadeIn(no);
                        } else {
                            fadeIn(calcNext(no));
                        }
                    });
                });
            } else {
                $(itemsSpan[no]).slideUp((timeOut/6), function() {
                $(items[no]).fadeOut((timeOut/6), function() {
                        if(next != null) {
                            no      = next;
                            next    = null;
                            fadeIn(no);
                        } else {
                            fadeIn(calcNext(no));
                        }
                    });
                });
            }
        }
        
        var calcNext = function(no) {
            return ((no + 2) == items.length) ? 0 : (no + 1);
        }
        
        fadeIn(no);

    };  

})(jQuery);  