function currentYPosition() {
	// Firefox, Chrome, Opera, Safari
	if (window.pageYOffset) return window.pageYOffset;
	// Internet Explorer 6 - standards mode
	if (document.documentElement && document.documentElement.scrollTop)
	return document.documentElement.scrollTop;
	// Internet Explorer 6, 7 and 8
	if (document.body.scrollTop) return document.body.scrollTop;
	return 0;
}

function elmYPosition(eID) {
	var elm = document.getElementById(eID);
	var y = elm.offsetTop;
	var node = elm;
	while (node.offsetParent && node.offsetParent != document.body) {
		node = node.offsetParent;
		y += node.offsetTop;
	}
	return y;
}

var scrollY = 0;
var scrollTarget = "";

function smoothScroll(eID) {
	scrollTarget = elmYPosition(eID);
	scrollY = currentYPosition();
	doScroll();
}

function doScroll() {
	if (!isNaN(scrollTarget)) {
		scrollTarget = Math.max(0, Math.min(maxYPosition(), scrollTarget));
		var currentY = currentYPosition();
		if (Math.round(scrollY) == currentY) {
			var distance = scrollTarget - scrollY;
			var step = distance * 0.1;
			if (Math.abs(step) < 0.01) {
				scrollY = scrollTarget;
			} else {
				scrollY = scrollY + step;
			}
			window.scrollTo(0, Math.round(scrollY));
			if (scrollY != scrollTarget) {
				setTimeout("doScroll()", 10);
			}
		}
	}
}

function maxYPosition()
{
  return getDocumentHeight() - getWindowHeight();
}

function getWindowHeight() {
  var winHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    winHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    winHeight = document.body.clientHeight;
  }
  return winHeight;
}

function getDocumentHeight() {
	return Math.max(
		Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
		Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
		Math.max(document.body.clientHeight, document.documentElement.clientHeight)
	);
}