// Make all blocks (divs) on one page the same height as the tallest one (when faux columns can't be used)

function getHeights(category){

	//alert ("gH("+category+")");
	//check for standards compliance
	if(!document.getElementById) return;
	if(!document.getElementsByTagName) return;
	
	var holder;
	
	if (category) {

		// If its a shop listing set heights for all rows.
		var maxH
		if (maxH=getMaxHeight("1")) setHeights("1", maxH)
		if (maxH=getMaxHeight("2")) setHeights("2", maxH);
		if (maxH=getMaxHeight("3")) setHeights("3", maxH);
	}
	
	// Category equal heights
	else {

		// If this is a product page try to set the right column
		//holder = document.getElementById("landing-level");
		
		
		// If its a shop listing set heights for all rows.
		var maxH
		if (maxH=getMaxCatHeight(1)) setCatHeights(1, maxH)
		if (maxH=getMaxCatHeight(2)) setCatHeights(2, maxH);
		if (maxH=getMaxCatHeight(3)) setCatHeights(3, maxH);
	}

}
	

function getMaxCatHeight(row) {

	var maxHeight=0;
	var elementId="landing-level";
	//alert("check id("+elementId+")");
	var holder = document.getElementById(elementId);
	var validRow = false;
	var landingpanel=0;
	
	// Does this page contain this holder???
	if (!holder) return;
	
	var lis = holder.getElementsByTagName("div");
	for(var j = 0; j < lis.length; j++){
		//alert("found div("+j+") class("+lis[j].className+") row("+row+")");
		if(lis[j].className == 'landing-panel') {
			validRow=false;
			landingpanel++;
			//alert ("Found landing panel("+landingpanel+") row("+row+") valid("+validRow+")");
			if ((landingpanel==1 || landingpanel==2) && row==1) validRow = true;
			if ((landingpanel==3 || landingpanel==4) && row==2) validRow = true;
			if ((landingpanel==5 || landingpanel==6) && row==3) validRow = true;

			if (validRow) {

				var thisheight = 0;
				var blocks = lis[j].getElementsByTagName("div");
				for(var i = 0; i < blocks.length; i++){
					//alert("found li("+j+")block("+i+") class("+blocks[i].className+")");
					if(blocks[i].className == 'panelcontent') {
						height = blocks[i].offsetHeight;
						thisheight += height;
					}
					if (thisheight>maxHeight) maxHeight=thisheight;
					//alert ("Summary("+j+") height("+thisheight+") max("+maxHeight+")");
				}
			}
		}
	}
	
	//alert ("GOT MAX("+maxHeight+") for categories("+row+")");	
	return maxHeight;
}

// make all divs the same height in pixels. must be run on window resize, text increase/decrease. (a ballache basically.)
function setCatHeights(row, height){
	//alert ("sCH("+row+", "+height+")");
	var elementId="landing-level";
	var validRow = false;
	var landingpanel=0;
	var holder = document.getElementById(elementId);
	
	// This should really be impossible as the element had
	// to exist for us to have called this function in the first place.
	if (!holder) return ;

	//var blocks = holder.getElementsByTagName("div");

	var lis = holder.getElementsByTagName("div");
	for(var j = 0; j < lis.length; j++){
		//alert("found div("+j+") class("+lis[j].className+") row("+row+")");
		if(lis[j].className == 'landing-panel') {
			validRow=false;
			landingpanel++;
			//alert ("Found landing panel("+landingpanel+") row("+row+") valid("+validRow+")");
			if ((landingpanel==1 || landingpanel==2) && row==1) validRow = true;
			if ((landingpanel==3 || landingpanel==4) && row==2) validRow = true;
			if ((landingpanel==5 || landingpanel==6) && row==3) validRow = true;

			if (validRow) {

				var thisheight = 0;
				var blocks = lis[j].getElementsByTagName("div");
				for(var i = 0; i < blocks.length; i++){
					//alert("found panel("+landingpanel+") block("+i+") class("+blocks[i].className+")");
					if(blocks[i].className == 'panelcontent') {
						thisheight += blocks[i].offsetHeight;
						var exHeight = height - thisheight;
						blocks[i].style.paddingBottom = exHeight+"px";
					}
				}
			}
		}
	}

}




function getMaxHeight(row) {

	var maxHeight=0;
	var elementId="store_list_1";
	//alert("check id("+elementId+") gMH("+row+")");
	var holder = document.getElementById(elementId);
	var validRow = false;
	var landingpanel=0;
	
	// Does this page contain this holder???
	if (!holder) return;
	
	var lis = holder.getElementsByTagName("div");
	for(var j = 0; j < lis.length; j++){
		//alert("found div("+j+") class("+lis[j].className+") row("+row+")");
		if(lis[j].className == 'panelcontent') {
			//alert("found div("+j+") class("+lis[j].className+") row("+row+")");
			validRow=false;
			landingpanel++;
			//alert ("Found landing panel("+landingpanel+") row("+row+") valid("+validRow+")");
			if ((landingpanel==1 || landingpanel==2) && row==1) validRow = true;
			if ((landingpanel==3 || landingpanel==4) && row==2) validRow = true;
			if ((landingpanel==5 || landingpanel==6) && row==3) validRow = true;

			if (validRow) {
				var thisheight = 0;
				height = lis[j].offsetHeight;
				thisheight += height;
				if (thisheight>maxHeight) maxHeight=thisheight;
				//alert ("Summary("+j+") height("+thisheight+") max("+maxHeight+")");
			}
		}
	}
	
	//alert ("GOT PROD MAX("+maxHeight+") for categories("+row+")");	
	return maxHeight;
}




// make all divs the same height in pixels. must be run on window resize, text increase/decrease. (a ballache basically.)
function setHeights(row, height){

	//alert ("sCH("+row+", "+height+")");
	var elementId="store_list_1";
	var validRow = false;
	var landingpanel=0;
	var holder = document.getElementById(elementId);
	
	// This should really be impossible as the element had
	// to exist for us to have called this function in the first place.
	if (!holder) return ;

	//var blocks = holder.getElementsByTagName("div");

	var lis = holder.getElementsByTagName("div");
	for(var j = 0; j < lis.length; j++){
		//alert("found div("+j+") class("+lis[j].className+") row("+row+")");
		if(lis[j].className == 'panelcontent') {
			validRow=false;
			landingpanel++;
			//alert ("Found landing panel("+landingpanel+") row("+row+") valid("+validRow+")");
			if ((landingpanel==1 || landingpanel==2) && row==1) validRow = true;
			if ((landingpanel==3 || landingpanel==4) && row==2) validRow = true;
			if ((landingpanel==5 || landingpanel==6) && row==3) validRow = true;

			if (validRow) {

				var thisheight = 0;
				thisheight += lis[j].offsetHeight;
				var exHeight = height - thisheight;
				//alert("Got exheight for panel("+landingpanel+")");
				
				var blocks = lis[j].getElementsByTagName("div");
				for(var i = 0; i < blocks.length; i++){
					//alert("found panel("+landingpanel+") block("+i+") class("+blocks[i].className+")");
					if(blocks[i].className == 'productData') {
						blocks[i].style.paddingBottom = exHeight+"px";
					}
				}
			}
		}
	}

}

//addEvent(window,"load",getHeights);
