
// class Scroller

var g_oScroller = null;

function Scroller( _objScroller, _objPane )
{
	this.m_ObjScroller = _objScroller;
	this.m_ObjScrollBar = _objScroller.childNodes[ 1 ];
	this.m_ObjPane = _objPane;
	this.m_bActive = false;
	this.m_iHeightSeen = 0;
	this.m_iHeightAll = 0;
	this.m_iRefY = 0;
	this.m_iOldY = 0;
	this.m_iY = 0;
	this.m_iNextY = 0;
	this.m_iHeightScroller = _objScroller.clientHeight;
	this.m_iHeightTop = 0;
}

Scroller.prototype.div = function( _iA, _iB )
{
	if ( _iB == 2 )
	{
		return _iA >> 1;
	}
	if ( _iB == 4 )
	{
		return _iA >> 2;
	}
	if ( _iB == 8 )
	{
		return _iA >> 3;
	}
	return ( _iA / _iB ) | 0;
}

Scroller.prototype.mouseDown = function( _event )
{
	var y = document.all ? window.event.clientY : _event.screenY;
	if ( this.m_iHeightSeen < this.m_iHeightAll )
	{
		this.m_iRefY = y;
		this.m_iOldY = this.m_iY;
		this.m_bActive = true;
	}
	g_oScroller = this;
}

Scroller.prototype.mouseUp = function( _event )
{
	this.m_bActive = false;
	g_oScroller = null;
}

Scroller.prototype.mouseMove = function( _event )
{
	var y = document.all ? window.event.clientY : _event.screenY;
	if ( this.m_bActive )
	{
		y -= this.m_iRefY;
		y = this.div( y * ( this.m_iHeightAll - this.m_iHeightSeen ), this.m_iHeightScroller - this.div( this.m_iHeightScroller * this.m_iHeightSeen, this.m_iHeightAll ) );
		y += this.m_iOldY;
		if ( y < 0 )
		{
			y = 0;
		}
		else if ( y > this.m_iHeightAll - this.m_iHeightSeen )
		{
			y = this.m_iHeightAll - this.m_iHeightSeen;
		}
		if ( y != this.m_iY )
		{
			this.m_ObjPane.style.top = String( -y + this.m_iHeightTop ) + "px";
			this.m_iY = y;
			this.m_ObjScrollBar.style.top = String( this.div( this.m_iHeightScroller * this.m_iY, this.m_iHeightAll ) ) + "px";
		}
	}
}

Scroller.prototype.mouseWheel = function( _event )
{
	var iDelta = 0;
	if ( !_event )
	{
		_event = window.event;
	}
	if ( _event.wheelDelta )
	{
		iDelta = ( _event.wheelDelta / 120 ) | 0;
	}
	else if ( _event.detail )
	{
		iDelta = -( ( _event.detail / 3 ) | 0 );
	}
	if ( !this.m_bActive && ( this.m_iHeightSeen < this.m_iHeightAll ) )
	{
		var y = this.m_iY - ( iDelta * 20/*g_iFontSize*/ );
		if ( y < 0 )
		{
			y = 0;
		}
		else if ( y > this.m_iHeightAll - this.m_iHeightSeen )
		{
			y = this.m_iHeightAll - this.m_iHeightSeen;
		}
		if ( y != this.m_iY )
		{
			this.m_ObjPane.style.top = String( -y + this.m_iHeightTop ) + "px";
			this.m_iY = y;
			this.m_ObjScrollBar.style.top = String( this.div( this.m_iHeightScroller * this.m_iY, this.m_iHeightAll ) ) + "px";
		}
	}
	if ( _event.preventDefault )
	{
		_event.preventDefault();
	}
	_event.returnValue = false;	
}

Scroller.prototype.getY = function()
{
	return this.m_iY;
}

Scroller.prototype.getNextY = function()
{
	return this.m_iNextY;
}

Scroller.prototype.setNextY = function( _iNextY )
{
	this.m_iNextY = _iNextY;
}

