/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0
	
	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 current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");
        var paused      = false;
        var nextItem    = null;
        var controllerPage  = $('#' + element[0].id + "Controller #" + element[0].id + "Pages");
        var controllerPause  = $('#' + element[0].id + "Controller #" + element[0].id + "Pause");
        controllerPause.click(function(){
            clearTimeout(timeOutFn);
            paused= !paused;
            if(!paused){
              fadeElement(true);
            }
            $(this).toggleClass('active');
        });

        items.each(function(i) {
            $(items[i]).mouseover(function() {
               mOver = true;
            });
            
            $(items[i]).mouseout(function() {
                mOver   = false;
                if(!paused){
                  fadeElement(true);
                  }
            });
            if(!$(items[i]).hasClass('clear')){
              $('<div class="' +element[0].id +'ControllerItem"></div>')
                .click(function(){ 
                  if(! controllerPause.hasClass('active'))
                    controllerPause.click();
                  controllerPage.children().removeClass('active');
                  nextItem=i; 
                  $(this).addClass('active');
                  makeSlider();

                })
                .appendTo(controllerPage);
            }
        });

        var fadeElement = function(isMouseOut) {
            var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if (paused) { thisTimeOut = 10; }
            if(items.length > 0 && !(paused && !faderStat)) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            }
        }
        var setCurrent = function(index) {
        current = items[index];
        }
        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            var newMargin   = $(element).width() * currNo;
            if(faderStat == true) {
                if(!mOver) {
                    $(items[currNo]).fadeIn('slow', function() {
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).fadeOut('slow', function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        } else {
                            $(itemsSpan[currNo]).fadeIn('slow', function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        }
                    });
                }
            } else {
                nextNo = nextItem == null ? currNo +1: nextItem;
                nextNo = nextNo == items.length ? 0 : nextNo;
                nextItem = null;
                if(!mOver) {
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).fadeIn('slow', function() {
                            $(items[currNo]).fadeOut('slow', function() {
                                $(controllerPage.children()[currNo]).removeClass('active');
                                faderStat = true;
                                current = items[nextNo];
                                $(controllerPage.children()[nextNo]).addClass('active');
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    } else {
                        $(itemsSpan[currNo]).fadeOut('slow', function() {
                        $(items[currNo]).fadeOut('slow', function() {
                                $(controllerPage.children()[currNo]).removeClass('active');

                                faderStat = true;
                                current = items[nextNo];
                                $(controllerPage.children()[nextNo]).addClass('active');
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    }
                }
            }
        }
        
        makeSlider();

    };  

})(jQuery);  

