var SCROLLER_SCROLL_UP = -1;
var SCROLLER_SCROLL_DOWN = 1;

function Scroller(scrollDiv, topPosition)
{
    // variables
    this.TargetDiv = scrollDiv;
    this.TopPosition = topPosition; // px  
    this.StartStep = 20; // px 
    this.CurrentStep = 0; // px
    this.StepIncrease = 0.05; // %
    this.MaxStep = 60.0; // px
    this.ScrollTimer = 50; // ms  
    this.ScrollInterval = null;   
    this.TopClip = 0; //px
    this.WidthClip = scrollDiv.parentNode.offsetWidth; // px
    this.BottomClip = scrollDiv.parentNode.offsetHeight - 3 * parseInt(scrollDiv.offsetTop) / 2; // px

    // mouse wheel handler
    scrollDiv.MouseWheelHandler = function(event){
        var delta = 0;
        if (!event) event = window.event;
        if (event.wheelDelta)
            delta = event.wheelDelta/120;
        else if (event.detail)
            delta = -event.detail/3;
        this.Scroller.ScrollBy(-this.Scroller.StartStep*delta);
        if (event.preventDefault) event.preventDefault();
        event.returnValue = false;
    }
    
    // ScrollBy methods
    this.ScrollBy = function(step){ 
        if ( !step )
            this.CurrentStep += this.CurrentStep * this.StepIncrease;
        else
            this.CurrentStep = step;
        
        if ( this.CurrentStep > this.MaxStep )
            this.CurrentStep = this.MaxStep;
        else if ( this.CurrentStep < -this.MaxStep )
            this.CurrentStep = -this.MaxStep;
            
        step = Math.round(this.CurrentStep);
        this.TopClip += step;
        this.BottomClip += step;
        this.TopPosition -= step;

        if (this.TopClip < 0 || this.BottomClip > this.TargetDiv.offsetHeight) {
            this.TopClip -= step;
            this.BottomClip -= step;
            this.TopPosition += step;    
        }

        this.TargetDiv.style.clip = 'rect(' + this.TopClip + 'px, ' + this.WidthClip + 'px, ' + this.BottomClip + 'px, 0)';
        this.TargetDiv.style.top = this.TopPosition + 'px';
    }
    
    // ScrollStart method
    this.Start = function(direction){
        this.ScrollBy(direction * this.StartStep);
        this.ScrollInterval = setInterval("document.getElementById('"+this.TargetDiv.id+"').Scroller.ScrollBy()",this.ScrollTimer);
    }
    
    this.Stop = function(){
        clearInterval(this.ScrollInterval);
        this.ScrollInterval = null;
        this.CurrentStep = 0;
    }
    
     this.Toggle = function(direction){
        this.ScrollInterval ? this.Start(direction) : this.Stop();
    }
    
    // div initialization    
    scrollDiv.parentNode.style.overflow = 'hidden';
    scrollDiv.style.clip = 'rect(' + this.TopClip + 'px,' + this.WidthClip + 'px,' + this.BottomClip + 'px, 0px)';
    if (scrollDiv.addEventListener) scrollDiv.addEventListener('DOMMouseScroll', scrollDiv.MouseWheelHandler, false);
    scrollDiv.onmousewheel = scrollDiv.MouseWheelHandler;    
    scrollDiv.Scroller = this;
    
}


