/*
	파일명 : lib.fixmenu.js
	작성자 : steelheart(http://dart.new21.net/zboard/zboard.php?id=dartjint&no=19)
	수정자 : 이범민
	용  도 : 따라다니는 배너
	사용법 : 

		<div id="FIXMENU" style="position:absolute;width:170px;height:310px;">
			메뉴내용
		</div>

		<script type="text/javascript" src="lib.fixmenu.js"></script>
		<script type="text/javascript">
			var fixmenu = new FixMenu();
			fixmenu.alignCenter = true;
			fixmenu.bodyWidth = 700;
			fixmenu.speed = 10;
			fixmenu.init('fixmenu','FIXMENU');
		</script>
*/
FixMenu = function () {
	this.div = false;								// 메뉴영역 레이어의 id 값
	this.clientWidth = document.body.clientWidth;	// 현재 브라우저의 넓이(사이즈 변경 감시용)
	this.alignCenter = false;						// 사이트가 가운데 정렬인가
	this.bodyWidth = 770;							// 사이트가 가운데 정렬인 경우 사이트 Body의 넓이
	this.baseLeft = 700;							// 사이트가 가운데 정렬이 아닐경우 왼쪽으로부터의 거리
	this.baseTop = 150;								// 기본위치(top)
	this.initTop = 100;								// 초기 시작위치(top) : 기본위치 값과 다를경우 약간의 효과를 낼수 있다.
	this.scrollGap = 50;							// 스크롤시 상단과의 거리(top)
	this.frequency = 100;							// 움직임을 감지하는 빈도(숫자가 클수록 둔감해짐)
	this.speed = 10;								// 스크롤 속도 (숫자가 클수록 느려짐)
	this.heightTaget;								// 문서높이 계산에 사용될 엘레먼트
	this.heightTagetID;							// 문서높이 계산에 사용될 엘레먼트 ID
	this.timer = false;
}

FixMenu.prototype.init = function(div_id) {
	if(!div_id) return false;
	if(document.getElementById(div_id) != 'undefined') {
		this.div = document.getElementById(div_id);
	} else {
		this.div = false;
	}
	this.div.obj = this;
	this.div.style.top = document.body.scrollTop + this.initTop;
	if(!this.heightTaget && this.heightTagetID) this.heightTaget = document.getElementById(this.heightTagetID);
	this.initPosition();
}

FixMenu.prototype.initPosition = function() {
	if(!this.div) return false;
	this.clientWidth = cWidth = document.body.clientWidth;
	if(cWidth<this.bodyWidth) cWidth = this.bodyWidth;
	if(this.alignCenter) {
		this.div.style.left = Math.ceil((cWidth + this.bodyWidth)/2);
	} else {
		this.div.style.left = this.baseLeft
	}
	this.RefreshFixMenu(this.div.id);
}

FixMenu.prototype.RefreshFixMenu = function(id) {
	obj = document.getElementById(id).obj;
	if (!obj.div) return;
	if (obj.clientWidth != document.body.clientWidth) obj.initPosition();
	var stmnStartPoint, stmnEndPoint, stmnRefreshTimer, baseHeight;
	stmnStartPoint = parseInt(obj.div.style.top, 10);
	stmnEndPoint = document.body.scrollTop + obj.scrollGap;
	baseHeight = obj.heightTaget.clientHeight;
	if (stmnEndPoint < obj.baseTop) stmnEndPoint = obj.baseTop;
	if (stmnEndPoint > baseHeight - obj.div.clientHeight + obj.baseTop) stmnEndPoint = baseHeight - obj.div.clientHeight + obj.baseTop;
	stmnRefreshTimer = obj.frequency;
	if ( stmnStartPoint != stmnEndPoint ) {
		stmnScrollAmount = Math.ceil( Math.abs( stmnEndPoint - stmnStartPoint ) / 15 );
		obj.div.style.top = parseInt(obj.div.style.top, 10) + ( ( stmnEndPoint<stmnStartPoint ) ? -stmnScrollAmount : stmnScrollAmount );
		stmnRefreshTimer = obj.speed;
	}
	obj.timer = setTimeout ("FixMenu.prototype.RefreshFixMenu('" + obj.div.id + "');", stmnRefreshTimer);
}
