jQuery(document).ready(function() {
	function smartColumns() {
        if ($("ul.column")){
        $("ul.column").css({ 'marginRight' : "0px"});
		$("ul.column").css({ 'marginLeft' : "0px"});
		var colWrap = $("ul.column").width();
		var colNum = Math.floor(colWrap /  $("ul.column li").width());
		var restoCol = Math.floor(colWrap %  $("ul.column li").width());
		var anchoCol = Math.floor(restoCol / (colNum-1));
		var colActual=1;
		$("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();
			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-1));
			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();
			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-1));
			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();
			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-1));
			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();
			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-1));
			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();
		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-1));
		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();	
    if ($("ul.column")||$("#featuredProducts ul.column")||$("#specialsDefault ul.column")||$("#crossSell ul.column")||$("#alsoPurchased ul.column")){
        $(window).resize(function () {
    		smartColumns();
		}); 
	}
});
