// anchor link scroll coded by atushi terada


window.onload = function () {
	var linkObj = document.getElementsByTagName('a');
	for (i=0; i<linkObj.length; i++) {
		var aHref = linkObj[i].href.split("#");
		if (aHref[1]) {
			linkObj[i].href="javascript:anchorlink('"+aHref[1]+"');";
		}
	}
	var linkObj = document.getElementsByTagName('area');
	for (i=0; i<linkObj.length; i++) {
		var aHref = linkObj[i].href.split("#");
		if (aHref[1]) {
			linkObj[i].href="javascript:anchorlink('"+aHref[1]+"');";
		}
	}
}
function anchorlink(s) {
	currentY = getWinYOffset();
	if(s == 'top') {
		targetY = 0;
	} else {
		targetY = getElementPosition(s)['top'];
	}
	pastY = currentY;
	intervalScroll = setInterval( "motionScroll()", 20 );
}
function motionScroll() {
	if (getWinYOffset() != pastY) {
		if (intervalScroll) clearTimeout(intervalScroll);
		return;
	}
	currentY = currentY + ( (targetY - currentY) / 5 );
	window.scrollTo(0,currentY);
	if (currentY - 1 < targetY && targetY < currentY + 1 ) {
		if (intervalScroll) clearTimeout(intervalScroll);
	}
	pastY = getWinYOffset();
}
function getWinYOffset() {
	if (window.scrollY) return window.scrollY;
	if (window.pageYOffset) return window.pageYOffset;
	if (document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	} else if (document.body && document.body.scrollTop) {
		return document.body.scrollTop;
	}
	return 0;
}
function getElementPosition(element) {
	var offsetTrail = (typeof element == 'string') ? document.getElementById(element) : element;
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft; offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin; offsetTop += document.body.topMargin;
	}
	return ({left: offsetLeft, top: offsetTop});
}
