﻿
function ArrowsScroll_ScrollUp() {
    if (this.ScrollingEnabled) {
        var top = parseInt(this.Content.style.top);

        if (top < 0) {
            this.Content.style.top = top + this.ScrollSpeed + "px";
        }

        this.RefreshArrows(this.ArrowUp);

        var reference = this;

        this.ScrollTimer = setTimeout(function () { reference.ScrollUp(); }, 10);
    }
}

function ArrowsScroll_ScrollDown() {
    if (this.ScrollingEnabled) {
        var top = parseInt(this.Content.style.top);
        var offsetHeight = this.Content.offsetHeight;
        var height = this.Container.offsetHeight;

        if (top >= (offsetHeight * (-1) + height)) {
            this.Content.style.top = top - this.ScrollSpeed + "px";
        }

        this.RefreshArrows(this.ArrowDown);

        var reference = this;

        this.ScrollTimer = setTimeout(function () { reference.ScrollDown(); }, 10);
    }
}

function ArrowsScroll_RefreshArrows(selectedArrow) {
    this.LoadElements();

    var top = parseInt(this.Content.style.top);
    var offsetHeight = this.Content.offsetHeight;
    var height = this.Container.offsetHeight;

    var showArrowUp = false;  
    var showArrowDown = false;

    var arrowUpVisibleBefore = (this.ArrowUp.style.display == "inline");
    var arrowDownVisibleBefore = (this.ArrowDown.style.display == "inline");

    this.ArrowUp.style.display = "none";
    this.ArrowDown.style.display = "none";

    if (height > 0) {
        showArrowUp = (top < 0);
        showArrowDown = (top > (offsetHeight * (-1) + height));
    }

    if (showArrowUp || showArrowDown) {
        if ((selectedArrow == this.ArrowUp) && showArrowUp && this.ScrollingEnabled) {
            this.ArrowUp.style.display = "inline";

            if (this.ArrowUpImageName.length > 0) {
                this.ArrowUp.src = this.ImageDir + "/" + this.ArrowUpImageName;
            }
        }
        else {
            if (this.ArrowUpImageName.length > 0) {
                this.ArrowUp.style.display = "inline";
                this.ArrowUp.src = this.ImageDir + "/" + this.ArrowUpGreyImageName;
            }
            else {
                this.ArrowUp.style.display = "none";
            }
        }


        if ((selectedArrow == this.ArrowDown) && showArrowDown && this.ScrollingEnabled) {
            this.ArrowDown.style.display = "inline";

            if (this.ArrowDownImageName.length > 0) {
                this.ArrowDown.src = this.ImageDir + "/" + this.ArrowDownImageName;
            }
        }
        else {
            if (this.ArrowDownImageName.length > 0) {
                this.ArrowDown.style.display = "inline";
                this.ArrowDown.src = this.ImageDir + "/" + this.ArrowDownGreyImageName;
            }
            else {
                this.ArrowDown.style.display = "none";
            }
        }

        var containerHeight = parseInt(this.Container.style.height);
    }

    var arrowUpVisibleAfter = (this.ArrowUp.style.display == "inline");
    var arrowDownVisibleAfter = (this.ArrowDown.style.display == "inline");

    if (arrowUpVisibleBefore != arrowUpVisibleAfter) {
        if (arrowUpVisibleAfter) {
            this.OnArrowUpShow();
        }
        else {
            this.OnArrowUpHide();
        }
    }

    if (arrowDownVisibleBefore != arrowDownVisibleAfter) {
        if (arrowDownVisibleAfter) {
            this.OnArrowDownShow();
        }
        else {
            this.OnArrowDownHide();
        }
    }
}

function ArrowsScroll_DisableScrolling(selectedArrow) {
    this.ScrollingEnabled = false;

    if (this.ScrollTimer) {
        clearTimeout(this.ScrollTimer);
        this.ScrollTimer = null;
    }

    this.RefreshArrows(selectedArrow);
}

function ArrowsScroll_LoadElements() {
    this.Container = document.getElementById(this.ContainerId);
    this.Content = document.getElementById(this.ContentId);
    this.ArrowUp = document.getElementById(this.ArrowUpId);
    this.ArrowDown = document.getElementById(this.ArrowDownId);
}

function ArrowsScroll_OnArrowUpShow() {
    var height = parseInt(this.Container.style.height);

    if (height > 0) {
        height -= this.ImageHeight;

        this.Container.style.height = height + "px";
    }
}

