﻿ScrollPanel = function(divId, leftButtonID, rightButtonID, topButtonID, bottomButtonID, autoHideArrows)
{
    this.divId = divId;
    this.leftButtonID = leftButtonID;
    this.rightButtonID = rightButtonID;
    this.topButtonID = topButtonID;
    this.bottomButtonID = bottomButtonID;
    this.ScrollController = null;
    this.DivContentController = null;
    this.StartMove = function(dirDown, dirRight)
    {
        var moveScript = "ScrollPanel.ScrollDiv(\'" + this.divId + "\'," + dirDown.toString() + "," + dirRight.toString() + ");";
        this.ScrollController = setInterval(moveScript, 30);
    }
    this.SetButton = function(buttonID, dirDown, dirRight)
    {
        if (buttonID && $Get(buttonID))
        {
            $Get(buttonID).ScrollPanel = this;
            $Get(buttonID).onclick = 'return false;';
            $Get(buttonID).onmouseover = new Function('obj', 'this.ScrollPanel.StartMove(' + dirDown.toString() + ',' + dirRight.toString() + ');return false;');
            $Get(buttonID).onmouseout = new Function('obj', 'this.ScrollPanel.ClearScrollController();');
            $Get(buttonID).onclick = new Function('obj', 'ScrollPanel.ScrollDiv(this.ScrollPanel.divId, 60*' + dirDown.toString() + ',60*' + dirRight.toString() + ');return false;');
        }
    }
    this.ClearScrollController = function()
    {
        clearInterval(this.ScrollController);
        this.ScrollController = null;
    }

    this.SetButton(this.leftButtonID, 0, -4);
    this.SetButton(this.rightButtonID, 0, 4);
    this.SetButton(this.topButtonID, -4, 0);
    this.SetButton(this.bottomButtonID, 4, 0);
    $Get(this.divId).ScrollPanel = this;
    this.DivContentController = setInterval('ScrollPanel.DivContentTimer(\'' + this.divId + '\');', 600);
}
ScrollPanel.DivContentTimer = function(divId)
{
    var div = $Get(divId);
    ScrollPanel.RefreshArrows(div.ScrollPanel);
}
ScrollPanel.RefreshArrows = function(sp)
{
    var div = $Get(sp.divId);
    var toggleIt = function(obj, show) { if (show) obj.style.display = ''; else obj.style.display = 'none'; }
    var maxScrolls = [div.scrollWidth - div.offsetWidth, div.scrollHeight - div.offsetHeight];

    if (sp.leftButtonID && $Get(sp.leftButtonID)) toggleIt($Get(sp.leftButtonID), (div.scrollLeft > 0));
    if (sp.rightButtonID && $Get(sp.rightButtonID)) toggleIt($Get(sp.rightButtonID), (div.scrollLeft < maxScrolls[0]));
    if (sp.topButtonID && $Get(sp.topButtonID)) toggleIt($Get(sp.topButtonID), (div.scrollTop > 0));
    if (sp.bottomButtonID && $Get(sp.bottomButtonID)) toggleIt($Get(sp.bottomButtonID), (div.scrollTop < maxScrolls[1]));
}
ScrollPanel.ScrollDiv = function(divID, dirDown, dirRight)
{
    var div = $Get(divID);
    if (dirDown != 0)
    {
        var top = div.scrollTop;
        top += dirDown;
        div.scrollTop = Math.max(0, top);
    }
    if (dirRight != 0)
    {
        var left = div.scrollLeft;
        left += dirRight;
        div.scrollLeft = Math.max(0, left);
    }
    ScrollPanel.RefreshArrows(div.ScrollPanel);
}

//var xptoScroll = new ScrollPanel('divXpto','lnkBack','lnkFwd');
