
// Browser Checker
function browserCheck(){ 
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera6=((this.agent.indexOf("Opera 6")>-1) && this.dom)?1:0;
	this.ie5=((this.ver.indexOf("MSIE 5")>-1) && this.dom && !this.opera6)?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.ie4=(document.all && !this.dom)?1:0;
	this.ie=(this.ie4||this.ie5||this.ie6r||this.ie6s)?1:0;
	this.mac=(this.agent.indexOf("Mac")>-1)?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)?1:0;
	this.bw6=(this.ie6s||this.ns6||this.opera6)?1:0;
	this.bw=(this.ie6r||this.ie6s||this.ie5||this.ns6||this.opera6)?1:0;
	return this;
}

// Global Variables
var ENMarginTop, ENMarginBottom, ENTop;
var ENDivName, ENObject, ENCurrentY;

function ENInit(id, mt, mb, tp) // DIVid, margin-top, margin-bottom, position-top,
{
	bw=new browserCheck;
	if (bw.bw) {
		ENDivName = bw.bw5 ? document.all(id) : bw.bw6 ? document.getElementById(id) : 0;
		ENObject = ENDivName.style;
		ENObject.position = 'absolute';
		ENMarginTop = mt ? mt : 0; // 引数mt省略時は0
		ENMarginBottom = mb ? mb : 0; // 引数mb省略時は0
		ENCurrentY = ENTop = tp ? tp : ENDivName.offsetTop; //引数tp省略時は自動取得
		ENSmoothMove();
	} 
}

function ENSmoothMove()
{
	var winh = bw.ie6s ? document.documentElement.clientHeight : (bw.ns6||bw.opera6) ? innerHeight : bw.bw5 ? document.body.clientHeight : 0 ;
	var yt = bw.ie6s ? document.documentElement.scrollTop : bw.bw5 ? document.body.scrollTop : (bw.ns6||bw.opera6) ? window.pageYOffset : 0;
	var divh = ENDivName.offsetHeight;

	if (winh >= ENMarginTop + divh + ENMarginBottom) {
		yt = Math.max(yt + ENMarginTop, ENTop);
	} else {
		// 下へ向かう力
		var yt1 = Math.max(yt + ENMarginTop, ENTop);
		var f1 = (yt1 > ENCurrentY) ? 1 : 0;
		// 上へ向かう力
		var yt2 = yt - (divh + ENMarginBottom - winh);
		yt2 = Math.max(yt2, ENTop);
		var f2 = (yt2 < ENCurrentY) ? 1 : 0;
		// 両方の力が働いている場合は相殺
		if (f1 && f2) yt = ENCurrentY;
		else yt = f2 ? Math.max(yt1, yt2) : Math.min(yt1, yt2);
	}

	if (yt != ENCurrentY) {
		// 目標値に徐々に近づける
		var vy = (yt - ENCurrentY) * 0.25;
		if (Math.abs(vy) < 1) vy = (vy > 0) ? 1 : (vy < 0) ? -1 : 0;
		ENCurrentY += Math.round(vy);
		ENObject.top = ENCurrentY + 'px';
	}
	setTimeout('ENSmoothMove()', 20);
}

