﻿
var timeID = null;
var maxX = 0;
var maxY = 0;
var ScrollSpeed = 20;
var saveScPos;

// 間違いのないように全文字列を小文字に変換
var agt = navigator.userAgent.toLowerCase();
var is_ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie7 = (agt.indexOf("msie 7") != -1);
var is_ns = (agt.indexOf("netscape") != -1);
var is_gecko = (agt.indexOf('gecko') != -1);
var is_opera = (agt.indexOf("opera") != -1);

function clearTime()
{
	clearInterval(timeID);
	timeID = null;
}


//要素のスタイル属性を取得する関数
function getElementStyle(targetElem,IEStyleProp,CSSStyleProp)
{
	var elm = targetElem;

	if(elm.currentStyle){
		return elm.currentStyle[IEStyleProp];

	}else if(window.getComputedStyle){
		var compStyle = window.getComputedStyle(elm,"");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
}

function getPosition(that)
{
	var targetElem = that;
	var pos = new function(){this.x = 0; this.y = 0;}

	while(targetElem){
		pos.x += targetElem.offsetLeft; 
		pos.y += targetElem.offsetTop; 
		targetElem = targetElem.offsetParent;

		//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
		if((targetElem) && (is_ie)){
			pos.x += (parseInt(getElementStyle(targetElem,"borderLeftWidth","border-left-width")) || 0);
			pos.y += (parseInt(getElementStyle(targetElem,"borderTopWidth","border-top-width")) || 0);
		}
	}

	//geckoの補正：カウントしないbody部border幅をマイナスしてしまうので２倍して加算
	if(is_gecko){
		//以下の部分でbody部を取得し、borderの減算を補正する。
		//var b = document.getElementsByTagName("body")[0];　　//body部を取得
		//pos.x += 2*(parseInt(getElementStyle(b, "borderLeftWidth","border-left-width")) || 0);
		//pos.y += 2*(parseInt(getElementStyle(b, "borderTopWidth","border-top-width")) || 0);
		pos.x += 2*(parseInt(getElementStyle(document.getElementsByTagName("body")[0],"borderLeftWidth","border-left-width")) || 0);
		pos.y += 2*(parseInt(getElementStyle(document.getElementsByTagName("body")[0],"borderTopWidth","border-top-width")) || 0);
	}
	return pos;
}

function getScrollNowPosition()
{
	var pos = new function(){this.x = 0; this.y = 0;}

	if(is_opera || is_ie7){
		pos.x = document.body.parentNode.scrollLeft;
		pos.y = document.body.parentNode.scrollTop;

	}else if(is_gecko){
		pos.x = window.pageXOffset;
		pos.y = window.pageYOffset;

	}else{
		pos.x = document.body.scrollLeft;
		pos.y = document.body.scrollTop;
	}
	return pos;
}

function softScrollTarget(yp)
{
	var pos = getScrollNowPosition();
	var scdistX = pos.x;
	var scdistY = pos.y;

	scdistY = yp - scdistY;

	if(maxY < 50 && scdistY){
		var gv = scdistX / scdistY;
		scdistY = (Math.abs(scdistY) > 2) ? Math.round(scdistY * 0.2) : ((scdistY > 0) ? 1 : -1);
		maxY ++;
		window.scrollBy(Math.round(-scdistY*gv), scdistY);

		if(pos.y == saveScPos.y){
			saveScPos = getScrollNowPosition();
			if(timeID == null){
				timeID = setInterval("softScrollTarget("+yp+")",ScrollSpeed);
			}
		}else{
			maxX = maxY = 0;
			clearTime();
		}

	}else if(maxX < 50 && scdistX){
		scdistX = (scdistX > 2) ? Math.round(scdistX * 0.2) : 1;
		maxX ++;
		window.scrollBy(-scdistX, scdistY);

		if(pos.x == saveScPos.x){
			saveScPos = getScrollNowPosition();
			if(timeID == null){
				timeID = setInterval("softScrollTarget("+yp+")",ScrollSpeed);
			}
		}else{
			maxX = maxY = 0;
			clearTime();
		}
	}else{
		window.scrollTo(scdistX,yp);
		maxX = maxY = 0;
		clearTime();
	}
}

function goPosition(id)
{
	var dn = (goPosition.arguments.length > 1) ? parseInt(goPosition.arguments[1],10) : 0;

	saveScPos = getScrollNowPosition();

	clearTime();

	var yp = getPosition(document.getElementById(id)).y + dn;

	var inner = 0;
	var ctshi = document.getElementById("content").offsetHeight;
	var inner = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	if(is_ie7){
		//ctshi += 19;
	}else if(is_opera || is_gecko){
		ctshi += 61;
	}
	if(ctshi - inner < yp){
		yp = ctshi - inner;
	}


	softScrollTarget(yp);

	return false;
}

function goTop()
{
	return goPosition("content");
}

function goTarget(url)
{
	location.href=url;
	return false;
}

function goPos()
{
	if(targetID != ""){
		setTimeout('goPosition("'+targetID+'",'+targetPS+')',300);
	}
}

var targetID = "";
var targetPS = 0;
var str = location.search + "";
var pt = str.indexOf("?");
if(pt >= 0){
	if(str.indexOf("&") != -1){
		if(str.split("&")[0].split("=")[0] == "?id" && str.split("&")[1].split("=")[0] == "ps"){
			targetID = str.split("&")[0].split("=")[1];
			targetPS = str.split("&")[1].split("=")[1];
		}
	}
}

window.onload = function(){
	// ターゲット位置に移動する
	goPos();
}

