﻿// Div Scrolling Javascript helper object by Julian Grinblat
// Provides functions to start scrolling, stop scrolling, and scroll by a few pixels

var ScrollTypes = {

    Top: 1,
    Bottom: 2,
    Left: 3,
    Right: 4

}

var DivScroller = function(div)
{
    this.div = div;
    this.deltaScroll = 1;
    this.interval = 50;
    this.scrolling = false;
    
    this.IsScrolling = function()
    {
        return this.scrolling;
    }
    
    this.StartScrolling = function(type)
    {
        this.scrolling = true;
        
        this.ScrollCallback(type);
    }
    
    this.StopScrolling = function()
    {
        this.scrolling = false;
    }
    
    this.ScrollCallback = function(type)
    {
        if(this.scrolling)
        {
            this.ScrollBy(type, this.deltaScroll);
            setTimeout(createObjectCallback(this, this.ScrollCallback, [type]), this.interval);
        }
    }
    
    this.ScrollBy = function(type, delta)
    {
        if(type == ScrollTypes.Left)
        {
            if(this.div.scrollLeft + delta < this.div.scrollWidth)
            {
                this.div.scrollLeft = this.div.scrollLeft + delta;
            }
            else
            {
                this.div.scrollLeft = this.div.scrollWidth;
            }
        }
        
        if(type == ScrollTypes.Right)
        {
            if(this.div.scrollLeft - delta >= 0)
            {
                this.div.scrollLeft = this.div.scrollLeft - delta;
            }
            else
            {
                this.div.scrollLeft = 0;
            }
        }
        
        if(type == ScrollTypes.Top)
        {
            if(this.div.scrollTop + delta < this.div.scrollHeight)
            {
                this.div.scrollTop += delta;
            }
            else
            {
                this.div.scrollTop = this.div.scrollHeight;
            }
        }
        
        if(type == ScrollTypes.Bottom)
        {
            if(this.div.scrollTop - delta >= 0)
            {
                this.div.scrollTop -= delta;
            }
            else
            {
                this.div.scrollTop = 0;
            }
        }
    }
}
