/* ------------------------------------------------------------------------
	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 : 5000;
        var fade	= 4000;
        var current     = null;
        var timeOutFn   = null;
        var jump 	= null;
        var lastNode	= 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 nivoControl = $('<div class="nivo-controlNav"></div>');
		
		$('#sliderNav').append(nivoControl);
		for(var i = 0; i < (items.length - 1); i++){
			var child = items.eq(i);
			if(!child.is('img')){
				child = child.find('img:first');
			}
            	nivoControl.append('<a class="nivo-control" rel="'+ i +'"><img src="'+ child.attr('rel') + '" alt="" /></a>');
        }
        
        $('.nivo-controlNav a').live('click', function(){
					if($(this).hasClass('active')) return false;
					//clearInterval(timeOut);
					//timeOut = '';jump = parseInt(jump);
					jump = parseInt($(this).attr('rel'));
					//faderStat = true;
					timeOut=0;
					fadeElement();
					timeOut=5000;				
				});
	

            
        items.each(function(i) {
    
            $(items[i]).mouseover(function() {
               mOver = true;
            });
            
            $(items[i]).mouseout(function() {
                mOver   = false;
                fadeElement(true);
            });
            
        });
        
        var fadeElement = function(isMouseOut) {
            var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if(items.length > 0) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            } else {
                console.log("Poof..");
            }
        }
        
        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            //$('.nivo-controlNav a:eq('+  ( (currNo == 0) ? 3 : currNo-1 ) +')').removeClass('active');
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            
            var newMargin   = $(element).width() * currNo;
            
            if(faderStat == true) {
                if(!mOver) {
                	
                    $(items[currNo]).fadeIn((timeOut/6), function() {
                    
                    	$('.nivo-controlNav a:eq('+ currNo +')').addClass('active');
                    	//$('.nivo-controlNav a:eq('+  ( (currNo == 0) ? 3 : currNo-1 ) +')').removeClass('active');
                    	$('.nivo-controlNav a:eq('+  (lastNode) +')').removeClass('active');
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).slideUp((timeOut/6), function() {                               
                                faderStat = false;
                                current = items[currNo];
                                timeOut=5000;
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        } else {

                            $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            	
                                faderStat = false;
                                current = items[currNo];
                                timeOut=5000;
                                if(!mOver) {
                                    fadeElement(false);
                                }
                                
                            });

                        }
                    });
                }
            } else {
                if(!mOver) {
                
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
                                lastNode = currNo;
                                current = items[(currNo+1)];
                                if (jump != null) {
            						current = items[(jump)];
            						jump=null;
               					}
                                if(!mOver) {
                                    fadeElement(false);
                                }
                                 
                            });
                        });
                             
                    } else {
               
                        $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                        $(items[currNo]).fadeOut((timeOut/6), function() {
                        $('.nivo-controlNav a:eq('+ currNo +')').addClass('active');
                        $('.nivo-controlNav a:eq('+  (lastNode) +')').removeClass('active');
                                faderStat = true;
                                lastNode = currNo;
                                current = items[(currNo+1)];
                                if (jump != null) {
            						current = items[(jump)];
            						jump=null;
            					}
                                if(!mOver) {
                                    fadeElement(false);
                                }
                                
                            });
                        });
                    }
                }
            }
        }
        
        makeSlider();

    };  

})(jQuery);  
