// actions to be performed when the DOM is loaded and can be manipulated

$(document).ready(function () {
	equaliseProductHeights();
});

function equaliseProductHeights() {
	$("ul#productList li:nth-child(3n-2)").addClass("first"); 						// apply class 'first' to first of every 3 list items
	$("ul#productList li:nth-child(3n)").after("<div class='hr'></div>"); 			// add clear div after every 3 list items

	var numRows = Math.ceil( $("ul#productList li").length / 3 ); 					// find number of rows
	// alert(numRows + " rows");
	// var row = 27;
	for (var row = 1; row <= numRows; row++) { 										// iterate through rows
	
		var start = (row * 3) - 3; 													// index of first element in row
		var end = (row * 3);														// index of last element in row
		// alert(row + " : " + start + " : " + end);
		
		var prodImgMinHeight = 20; 													// minimum height of img
		var prodNameMinHeight = 0;  												// minimum height of span.prodName 
		
		var $prodImgRow = $("ul#productList li img").slice(start,end);				// jQuery object filtered by slicing out row
		$prodImgRow.each(function (i) {												// iterate through each element
			prodImgHeight = $(this).height();										// find height of element
			// alert("prodImgHeight: " + prodImgHeight);
			if (prodImgHeight > prodImgMinHeight) {									// increase minimum height if height is greater
				prodImgMinHeight = prodImgHeight;
			}
		});
		// alert("prodImgMinHeight: " + prodImgMinHeight);
		$prodImgRow.each(function (i) {												// iterate through each element
			prodImgHeight = $(this).height();										// find height of element
			var prodImgMargin = (prodImgMinHeight - prodImgHeight) / 2;				// calculate margin
			// alert("prodImgMargin: " + prodImgMargin);
			$(this).css("margin-bottom",prodImgMargin + 7);								// apply margin
			$(this).css("margin-top",prodImgMargin);								// apply margin
		});
		
		var $prodNameRow = $("ul#productList li span.prodName").slice(start,end); 	// repeat for span.prodName
		$prodNameRow.each(function (i) {
			prodNameHeight = $(this).height();
			if (prodNameHeight > prodNameMinHeight) {
				prodNameMinHeight = prodNameHeight;
			}
		});
		$prodNameRow.height(prodNameMinHeight);
	}
}

function RedirectToSearch(dat){
	 window.location = '/pages/stocksearch.aspx?keyword=' + dat
	}
	
function GoHere(pMessage,pLink){
    
    var vRet; 
    vRet = confirm(pMessage);
    if (vRet==true) {
    window.location = pLink;
    }
}
