/****************************************************************************************************
제목 : 객체 스크롤 제어 함수
----------------------------------------------------------------------------------------------------
목적 :
----------------------------------------------------------------------------------------------------
주의 :
	1. 의존성 : UTIL.js , jQuery-1.3.2.js
----------------------------------------------------------------------------------------------------
POST
----------------------------------------------------------------------------------------------------
GET
----------------------------------------------------------------------------------------------------
REQUEST
----------------------------------------------------------------------------------------------------
작성자 : 고광진 (akanaka)
작성일 : 2007년 11월 23일
수정일 : 2009년 07월 01일
연락처 : akanaka@hanmail.net
****************************************************************************************************/


var SCROLL = new Object () ;


/**************************************************
가속도 계산
--------------------------------------------------
공식 : ceil ( abs ( 움직일 거리 - 현재위치 / 속도 ) )
**************************************************/
SCROLL.accel = function ( objTo , objNow , speed )
{
	return Math.ceil ( Math.abs ( objTo - objNow ) / speed ) ;
}


SCROLL.slide = new Object () ;


/**************************************************
스크롤 바에 따른 객체 슬라이드 : 위 / 아래
--------------------------------------------------
obj			: 객체 ID
limitTop	: 상단 제한 ( 단위 : px )
limitBottom	: 하단 제한 ( 단위 : px )
outer		: 외부 객체 ( 기본 값 : document.body )
--------------------------------------------------
this.flagDrag 사용
this.outerTag 사용
**************************************************/
SCROLL.slide.upDown = function ( obj , limitTop , limitBottom , outer )
{			
	var timeoutNextCheck = 100 ;	
	if ( ! obj.outerTag )
	{		
		obj.outerTag = ( UTIL.get.type ( outer ) == "object" ) ? outer : document.documentElement ;
	}
	if ( obj.flagDrag != true )
	{		
		limitBottom = ( limitBottom ) ? limitBottom : 0 ;
		var bodyScrollHeight = obj.outerTag.scrollHeight ;
		var bodyClientHeight = obj.outerTag.clientHeight ;
		var pageHeight = ( bodyScrollHeight > bodyClientHeight ) ? bodyScrollHeight : bodyClientHeight ;
		if ( limitTop < pageHeight )
		{			
			var objTop		= $( obj ).offset().top ;						// 오브젝트의 현재 위치			
			var objTo		= $( obj.outerTag ).scrollTop() + limitTop ;	// 스크롤바 위치에 따른 움직일 위치 값
			var objHeight	= obj.offsetHeight ;							// 객체의 높이
			var objBottom	= objTo + objHeight ;
			var pageBottom	= pageHeight - limitBottom ;
			if ( objBottom >= pageBottom )
			{
				objTo = pageBottom - objHeight ;
			}
			if ( objTop != objTo )
			{
				yOffset = SCROLL.accel ( objTo , objTop , 20 ) ;
				$( obj ).css( 'top' , parseInt ( $( obj ).offset().top , 10 ) + ( ( objTo < objTop ) ? -yOffset : yOffset ) ) ;
				timeoutNextCheck = 10 ;				
			}
		}
	}
	setTimeout ( 'SCROLL.slide.upDown ( UTIL.get.obj ( "' + obj.id + '" ) , ' + limitTop + ' , ' + limitBottom + ' )' , timeoutNextCheck ) ;
}


/**************************************************
스크롤 바에 따른 객체 슬라이드 : 왼쪽 / 오른쪽
--------------------------------------------------
obj			: 객체 ID
limitLeft	: 왼쪽 제한 ( 단위 : px )
limitRight	: 오른쪽 제한 ( 단위 : px )
outer		: 외부 객체 ( 기본 값 : document.body )
--------------------------------------------------
this.flagDrag 사용
this.outerTag 사용
**************************************************/
SCROLL.slide.leftRight = function ( obj , limitLeft , limitRight , outer )
{
	var timeoutNextCheck = 100 ;
	if ( ! obj.outerTag )
	{
		obj.outerTag = ( UTIL.get.type ( outer ) == "object" ) ? outer : document.documentElement ;
	}
	if ( obj.flagDrag != true )
	{
		limitRight = ( limitRight ) ? limitRight : 0 ;
		var bodyScrollWidth = obj.outerTag.scrollWidth ;
		var bodyClientWidth = obj.outerTag.clientWidth ;
		var pageWidth = ( bodyScrollWidth > bodyClientWidth ) ? bodyScrollWidth : bodyClientWidth ;
		if ( limitLeft < pageWidth )
		{
			var objLeft		= $( obj ).offset().left ;						// 오브젝트의 현재 위치
			var objTo		= $( obj.outerTag ).scrollLeft() + limitLeft ;	// 스크롤바 위치에 따른 움직일 위치 값
			var objWidth	= obj.offsetWidth ;								// 객체의 높이
			var objRight	= objTo + objWidth ;
			var pageRight	= pageWidth - limitRight ;
			if ( objRight >= pageRight )
			{
				objTo = pageRight - objRight ;
			}
			if ( objLeft != objTo )
			{
				yOffset = SCROLL.accel ( objTo , objLeft , 20 ) ;
				$( obj ).css( 'left' , parseInt ( obj.style.left , 10 ) + ( ( objTo < objLeft ) ? -yOffset : yOffset ) ) ;
				timeoutNextCheck = 10 ;
			}
		}
	}
	setTimeout ( "SCROLL.slide.leftRight ( " + obj.id + " , " + limitLeft + " , " + limitRight + " )", timeoutNextCheck ) ;
}


