function unifyProductTableHeights(cells)
{
	// size the product grid cells per row
	$(".product_grid > tbody > tr").each(function(x){
		if(!$(this).data("calculated"))
		{
			if(!cells) var ic = ".product_image";
			else var ic = cells; // cells
			var c = $(this).find(ic);
			var mH = 0;
			var p = 20;
			
			if(c.length > 0)
			{
				var h = new Array();
				for(var i=0; i < c.length; i++) { h[i] = $(c[i]).height(); }
				mH = Math.max.apply(null, h);
			}
			
			if(mH > 0)
			{
				var csstxt = "height: " + (mH+p) + "px !important;";
				c.css("cssText", csstxt);
			}
			
			$(this).data("calculated", true);
		}
	});	
}

jQuery(document).ready(function()
{
	/*
	$(".category_image_link").hover(
		function(){
			if(!$(this).data("timer"))
			{
				var z=$(this);
				var img=z.find("img");
				var txt=$("#description_"+z.attr("rel")).text();
				var tip=$('<div class="product_grid_tooltip"><p>'+txt+"</p></div>");
				var timerId=setTimeout(function(){
					z.data("timer", null);
					$("body").append(tip);
					$(tip).hide().css({left: img.offset().left + img.width() + 1, top: img.offset().top}).fadeIn("fast");
				}, 666);
				$(this).data("timer", timerId);
			}
		},
		function(){
			var t;
			t=$(this).data("timer");
			if(t) clearTimeout(t);
			$(this).data("timer", null);
			$(".product_grid_tooltip").fadeOut("fast",function(){$(this).remove()});
			
		}
	);
	*/
	
	// insert expand-buttons to toplevel categories
	// and also add proper class if it's opened (change to minus sign)
	// .catlv1.catkids
	$(".catlv1").each(function(){
		$(this).prepend(($(this).hasClass("item-active"))? "<span class=\"expand_icon opened\"></span>":"<span class=\"expand_icon\"></span>");
	});
	
	$(".expand_icon").click(
		function()
		{
			$(this).parent().children("ul").slideToggle("medium");
			$(this).parent().removeClass("item-active");
			$(this).toggleClass("opened");
		}
	);
	
	// more clickable
	$("table.product_grid td.product_image").click(
		function()
		{
			var url = $(this).find("a").attr("href");
			if(url) window.location = url;
		}
	);
	
	$("table.product_grid td.product_image").css("cursor","pointer");
	$(window).load(unifyProductTableHeights());
});