// ----------------
// グローバル変数
// ----------------
var gDivName;         // レイヤー
var gObject;          // オブジェクト
var gMgnTop;          // 上マージン
var gMgnBtm;          // 下マージン
var gCurrentY;        // 現在の位置
var gTop;             // 初期位置
var gExY = cExY = 10; // 余分な距離
var gTurn = 0;        // 0:通常移動,1:折り返し,2:停止

// ----------------
// ブラウザチェック
// ----------------
function fCheckBrowser() {
	this.ver   = navigator.appVersion;
	this.agent = navigator.userAgent;
	this.dom   = document.getElementById ? 1 : 0;
	this.mac   = (this.agent.indexOf("Mac") > -1) ? 1 : 0;
	this.op6   = ((this.agent.indexOf("Opera 6") > -1) && this.dom) ? 1 : 0;
	this.ie5   = ((this.ver.indexOf("MSIE 5") > -1) && this.dom && !this.op6) ? 1 : 0;
	this.ie6r  = ((this.ver.indexOf("MSIE 6") > -1) && this.dom && (document.compatMode == "BackCompat")) ? 1 : 0;
	this.ie6s  = ((this.ver.indexOf("MSIE 6") > -1) && this.dom && (document.compatMode == "CSS1Compat")) ? 1 : 0;
	this.ie7  = ((this.ver.indexOf("MSIE 7") > -1) && this.dom) ? 1 : 0;
	this.ie8  = ((this.ver.indexOf("MSIE 8") > -1) && this.dom) ? 1 : 0;
	this.ie4   = (document.all && !this.dom) ? 1 : 0;
	this.ie    = (this.ie4 || this.ie5 || this.ie6r || this.ie6s || this.ie7 ||this.ie8) ? 1 : 0;
	this.ns6   = (this.dom && (parseInt(this.ver) >= 5)) ? 1 : 0;
	this.ns4   = (document.layers && !this.dom) ? 1 : 0;
	this.ns    = (this.ns4 || this.ns6) ? 1 : 0;
	this.bw5   = (this.ie5 || this.ie6r ||this.ie7 ||this.ie8) ? 1 : 0;
	this.bw6   = (this.ie7 ||this.ie8 || this.ie6s || this.ns6 || this.op6) ? 1 : 0;
	this.bw    = (this.ie7 ||this.ie8 || this.ie6r || this.ie6s || this.ie5 || this.ns6 || this.op6) ? 1 : 0;
	return this;
}

// ----------------
// レイヤー移動準備
// 引数id:レイヤーID
// 　　mt:上マージン
// 　　mb:下マージン
// 　　tp:初期位置
// ----------------
function ENInit(id, mt, mb, tp) {
	oBw = new fCheckBrowser;
	if (oBw.bw) {
		gDivName         = oBw.bw5 ? document.all(id) : oBw.bw6 ? document.getElementById(id) : 0;
		gObject          = gDivName.style;
		gObject.position = 'absolute';
		gMgnTop          = mt ? mt : 0;
		gMgnBtm          = mb ? mb : 0;
		gCurrentY = gTop = tp ? tp : gDivName.offsetTop;
		fMoveLayer();
	}
}

// ----------------
// レイヤー移動
// ----------------
function fMoveLayer() {
	var lWinH = 0;  // ウィンドウの縦幅
	var lDivH = 0;  // レイヤーの縦幅
	var lTgtY   = 0;  // 移動目標
	var lTgtY1  = 0;  // 移動目標（下方移動）
	var lTgtY2  = 0;  // 移動目標（上方移動）
	var lDtcY   = 0;  // 移動距離

	lDivH = gDivName.offsetHeight;

	if (oBw.ie6s) {
		lWinH = document.documentElement.clientHeight;
	} else if (oBw.ns6 || oBw.op6) {
		lWinH = innerHeight;
	} else if (oBw.bw5) {
		lWinH = document.body.clientHeight;
	} else {
		lWinH = 0;
	}

	if (oBw.ie6s) {
		lTgtY = document.documentElement.scrollTop;
	} else if (oBw.bw5) {
		lTgtY = document.body.scrollTop;
	} else if (oBw.ns6 || oBw.op6) {
		lTgtY = window.pageYOffset;
	} else {
		lTgtY = 0;
	}

	if (lWinH >= gMgnTop + lDivH + gMgnBtm) {
		lTgtY = Math.max(lTgtY + gMgnTop, gTop);
	} else {
		lTgtY1 = Math.max(lTgtY + gMgnTop, gTop);
		lTgtY2 = Math.max(lTgtY - (lDivH + gMgnBtm - lWinH), gTop);
		if (lTgtY1 > gCurrentY && lTgtY2 < gCurrentY) {
			lTgtY = gCurrentY;
		} else if (lTgtY2 < gCurrentY) {
			lTgtY = Math.max(lTgtY1, lTgtY2);
		} else {
			lTgtY = Math.min(lTgtY1, lTgtY2);
		}
	}
	lTgtY += gExY;

	if (lTgtY != gCurrentY) {
		lDtcY = (lTgtY - gCurrentY) * 0.3;
		if (Math.abs(lDtcY) < 1 || (Math.abs(lTgtY - gCurrentY) <= cExY && gTurn == 1)) {
			lDtcY = (lDtcY > 0) ? 1 : (lDtcY < 0) ? -1 : 0;
		}
		gCurrentY   += Math.round(lDtcY);
		gObject.top =  gCurrentY + 'px';
		if (Math.abs(lTgtY - gCurrentY) <= cExY && gTurn == 1) {
			gExY  = 0;
		} else {
			gExY  = (lDtcY > 0) ? cExY : (lDtcY < 0) ? -cExY : 0;
			gTurn = 0;
		}
	} else {
		if (gTurn == 0) {
			gTurn = 1;
			gExY  = 0;
		} else {
			gTurn = 2;
		}
	}

	setTimeout('fMoveLayer()', 20);
}
