var lastBottom;

function matchHeight(when) {
	var arrObj, maxBottom;
	// array for divs to match heights
	arrObj = [ "nav", "content", "sidebar" ];
	
	//find div with tallest height
	if(when=="after") {
		setObjHeights(arrObj,lastBottom - 1);
		resetHeights(arrObj);
	}
	maxBottom = getMaxBottom(arrObj);
	
	//set heights
	setObjHeights(arrObj,maxBottom);
	if(when == "onload") {
		clearInterval( idBefore );
		if (window.removeEventListener){ 
			window.removeEventListener("load", matchHeight, false); 
		} else if (window.detachEvent){ 
			window.detachEvent("onload", matchHeight); 
		}
		setTimeout("matchHeight('after')",1000);
	}
	lastBottom = maxBottom;
}

function resetHeights(arr) {
	for(var i = 0, myItem; myItem = arr[ i ]; i++) {
		d = document.getElementById( myItem );
		if(d) {
			if(d.style.height) {
				d.style.height = "auto";
			}
		}
	}	
}

function getMaxBottom(arr) {
	var d, objBottom, maxBottom = 0;
	for(var i = 0, myItem; myItem = arr[ i ]; i++) {
		d = document.getElementById( myItem );
		if(d) {
			// find bottom
			objBottom = getHeight(d) + getTop(d);
			// calculate maximum bottom
			maxBottom = Math.max(maxBottom, objBottom);
		}
	}
	return maxBottom;
}

function getHeight(obj) {
	if(obj.offsetHeight) {
		objHeight = obj.offsetHeight;
	} else if(obj.style.pixelHeight) {
		objHeight = obj.style.pixelHeight;
	}
	return objHeight;
}

function setObjHeights(arr,b) {
	var d, h;
	for( var i=0, myItem; myItem = arr[ i ]; i++ ) {
		d = document.getElementById( myItem );
		if (d) {
			h = b - getTop(d);
			if (!d.myH) d.myH = 0
			if (h != d.myH) {
				d.style.height = h + 'px';
				d.myH = h;
				nowB = getTop(d) + getHeight(d);
				newH = h - (nowB - b);
				if(newH != h) d.style.height = newH + 'px';
			}
		}
	}
}

function getTop(obj) {
	var objTop = 0;
	originalObj = obj;
	if(obj.offsetParent) {
		while(1) {
			objTop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		objTop += obj.y;
	}
	return objTop;
}

var idBefore = setInterval("matchHeight('before')",500);
if (window.addEventListener){ 
	window.addEventListener("load", function(){matchHeight("onload")}, false); 
} else if (window.attachEvent){ 
	window.attachEvent("onload", function(){matchHeight("onload")}); 
}