/* Category Page Javascript */

/* Load-time stuff */


$(window).load(function() {
	//Resize the product links
	//NOTE intentionally moved into $(window).load as this executes later, after CSS and JS and images have fully loaded
	resizeProductLinks();
});


/**
 * Resize all the product links and images
 */
function resizeProductLinks() {
	//Get the products array
	var products=$("li.product");

	if (products.length>0) {

		//Find how wide a product is
		var productWidth=$(products[0]).attr("offsetWidth");
		var viewportWidth=$($("div.category")[0]).attr("offsetWidth");
		
		//Work out how many on a row
		var productsPerRow=parseInt(viewportWidth/productWidth);
		
		//Iterate through the array getting heights
		var col=0;
		var row=0;
		var maxHeaderHeight=0;
		var maxImageHeight=0;
		
		//Get the no product image height
		var noProductImageHeight=36; //A default!
		if ($("div.noProductImage").length>0) {
			$($("div.noProductImage")[0]).removeAttr("height");
			noProductImageHeight=$($("div.noProductImage")[0]).height();
		}
		for (i=0;i<products.length;i++) {
			//Get jquery object for product
			var prod=$(products[i]);	
			var id=$(products[i]).attr("id");
			var productId=id.substr(7);
			
			//Inc the column number
			++col;
			
			if (col>productsPerRow) {
				//End of row - set the heights
				$("li.row"+row+" h3").css("height",maxHeaderHeight);
				var combinedHeight=parseInt(maxHeaderHeight)+parseInt(maxImageHeight)+10;
// 				if (jQuery.browser.safari) combinedHeight+=68; //A hack for chrome and safari!
				$("li.row"+row).css("height",combinedHeight+"px");
	
				//Reset everything
				++row;
				col=1;
				maxHeaderHeight=0;
				maxImageHeight=0;
			}
			
			//Get the height of the heading
			$('#'+id+' h3').removeAttr("height");
			var headerHeight=$('#'+id+' h3').height();
			if (headerHeight>maxHeaderHeight) maxHeaderHeight=headerHeight;
	
			//Get the height of the image
			$('#preview'+productId).removeAttr("height");
			var imageHeight=$('#preview'+productId).height();
			if (!imageHeight) imageHeight=noProductImageHeight;
	
			//We have a real image - use that
			if (imageHeight>maxImageHeight) maxImageHeight=imageHeight;
			
			//Set the class for that row
			prod.addClass("row"+row);
		}
		
		//End of list - set the heights
		$("li.row"+row+" h3").css("height",maxHeaderHeight);
		var combinedHeight=parseInt(maxHeaderHeight)+parseInt(maxImageHeight)+10;
// 		if (jQuery.browser.safari) combinedHeight+=68; //A hack for chrome and safari!
		$("li.row"+row).css("height",combinedHeight+"px");
	}	
}