var gobjScrollArea = new ScrollArea();
var gScrollTimer;

function ScrollArea()
{
    //Public Properties
    this.ID;
    this.Speed;
    this.ObjBodyScroller
    this.ObjBodyContainer
    this.Scroller;
    this.ScrollerTop;
    this.ScrollerHeight;
    this.Container;
    this.ContainerHeight;
    this.CursorBackHeight;
    this.Cursor;
    this.CursorHeight;
    this.CursorTop;    
    
    //Public methods
    this.GetElements = getElements;
    this.MoveElements = moveElements;
    this.MoveCursor      = moveCursor;    
    this.MoveMaxElements = moveMaxElements;
    
    //Constructor
    this.Speed = 50;
    
    function getElements()
    {
        var objCursor         = document.getElementById("DivBodyCursor" + this.ID);
    
		this.Container        = document.getElementById("DivBodyContainer" + this.ID).style;
		this.ContainerHeight  = parseInt(this.Container.height,10);
		
		
		this.Scroller         = document.getElementById("DivBodyScroller" + this.ID).style;
		this.ScrollerTop      = parseInt(this.Scroller.top);
		this.ScrollerHeight   = document.getElementById("DivBodyScroller" + this.ID).offsetHeight;
		
		this.CursorBackHeight = document.getElementById("DivBodyCursorBack" + this.ID).offsetHeight;
		this.Cursor           = objCursor.style;
		this.CursorHeight     = parseInt(this.Cursor.height,10);
		this.CursorTop        = parseInt(this.Cursor.top,10);		
    }
    
    function moveElements(intMove)
    {
        //var intCMove      = Math.abs( (this.ContainerHeight - (2*CursorPadding) - this.CursorHeight) * (intMove / (this.ScrollerHeight - this.ContainerHeight)));
        var intCMove      = Math.abs( (this.CursorBackHeight - this.CursorHeight) * (intMove / (this.ScrollerHeight - this.ContainerHeight)));
    
        if( this.ScrollerHeight > this.ContainerHeight )
        {
            if( intMove > 0 ) //Arrow DOWN (Scroller -> UP)
            {
                if( ( this.ScrollerTop - Math.abs(intMove) ) > (this.ContainerHeight - this.ScrollerHeight) )
                {
                    //*** Move it up
                    this.ScrollerTop   -= Math.abs(intMove);
                    this.Scroller.top   = this.ScrollerTop + "px";
                    this.CursorTop     += intCMove;
                    this.Cursor.top     = parseInt(this.CursorTop,10) + "px";                     
                }
                else if ( ( this.ScrollerTop ) != (this.ContainerHeight - this.ScrollerHeight) )
                {
                    //*** Move it to the max
                    this.ScrollerTop    = (this.ContainerHeight - this.ScrollerHeight);
                    this.Scroller.top   = this.ScrollerTop + "px";
                    this.CursorTop      = (this.CursorBackHeight - this.CursorHeight);
                    this.Cursor.top     = this.CursorTop + "px";                     
                }
            }
            else if( intMove < 0 ) //Arrow UP (Scroller -> DOWN)
            {
                if ( this.ScrollerTop + Math.abs(intMove) < 0 )
                {
                    //*** Move it down
                    this.ScrollerTop   += Math.abs(intMove);
                    this.Scroller.top   = this.ScrollerTop + "px";
                    this.CursorTop     -= intCMove;
                    this.Cursor.top     = parseInt(this.CursorTop,10) + "px";                      
                }
                else if ( this.ScrollerTop != 0 )
                {
                    //*** Move it to the min
                    this.ScrollerTop    = 0;
                    this.Scroller.top   = this.ScrollerTop + "px";  
                    this.CursorTop      = 0;
                    this.Cursor.top     = this.CursorTop + "px";                                
                }
            }
        }
    }
    
    function moveMaxElements(intDir)
    {
        if( this.ScrollerHeight > this.ContainerHeight )
        {
            if( intDir > 0 )
            {
                this.ScrollerTop    = this.ContainerHeight - this.ScrollerHeight;
                this.Scroller.top   = this.ScrollerTop + "px";
                this.CursorTop      = (this.CursorBackHeight - this.CursorHeight);
                this.Cursor.top     = this.CursorTop + "px";                  
            }
            else if( intDir < 0)
            {
                this.ScrollerTop    = 0;
                this.Scroller.top   = this.ScrollerTop + "px";
                this.CursorTop      = 0;
                this.Cursor.top     = this.CursorTop + "px";                   
            }
        }
    }
    
    function moveCursor(intCMove)
    {
        var intCursorCanMove   = (this.CursorBackHeight - this.CursorHeight);
        var intScrollerCanMove = (this.ScrollerHeight - this.ContainerHeight);

        var intMove    = ( (intCMove / intCursorCanMove) *  intScrollerCanMove);
        
        this.MoveElements(intMove)
    }    
    
}


function PerformVScroll(intID, intMove)
{
	if( gobjScrollArea.ID != intID )
	{
	    gobjScrollArea.ID = intID;
        
	}
	gobjScrollArea.GetElements();
	
	recuMoveScroll(intMove);
}

function PerformMaxVScroll(intID,intDir)
{
	if( gobjScrollArea.ID != intID )
	{
	    gobjScrollArea.ID = intID;
	}
	
	gobjScrollArea.GetElements();
	gobjScrollArea.MoveMaxElements(intDir)
}

function recuMoveScroll(intMove)
{
    gobjScrollArea.MoveElements(intMove)
    gScrollTimer = setTimeout("recuMoveScroll(" + intMove + ")", gobjScrollArea.Speed); 
}

function CeaseScroll()
{
    if( gScrollTimer )
    {
        clearTimeout(gScrollTimer);
    }
}







//*** Start Cursor Scroll
function PerformCScroll(intID,e)
{
    gobjScrollArea.ID = intID;
	gobjScrollArea.GetElements();
    if( gobjScrollArea.ScrollerHeight > gobjScrollArea.ContainerHeight )
    {
	    gCursorFrom = (!document.all)? e.pageY : e.clientY + document.body.scrollLeft;
	    
	    //window.status = "gCursorFrom:" + gCursorFrom;
    	
	    document.onmousemove = moveScrollCursor;
	    document.onmouseup   = dropScrollCursor;
	    if( !document.all ) 
	    {
		    document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP)
	    }
    }
    return(false);
}

function moveScrollCursor(e)
{
	var toY = (!document.all)? e.pageY : event.clientY + document.body.scrollLeft;
	//window.status = "fromY:" + gCursorFrom + " toY:" + toY;
	
	var distanceY = toY - gCursorFrom;    
    
    //window.status = distanceY;
    gobjScrollArea.MoveCursor(distanceY);
    
    gCursorFrom = toY;
    
    return(false);
}

function dropScrollCursor(e)
{
	//window.status = "Don't stop moving";
	document.onmousemove = null;
	document.onmouseup   = null;
	return(false);
}