(function($){
	
	$.fn.columns = function(){
	    
	    var columns_number = 4;
	    
	    return this.each(function(){
	       
	       var $wrapper = $(this);
	       var $list = $wrapper.find("ul").first();
	       var $list_items = $list.children();
	       
	       var totalHeight = $list.height();
	       var minHeight = totalHeight/columns_number;
	       
	       var columns = new Array();
	       
	       for( var i =0; i < columns_number; i++ ){
	           columns.push($("<div class='col' ></div>"));
	       }
	       
	       var currentColumn=0;
	       var lastTop = 0;
	       
	       $list_items.each(function(){
	           
	           var $list_item = $(this);
	           
	           if($list_item.position().top > (currentColumn +1)* minHeight ) {
	               
	               currentColumn+=1;
	               
	           }
	           
	           columns[currentColumn].append($list_item.clone());
	           
	       });
	       
	       for( var i = 0 , max = columns.length; i < max ; i++ ){
	           
	           $wrapper.append(columns[i]);
	       }
	       
	       $list.remove();
	       
	       $wrapper.hide().css("height","auto").slideDown(500);
	       
	    });
	    
	}
	
	$.fn.slideshow = function( options ){
	
		var settings = {
			speed : 300,
			duration : 5000
		}
		
		var opts = $.extend( settings, options );
		
		return this.each(function(){
			
			var $slides = $(this).find("div.slide"),
				total = $slides.size(),
				current = 0;
			
			
			function next(){
			
				var nextSlide = current + 1;
				
				if( nextSlide == total ){
				
					nextSlide = 0;
				}
				
				$slides.eq(current).fadeOut(opts.speed);
				$slides.eq(nextSlide).fadeIn(opts.speed);
				
				current = nextSlide;
				
				queue();
			}
			
			function queue(){

				setTimeout(function(){
					next();
				},opts.duration);
				
			}
			
			$slides.eq(0).show();
			$slides.slice(1).hide();
			
			if(total > 1)
				queue();
		});
	};
	
	$(window).load(function(){
		
		$("#slideshow").slideshow({
			speed : 1000,
			duration : 10000
		});
	    
	    $("div.product-line").columns();
	    
	});
})(jQuery);
