/*	scrollMenu by Stewart Orr
		Moves the left hand menu to where you have scrolled on 
		the page. If browser window is too short to fit the contents
		it will not scroll.
		2006.6.22 - added smooth animation
*/
function getPageOffsetLeft(el) {
	return (el.offsetParent != null) ? el.offsetLeft + getPageOffsetLeft(el.offsetParent) : el.offsetLeft;
}
function getPageOffsetTop(el) {
	return (el.offsetParent != null) ? el.offsetTop + getPageOffsetTop(el.offsetParent) : el.offsetTop;
}

var menu, startPos, menuInterval;

window.onresize = function resizeScreen() {
	clearTimeout(menuInterval);
	scrollMenu();
}

window.onload = function () {
	// initialise menu
	menu = document.getElementById("sub_content");
	startPos = getPageOffsetTop(menu);
	scrollMenu();
}

function scrollMenu() {
	var scrollView = (window.innerHeight) ? window.innerHeight :  document.documentElement.clientHeight;
	var currentPos = getPageOffsetTop(menu)-startPos;
	var targetPadding = 15;
	
	// should we scroll menu
	if (menu.clientHeight+targetPadding < scrollView-(startPos/2)) {
		if (window.innerHeight) {
			targetPos = window.pageYOffset-startPos;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			targetPos = document.documentElement.scrollTop-startPos;
		} else if (document.body)	{
			targetPos = document.body.scrollTop-startPos;
		}
		targetPos+=targetPadding;
		
		//is target position higher that it should be?
		if (targetPos<0) {
			targetPos = 0;
		} else {		
			if (currentPos<targetPos) {
				targetPos = Math.round(currentPos + ((targetPos-currentPos)*0.3));
			} else {
				targetPos = Math.round(currentPos - ((currentPos-targetPos)*0.3));
			}
		}
		//debug
		//window.status = "currentPos="+currentPos + ", startPos="+startPos+", targetPos="+targetPos;	
		menu.style.top = targetPos+"px";
		menuInterval = setTimeout('scrollMenu()',5);
	}	else {
		//reset it's position
		menu.style.top = "0px";
	}
}