/* ------------------------------------------------------------------------
	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.slider = function(opts) {       
        
        var t    		= this;
		var first 		= true;
		var mOver		= false;
		var mClick		= false;
		var swapOn_mOut = false;
		var timeOut		= (opts.timeOut != undefined ? opts.timeOut : 5000);
        var current		= null;
		var curIndex 	= null;
		var items		= $(t).find(".content .image");
		
		
		$(t).mouseover(function() {
			mOver = true;
		
			$(t).find("ul.browse").css("opacity",1);
		});
		
		$(t).mouseout(function() {
			mOver = false;
			
			if (swapOn_mOut) { run(timeOut/4); }
			
			swapOn_mOut = false;
		
			$(t).find("ul.browse").css("opacity",0.5);
		});
		
		
		var browse = $('<ul class="browse"></ul>');
		
        items.each(function(i) {
    		
			browse.append($('<li><a href="#">'+ (i+1) +'</a></li>'));
        });
		
		$(t).append(browse);
		
		$(t).find("ul.browse a").click(function() {
			
			mClick = true;
			
			if ($(this).hasClass("active")) { return false; }
			
			curIndex = $(this).html() - 1;
			
			$(t).find("span").clearQueue().stop(true,true);
			$(t).find(".image").clearQueue().stop(true,true);
			
			$(current).find("span").hide(0,function() {
				$(current).hide(0,function() {
					
					slidein(curIndex);
				})
			});
		});
		
		$(t).find("ul.browse").css("opacity",0.5);
		
		var thumb = $(t).find("ul.browse li")
		
        var run = function(thisTimeOut) {
			
			if (!mClick) {
				
				setTimeout(slide, thisTimeOut);
			}
			
        }
		
		var sleep = function() {
			
		}
		
		var slidein = function(index) {
			
			$(items[index]).fadeIn(300,function() {
				
			$(thumb).find('a').removeClass('active');
			$(thumb[curIndex]).find('a').addClass('active');
				
				$(items[index]).find("span").slideDown(300,function() {
					current = items[index];
					
					if (!mClick) {
						run(timeOut);
					}
				});
			});
		}
        
        var slide = function() {
			
			if (mClick) { return false; }
			
			if (mOver) {
				swapOn_mOut = true;
				return;
			}
			
			curIndex = (curIndex == null ? 0 : curIndex+=1 );
			
			if (curIndex >= items.length) { curIndex = 0; }
			
			if (current != null) {
				
				if (items.length <= 1) { return; }
				
				$(current).find("span").slideUp(500,function() {
					$(current).fadeOut(500,function() {
						
						slidein(curIndex);
					})
				});
			
			} else {
				
				slidein(curIndex);
			}
        }
        
        run(0);

    };  

})(jQuery);  


$(document).ready(function(){
	
	jQuery.fx.interval = 25;
	
	$('#slider').slider({
      timeOut: 5000
   	});
});