Scroller.prototype.isAtEnd = function()
{
	return this.m_iY >= this.m_iHeightAll - this.m_iHeightSeen;
}

Scroller.prototype.setToEnd = function()
{
	var y = this.m_iHeightAll - this.m_iHeightSeen;
	if ( ( y != this.m_iY ) && ( y >= 0 ) )
	{
		this.m_ObjPane.style.top = String( -y + this.m_iHeightTop ) + "px";
		this.m_iY = y;
		this.m_ObjScrollBar.style.top = String( this.div( this.m_iHeightScroller * this.m_iY, this.m_iHeightAll ) ) + "px";
	}
}

Scroller.prototype.setPane = function( _objPane )
{
	this.m_ObjPane = _objPane;
}

Scroller.prototype.init = function( _iHeightSeen, _iHeightAll, _iHeightTop )
{
	this.m_iY = this.m_iNextY;
	this.m_iHeightSeen = _iHeightSeen;
	this.m_iHeightAll = _iHeightAll;
	this.m_iHeightTop = ( _iHeightTop == null ) ? 0 : _iHeightTop;
	this.m_iNextY = 0;
	if ( _iHeightSeen >= _iHeightAll )
	{
		this.m_ObjPane.style.top = "0px";
		this.m_ObjScrollBar.style.top = "0px";
		this.m_ObjScrollBar.style.height = "0px";
		this.m_ObjScrollBar.style.cursor = "default";
		this.m_ObjScrollBar.tgtg = this;
		this.m_ObjScrollBar.onmousedown = function( _event ) {};
	}
	else
	{
		if ( this.m_iY > this.m_iHeightAll - this.m_iHeightSeen )
		{
			this.m_iY = this.m_iHeightAll - this.m_iHeightSeen;
		}
		this.m_ObjPane.style.top = ( -this.m_iY + this.m_iHeightTop ) + "px";
		this.m_ObjScrollBar.style.top = this.div( this.m_iHeightScroller * this.m_iY, this.m_iHeightAll ) + "px";
		this.m_ObjScrollBar.style.height = ( this.div( this.m_iHeightScroller * _iHeightSeen, _iHeightAll ) + 1 ) + "px";
		this.m_ObjScrollBar.style.cursor = "pointer";
		this.m_ObjScrollBar.tgtg = this;
		this.m_ObjScrollBar.onmousedown = function( _event ) { this.tgtg.mouseDown( _event ); };
		this.m_ObjPane.parentNode.tgtg = this;
		this.m_ObjPane.parentNode.onmousewheel = function( _event ) { this.tgtg.mouseWheel( _event ); };
		if ( this.m_ObjPane.parentNode.addEventListener && !this.m_ObjPane.parentNode.mgmg )
		{
			this.m_ObjPane.parentNode.mgmg = true;
			this.m_ObjPane.parentNode.addEventListener( 'DOMMouseScroll', function( _event ) { this.tgtg.mouseWheel( _event ); }, false );
		}
		document.onmouseup = function( _event ) { if ( g_oScroller ) g_oScroller.mouseUp( _event ); };
		document.onmousemove = function( _event ) { if ( g_oScroller ) g_oScroller.mouseMove( _event ); };
	}
	if ( window.opera )
	{
		document.body.style += "";
	}
}

Scroller.prototype.deinit = function()
{
}

function onWheel( _event )
{
/*	var iDelta = 0;
	if ( !_event )
	{
		_event = window.event;
	}
	if ( _event.wheelDelta )
	{
		iDelta = ( _event.wheelDelta / 120 ) | 0;
	}
	else if ( _event.detail )
	{
		iDelta = -( ( _event.detail / 3 ) | 0 );
	}
	if ( g_oScroller != null )
	{
		g_oScroller.mouseWheel( iDelta );
	}
	if ( _event.preventDefault )
	{
		_event.preventDefault();
	}
	_event.returnValue = false;	*/
}

