﻿// ### ページトップへ関数
// backToTop()
function backToTop(pos,now) {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = y4 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	if (!pos) {
		//ページトップへ
		window.scrollTo(Math.floor(x / 1.25), Math.floor(y / 1.25));
		if (x > 0 || y > 0) {
			window.setTimeout("backToTop()", 25);
		}
	} else {
		//引数に入れたid名の場所へ(y軸限定)
		if (window.opera) {
			//でもOpera9未満は除外
			var usrAge = navigator.userAgent;
			var posStart = usrAge.indexOf("Opera",0);
			var posEnd = usrAge.indexOf(" ",posStart+6);
			if (posEnd <= 0) posEnd = usrAge.length;
			var AgeVer = usrAge.substring(posStart+6,posEnd);
			if (AgeVer < 9) {
				window.open("#"+pos,"_self")
				return false;
			}
		}
		var isMSIE = /*@cc_on!@*/false;
		var targetEle = document.getElementById(pos);

		while( targetEle ){
			y4 += targetEle.offsetTop;
			targetEle = targetEle.offsetParent;
			//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
			if ((targetEle) && (isMSIE)) {
				y4 += (parseInt(getElementStyle(targetEle,"borderTopWidth","border-top-width")) || 0);
			}
		}

		var y5 = y4-y;
		y += Math.floor(y5 / 5);
		window.scrollTo(x, y);
		if ((!now || now != y)&&(y > y4-5 || y < y4-5)) {
			window.setTimeout('backToTop("'+pos+'","'+y+'")', 25);
		}
	}
}
function getElementStyle(targetElm,IEStyleProp,CSSStyleProp) {
	var elem = targetElm;
	if (elem.currentStyle) {
		return elem.currentStyle[IEStyleProp];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(elem,"");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
}