function ArrowsScroll_OnArrowUpHide() {
    var height = parseInt(this.Container.style.height);

    if (height > 0) {
        height += this.ImageHeight;

        this.Container.style.height = height + "px";
    }
}

function ArrowsScroll_OnArrowDownShow() {
    var height = parseInt(this.Container.style.height);

    if (height > 0) {
        height -= this.ImageHeight;

        this.Container.style.height = height + "px";
    }
}

function ArrowsScroll_OnArrowDownHide() {
    var height = parseInt(this.Container.style.height);

    if (height > 0) {
        height += this.ImageHeight;

        this.Container.style.height = height + "px";
    }
}

function ArrowsScroll(scrollerId, containerDivId, contentDivId, arrowUpId, arrowDownId) {
    this.Id = scrollerId;
    this.ContainerId = containerDivId;
    this.ContentId = contentDivId;
    this.ArrowUpId = arrowUpId;
    this.ArrowDownId = arrowDownId;
    this.Container = null;
    this.Content = null;
    this.ArrowUp = null;
    this.ArrowDown = null;
    this.LoadElements = ArrowsScroll_LoadElements;
    this.ScrollUp = ArrowsScroll_ScrollUp;
    this.ScrollDown = ArrowsScroll_ScrollDown;
    this.ScrollSpeed = 3;
    this.ScrollTimer = null;
    this.RefreshArrows = ArrowsScroll_RefreshArrows;
    this.ImageDir = "./images";
    this.ArrowUpImageName = "arrowup.gif";
    this.ArrowUpGreyImageName = "arrowup_grey.gif";
    this.ArrowDownImageName = "arrowdown.gif";
    this.ArrowDownGreyImageName = "arrowdown_grey.gif";
    this.ScrollingEnabled = false;
    this.DisableScrolling = ArrowsScroll_DisableScrolling;
    this.OnArrowUpShow = ArrowsScroll_OnArrowUpShow;
    this.OnArrowUpHide = ArrowsScroll_OnArrowUpHide;
    this.OnArrowDownShow = ArrowsScroll_OnArrowDownShow;
    this.OnArrowDownHide = ArrowsScroll_OnArrowDownHide;
    this.ImageHeight = 24;

    var reference = this;

    Event.observe(window, 'load',
    function () {
        reference.RefreshArrows();

        reference.ArrowUp.onmouseover = function () { reference.RefreshArrows(reference.ArrowUp); };
        reference.ArrowUp.onmouseout =
        function () {
            reference.DisableScrolling(reference.ArrowUp);
        };

        reference.ArrowDown.onmouseover = function () { reference.RefreshArrows(reference.ArrowDown); };
        reference.ArrowDown.onmouseout =
        function () {
            reference.DisableScrolling(reference.ArrowDown);
        };

        reference.ArrowUp.onmousedown = function () { reference.ScrollingEnabled = true; reference.ScrollUp(); };
        reference.ArrowDown.onmousedown = function () { reference.ScrollingEnabled = true; reference.ScrollDown(); };

        reference.ArrowUp.ondragstart = function () { return false; }
        reference.ArrowDown.ondragstart = function () { return false; }

        reference.ArrowUp.onmouseup =
        function () {
            reference.DisableScrolling(this.ArrowUp);
        };

        reference.ArrowDown.onmouseup =
        function () {
            reference.DisableScrolling(this.ArrowDown);
        };


    });
}

function _ArrowsScrollManager() {
    this.Items = new Array();
    this.Get = ArrowsScrollManager_Get;
    this.GetList = ArrowsScrollManager_GetList;
    this.AddItem = ArrowsScrollManager_AddItem;
}

var ArrowsScrollManager = new _ArrowsScrollManager();

function ArrowsScrollManager_Get(scrollerId) {
    for (var i = 0; i < this.Items.length; i++) {
        if (this.Items[i].Id == scrollerId) {
            return this.Items[i];
        }
    }

    return null;
}

function ArrowsScrollManager_GetList(scrollerId) {
    var list = new Array();

    for (var i = 0; i < this.Items.length; i++) {
        if (this.Items[i].Id == scrollerId) {
            list.push(this.Items[i]);
        }
    }

    return list;
}

function ArrowsScrollManager_AddItem(item) {
    this.Items.push(item);
}







