// JavaScript Document

/*---scroll.js---*/

var numX_from = 0;      // 開始位置：Y座標
var numY_from = 0;      // 開始位置：Y座標
var numY_to= 0;   		// 停止位置：Y座標
var num_repeat = 5;     // タイマー呼出間隔（ms）
var num_speed = 0.8;    // 移動スピード
var objTimer = null;

var bName = "";
var bVersion = 0;

checkBrowser();

//ブラウザチェック
function checkBrowser(){
	ua = navigator.userAgent;
	//Firefox
	cName = "Firefox";
	num_cName = cName.length;
	num_pos_to = ua.indexOf(cName);
	if (num_pos_to != -1){
		n =num_pos_to+num_cName+1;
		bName = cName ;
		bVersion = ua.substr(n);
		return;
	}
	//Opera
	cName = "Opera";
	num_cName = cName.length;
	num_pos_to = ua.indexOf(cName);
	if (num_pos_to != -1){
		n =num_pos_to+num_cName+1;
		bName = cName ;
		bVersion = ua.substr(n,1);
		return;
	}
	//Safari
	cName = "Safari";
	num_cName = cName.length;
	num_pos_to = ua.indexOf(cName);
	if (num_pos_to != -1){
		n =num_pos_to+num_cName+1;
		bName = cName ;
		bVersion = ua.substr(n);
		if (bVersion>400){
			bVersion = 2;
		} else {
			bVersion = 1;
		}
		return;
	}
	//Netscape
	cName = "Netscape";
	num_cName = cName.length;
	num_pos_to = ua.indexOf(cName);
	if (num_pos_to != -1){
		n =num_pos_to+num_cName+1;
		bName = cName ;
		bVersion = ua.substr(n,1);
		return;
	}
	//MSIE
	cName = "MSIE";
	num_cName = cName.length;
	num_pos_to = ua.indexOf(cName);
	if (num_pos_to != -1){
		n =num_pos_to+num_cName+1;
		bName = cName ;
		bVersion = ua.substr(n,1);
		return;
	}

}

//現在位置取得：x
function getCurrentPos_x(){
	if (bName == "MSIE"){			
		if (bVersion == 6){
			return document.documentElement.scrollLeft;
		} else {
			return document.body.scrollLeft;
		}
	} else {
		return window.pageXOffset;
	}
}

//現在位置取得：y
function getCurrentPos_y(){
	if (bName == "MSIE"){			
		if (bVersion == 6){
			return document.documentElement.scrollTop;
		} else {
			return document.body.scrollTop;
		}
	} else {
		return window.pageYOffset;
	}
}

//スクロール：開始
function goTop(){
	numX_from = getCurrentPos_x();
	numY_from = getCurrentPos_y(); 
	/*alert("start:x = "+numX_from+" y = "+numY_from);*/
	if (numY_from>0){
		/*alert("scroll:"+numY_from);*/
		goScroll();
	}
}

//スクロール
function goScroll() {
	numX_from = getCurrentPos_x();
	numY_from = getCurrentPos_y(); 

    if (numY_from > 1) {
		/*alert("numY_from:"+numY_from);*/
		var wDis = Math.floor((numY_from-numY_to)*num_speed);
		window.scrollTo(numX_from, wDis);
		clearTimeout(objTimer);
		objTimer = setTimeout("goScroll()", num_repeat);
    }else{
		/*alert("scroll end")*/
		numX_from = 0;
		numY_from = 0;
		clearTimeout(objTimer);
    }
}
  

