var eventTimer;				// タイマー変数
var restScroll=0;			// スクロール残量
var currentBlock = 0;	//現在位置
var obj_move = 0;			//指定スクロール先
var elem_move = 0;		//動き先


function getElementY(elem){
var y = 0;
	while(elem.offsetParent){
		y += elem.offsetTop
		elem = elem.offsetParent;
	}
return y;
}


function getTop(){
	if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	else return window.pageYOffset;
}


function getWindowHeight(){
var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
	}
return myHeight;
}

function Scroll(base,move){
var obj_base = getElemPosition(base);// 移動元(base)要素＆オブジェクトを取得
elem_move = document.getElementById(move);// 移動先(move)要素＆オブジェクトを取得
obj_move = getElemPosition(elem_move);

var startPosition = getTop()
var TESdocumentHeight = document.documentElement.scrollHeight;
var TESwindowHeight = getWindowHeight();

	if(obj_move.y >= (TESdocumentHeight - TESwindowHeight)){
	obj_move.y = (TESdocumentHeight - TESwindowHeight);
	}
restScroll = obj_move.y - startPosition;
eventTimer = setInterval(setScrollPosition,1);
}

// スクロール処理をする
function setScrollPosition() {
var moveValue=0;
// スクロール残量が80以上の場合、スクロール量を変える
// Math.abs()では値の絶対値を取得
var moveScrollNum = Math.abs((restScroll/2));
myHeight = getWindowHeight();
currentBlock = getTop();
	if(Math.abs(restScroll)>myHeight){
	moveValue = (restScroll>0)?moveScrollNum:(moveScrollNum-(moveScrollNum*2));
	}else{
	moveValue = Math.round(restScroll/5);
	}
// スクロールを処理
parent.scrollBy(0,moveValue);
// スクロール残量を計算して、残りが無ければタイマー解除
restScroll = (restScroll>0)?restScroll-moveValue:restScroll-moveValue;
	if(moveValue == 0){
	clearInterval(eventTimer);
	restScroll=0;
	}
}

//要素の位置を取得し、オブジェクトとして返す
function getElemPosition(elem) {
var obj = new Object();
obj.x = elem.offsetLeft;
obj.y = elem.offsetTop;
// 親要素を取得して位置情報を修正する
	while(elem.offsetParent) {
		elem = elem.offsetParent;
		obj.x += elem.offsetLeft;
		obj.y += elem.offsetTop;
	}
return obj;
}
