jQuery(document).ready(function() {
	
	function smartColumns() {
        var SecurityPixel=5;
	var MinSecurity=2;
        if ($("ul.column")){
        $("ul.column").css({ 'marginRight' : "0px"});
		$("ul.column").css({ 'marginLeft' : "0px"});
		var colWrap = $("ul.column").width()+SecurityPixel;
		var colNum = Math.floor(colWrap /  $("ul.column li").width());
		var restoCol = Math.floor(colWrap %  $("ul.column li").width());
		var anchoCol = Math.floor(restoCol / (colNum));
		                   	/*Minimo de distancia de seguridad*/
		anchoCol=anchoCol-SecurityPixel;			
		if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/
		
		var colActual=1;
		$("ul.column li").each(function(){
//			alert(colNum);	
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
	
				}
				colActual++;

        }) ;
       }	
     if ($("#productListing ul.column")){
					
			$("#productListing ul.column").css({ 'marginRight' : "0px"});
			$("#productListing ul.column").css({ 'marginLeft' : "0px"});
		
			var colWrap = $("#productListing ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#productListing ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#productListing ul.column li").width());
			var anchoCol = Math.floor(restoCol / (colNum));
			var colActual=1;
			                   	/*Minimo de distancia de seguridad*/
			anchoCol=anchoCol-SecurityPixel;			
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#productListing ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/

			$("#productListing ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
        if ($("#indexCategories ul.column")){
			
			$("#indexCategories ul.column").css({ 'marginRight' : "0px"});
			$("#indexCategories ul.column").css({ 'marginLeft' : "0px"});
		
			var colWrap = $("#indexCategories ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#indexCategories ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#indexCategories ul.column li").width());
			var anchoCol = Math.floor(restoCol / (colNum));	
			anchoCol=anchoCol-SecurityPixel;
			                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#indexCategories ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/

			var colActual=1;
			$("#indexCategories ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
	
	//Aquí montamos las colummnas de los productos relacionados
		if ($("#crossSell ul.column")){
			$("#crossSell ul.column").css({ 'marginRight' : "0px"});
			$("#crossSell ul.column").css({ 'marginLeft' : "0px"});
		
			var colWrap = $("#crossSell ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#crossSell ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#crossSell ul.column li").width());			
			var anchoCol = Math.floor(restoCol / (colNum));
			anchoCol=anchoCol-SecurityPixel;
			                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#crossSell ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/

			var colActual=1;
			$("#crossSell ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
		if ($("#featuredProducts ul.column")){
			$("#featuredProducts ul.column").css({ 'marginRight' : "0px"});
			$("#featuredProducts ul.column").css({ 'marginLeft' : "0px"});
			var colWrap = $("#featuredProducts ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#featuredProducts ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#featuredProducts ul.column li").width());
                        var anchoCol = Math.floor(restoCol / (colNum));
			anchoCol=anchoCol-SecurityPixel;
			                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#featuredProducts ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/
	
			var colActual=1;
			$("#featuredProducts ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
	   	if ($("#whatsNew ul.column")){
			$("#whatsNew ul.column").css({ 'marginRight' : "0px"});
			$("#whatsNew ul.column").css({ 'marginLeft' : "0px"});
			var colWrap = $("#whatsNew ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#whatsNew ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#whatsNew ul.column li").width());
			var anchoCol = Math.floor(restoCol / (colNum));
			anchoCol=anchoCol-SecurityPixel;
                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#whatsNew ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/
			var colActual=1;
			$("#whatsNew ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }   
		if ($("#specialsDefault ul.column")){
			$("#specialsDefault ul.column").css({ 'marginRight' : "0px"});
			$("#specialsDefault ul.column").css({ 'marginLeft' : "0px"});
			var colWrap = $("#specialsDefault ul.column").width()+SecurityPixel;
			var colNum = Math.floor(colWrap /  $("#specialsDefault ul.column li").width());
			var restoCol = Math.floor(colWrap %  $("#specialsDefault ul.column li").width());
			var anchoCol = Math.floor(restoCol / (colNum));
			anchoCol=anchoCol-SecurityPixel;
			                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#specialsDefault ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/

			var colActual=1;
			$("#specialsDefault ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
		//Also purchased
	if ($("#alsoPurchased ul.column")){
        	$("#alsoPurchased ul.column").css({ 'marginRight' : "0px"});
		$("#alsoPurchased ul.column").css({ 'marginLeft' : "0px"});
		var colWrap = $("#alsoPurchased ul.column").width()+SecurityPixel;
		var colNum = Math.floor(colWrap /  $("#alsoPurchased ul.column li").width());
		var restoCol = Math.floor(colWrap %  $("#alsoPurchased ul.column li").width());
		var anchoCol = Math.floor(restoCol / (colNum));
		anchoCol=anchoCol-SecurityPixel;
		                   	/*Minimo de distancia de seguridad*/
			if (anchoCol<MinSecurity){
			   colNum = colNum -1;
			   restoCol = restoCol + $("#alsoPurchased ul.column li").width();
			   anchoCol=Math.floor(restoCol / (colNum));	
                        }
			/**/
	
		var colActual=1;
		$("#alsoPurchased ul.column li").each(function(){
				if (colActual==colNum){
					$(this).css({ 'marginRight' : "0px"});				
					colActual=0;
				}else{
					$(this).css({ 'marginRight' : anchoCol});
				}
				colActual++;
        }) ;
       }
	}	
	
          	
	smartColumns();		
	$(window).resize(function () {
	     if ($("ul.column")||$("#whatsNew ul.column")||$("#featuredProducts ul.column")||$("#specialsDefault ul.column")||$("#crossSell ul.column")||$("#alsoPurchased ul.column")||("#productListing ul.column")||$("#indexCategories ul.column")){ 
    		smartColumns();
		
              }
	}); 
});

