// JQUERY FUNCTION

// Remove margin right every 3 products
function removeMarginFromList(target,numberByRow){
	
	var numberLi = $(target).length;
	var j=0;
	var k=0;
	for(i=0;i<numberLi;i++)
	{
		j++;
		if(j==numberByRow)
		{
			k++;
			var li = (j*k)-1;
			$(target+":eq("+li+")").css({ "margin-right":"0" });
			j=0;
			//alert(li);
		}
	}
}

function showHide(action,target){
	if(action == 'show')
	{
		$(target).css({ display:"block"});
	}
	else
	{
		$(target).css({ display:"none"});
	}
}

function alignListElements(container,tag,targetHeight,nbItemARow) // (".products","li",".title",3)
{
	var nbElements = $(container+" "+tag).size();
	//alert(nbElements);

	for(i=0;i<=nbElements;i=i+nbItemARow) 
	{
		var j=i;
		maxHeight = 0;
		while(j<(nbItemARow+i))
		{
			var heightItem = $(container+" "+tag+":eq("+j+") "+targetHeight).height();
			
			if(heightItem > maxHeight)
			{
				var maxHeight = heightItem;
			}
			j++;
		}
		
		var j=i;
		while(j<(nbItemARow+i))
		{
			$(container+" "+tag+":eq("+j+") "+targetHeight).height(maxHeight);
			j++;
		}

	}
}


$(window).load(function(){

});

jQuery(document).ready(function($){
																
	alignListElements(".products","li",".title",3)
	
	$(".mainContent h1:eq(0)").css({ marginTop:"0" });
	$(".mainContent h2:eq(0)").css({ marginTop:"0" });
	$("#submenu a:eq(0)").css({ border:"0","padding-left":"0" });
	$(".mainContent h1 + h2").css({  marginTop:"0" });
	$(".mainContent h1 + h2").prev().css({ marginBottom:"0" });
	$(".mainContent p + ul").prev().css({ marginBottom:"0.5em" });
	
	
	// Remove Margin Right every 3 products
	removeMarginFromList(".categories .catalogue li",3);
	removeMarginFromList(".categories .products li",3);
	removeMarginFromList(".fabric li",3);

	$(".mainContent li").wrapInner("<span></span>");
	
	$(".categories li").wrapInner("<span></span>");


	// IMAGE WITH CLASS PICTURE WILL WORK WITH THICKBOX
	$("img.picture, img[longdesc]").each(function(){		
  	//var stop = $(this).attr("src").indexOf("-256_x.jpg");
		//var sublink = $(this).attr("src").substring(0,stop);
		
		var rel = $(this).attr("rel");
		var longdesc = $(this).attr("longdesc");
		
		if(longdesc =='' || longdesc ==undefined)
		{
			var link = rel;
		}
		else
		{
			var link = longdesc;
		}
		
		$(this).wrap("<a href=\""+link+"\" class=\"thickbox\"></a>");
		$(this).removeAttr("title");
	});
	
	// PAGE NAVIGATION SHOW HIDE
	var page = $("#pages li").size();
	if(page < 6)
	{
		$("#pages").remove();
	};

	
	// TARGET BLANK XHTML STRICT
	$('a[@rel$="external"]').click(function(){
	
		this.target = "_blank";
	
	});
	
	
	// Delete picture whitout src
	$("img").each(function() {
		var src = $(this).attr("src");
		
		if(src == undefined)
		{
			$(this).css({display:"none"});
		}
	});
	
});

function noError(){return true;}
window.onerror = noError;