/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.1 Special Edition for Mr. Mészáros Róbert
	
	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 || 4000;
        var moPause      = vars.mouseOverPause || true;
        var imgLoad      = vars.imgLoadWait || false;
        var imgWaitText  = vars.imgWaitText || 'Loading images..';
        var capOpacity   = vars.captionOpacity || .7;
        var pauseVal     = 'pause';
        var trans        = false;
        var no           = 0;
        var mOver        = false;
        var next         = null;
        var prev         = null;
        var items        = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsCaption = $("#" + element[0].id + "Content ." + element[0].id + "Image div");
        var tmp          = null;

        itemsCaption.css({"opacity":capOpacity});
        
        items.each(function(i) { 
            if(moPause) {
                $(items[i]).mouseover(function() {
                   mOver = true;
                    pauseVal    = 'play';
                });                
                $(items[i]).mouseout(function() {
                    mOver   = false;
                    pauseVal    = 'pause';
                });
            }
            $(items[i]).css({"opacity":0});
            $(itemsCaption[i]).css({"display":"none"});
        });
        
        var fadeIn  = function(no) {
            no = no;
            items.css({"z-index":1});
            items.each(function(i) {
                if(i != no) {
                    $(items[i]).css({"opacity":0});
                }
            });
            $(items[no]).css({"z-index":6,"opacity":.1});
            trans = true;
            $(items[no]).animate({
                "opacity": 1
            },(timeOut/6), function() {
                if($(itemsCaption[no]).css('bottom') == 0) {
                    $(itemsCaption[no]).slideUp((timeOut/6), function() {
                        trans = false;
                        if(!mOver) tmp = setTimeout(function() {fadeOut(no);}, timeOut);
                        else triggerMe(no);
                    });
                } else {
                    $(itemsCaption[no]).slideDown((timeOut/6), function() {
                        trans = false;
                        if(!mOver) tmp = setTimeout(function() {fadeOut(no);}, timeOut);
                        else triggerMe(no);
                    });
                }
            });
        }
        
        var fadeOut = function(no) {
            trans = true;
            if(mOver) {
                triggerMe(no);
                return false;
            }
            if($(itemsCaption[no]).css('bottom') == 0) {
                $(itemsCaption[no]).slideDown((timeOut/6), function() {
                    $(items[no]).animate({
                        "opacity":.15
                    },(timeOut/6), function() {
                        if(next != null) {
                            no      = next;
                            next    = null;
                            fadeIn(no);
                        } else {
                            fadeIn(calcNext(no));
                        }
                    });
                });
            } else {
                $(itemsCaption[no]).slideUp((timeOut/6), function() {
                    $(items[no]).animate({
                        "opacity":.15
                    },(timeOut/6), function() {
                        if(next != null) {
                            no      = next;
                            next    = null;
                            fadeIn(no);
                        } else {
                            fadeIn(calcNext(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 calcNext = function(no) {
            return ((no + 2) == items.length) ? 0 : (no + 1);
        }
        
        var imagesLoaded = function(a) {
            if(a == images.length) {
                $(".txtNotifyer").remove();
                fadeIn(no);
            }
        }
        
        if(imgLoad) {
            var a = 1;
            var images      = $("#" + element[0].id + "Content ." + element[0].id + "Image img");
            var thisWidth   = $("#" + element[0].id+"").width();
            var thisHeight  = $("#" + element[0].id+"").height();
            $("#" + element[0].id).append("<div class='txtNotifyer' style='z-index:999;width:"+thisWidth+"px; height:"+thisHeight+"px;margin-top:"+(thisHeight/3)+"px;margin-left:"+(thisWidth/3)+"px;'>" + imgWaitText + "</div>");
            images.each(function(i) {
                $(images[i]).load(imagesLoaded(++a));
            })
        } else {
            fadeIn(no);
        }

    };  

})(jQuery);  