/**************************************************
객체 스크롤 : 좌측 이동
--------------------------------------------------
obj		: 객체 ID
speed	: 속도
accel	: 가속 여부 ( 가속 : true , 비 가속 : false )
**************************************************/
SCROLL.left = function ( obj , speed , accel )
{
	if ( obj.clientWidth < obj.scrollWidth )
	{
		if ( obj.scrollLeft != 0 )
		{
			if ( obj.scrollLeft - speed < 0 && accel == false )
			{
				obj.scrollLeft = 0 ;
			}
			else if ( accel )
			{
				obj.scrollLeft -= SCROLL.accel ( 0 , obj.scrollLeft , speed ) ;
			}
			else
			{
				obj.scrollLeft -= speed ;
			}
		}
		if ( obj.flagLeft )
		{
			setTimeout ( "SCROLL.left ( " + obj.id + " , " + speed + " , " + accel + " )" , 10 ) ;
		}
	}
}


/**************************************************
객체 스크롤 : 상단 이동
--------------------------------------------------
obj		: 객체 ID
speed	: 속도
accel	: 가속 여부 ( 가속 : true , 비 가속 : false )
**************************************************/
SCROLL.top = function ( obj , speed , accel )
{
	if ( obj.clientHeight < obj.scrollHeight )
	{
		if ( obj.scrollTop != 0 )
		{
			if ( obj.scrollTop - speed < 0 && accel == false )
			{
				obj.scrollTop = 0
			}
			else if ( accel )
			{
				obj.scrollTop -= SCROLL.accel ( 0 , obj.scrollTop , speed ) ;
			}
			else
			{
				obj.scrollTop -= speed ;
			}
		}
		if ( obj.flagTop )
		{
			setTimeout ( "SCROLL.top ( " + obj.id + " , " + speed + " , " + accel + " )" , 10 ) ;
		}
	}
}


/**************************************************
객체 스크롤 : 우측 이동
--------------------------------------------------
obj		: 객체 ID
speed	: 속도
accel	: 가속 여부 ( 가속 : true , 비 가속 : false )
**************************************************/
SCROLL.right = function ( obj , speed , accel )
{
	if ( obj.clientWidth < obj.scrollWidth )
	{
		var scrollBar	= ( obj.scrollFlat == "yes" ) ? 16 : 18 ;
		var limit		= obj.scrollWidth - obj.offsetWidth + scrollBar ;
		if ( obj.scrollLeft != limit )
		{
			if ( obj.scrollTop + speed > limit )
			{
				obj.scrollLeft = limit ;
			}
			else if ( accel )
			{
				obj.scrollLeft += SCROLL.accel ( limit , obj.scrollLeft , speed ) ;
			}
			else
			{
				obj.scrollLeft += speed ;
			}
		}
		if ( obj.flagRight )
		{
			setTimeout ( "SCROLL.right ( " + obj.id + " , " + speed + " , " + accel + " )" , 10 ) ;
		}
	}
}


/**************************************************
객체 스크롤 : 하단 이동
--------------------------------------------------
obj		: 객체 ID
speed	: 속도
accel	: 가속 여부 ( 가속 : true , 비 가속 : false )
**************************************************/
SCROLL.bottom = function ( obj , speed , accel )
{
	if ( obj.clientHeight < obj.scrollHeight )
	{
		var scrollBar	= ( obj.scrollFlat == "yes" ) ? 16 : 18 ;
		var limit		= obj.scrollHeight - obj.offsetHeight + scrollBar ;
		if ( obj.scrollTop != limit )
		{
			if ( obj.scrollTop + speed > limit )
			{
				obj.scrollTop = limit ;
			}
			else if ( accel )
			{
				obj.scrollTop += SCROLL.accel ( limit , obj.scrollTop , speed ) ;
			}
			else
			{
				obj.scrollTop += speed ;
			}
		}
		if ( obj.flagBottom )
		{
			setTimeout ( "SCROLL.bottom ( " + obj.id + " , " + speed + " , " + accel + " )" , 10 ) ;
		}
	}
}