﻿/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

                                                        Basic Scroller

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

var basicScrollerArray = new Array();

function BasicScroller(S1, S2, offset) {
    var newBasicScroller = new Object();
    if (S1.tagName != null) {
        newBasicScroller.container = S1;
        newBasicScroller.content = S2;
    }
    else {
        newBasicScroller.container = document.getElementById(S1);
        newBasicScroller.content = document.getElementById(S2);
    }
    newBasicScroller.contentOver = newBasicScroller.container.offsetHeight - newBasicScroller.content.offsetHeight;
    newBasicScroller.basicScrollOver = 0;
    newBasicScroller.overPercent = 0.0;
    newBasicScroller.mouseDown = false;
    newBasicScroller.mouseOver = false;

    if (window.addEventListener) {
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', basicScrollerMouseWheel, false);
    }
    else {
        /** IE/Opera. */
        window.onmousewheel = document.onmousewheel = basicScrollerMouseWheel;
    }

    newBasicScroller.basicScrollPanel = document.createElement("div");
    newBasicScroller.basicScrollPanel.id = "basicScroller" + basicScrollerArray.length;
    newBasicScroller.basicScrollPanel.className = "lluBasicScrollPanel";

    newBasicScroller.basicScrollPanelTop = document.createElement("div");
    newBasicScroller.basicScrollPanelTop.className = "lluBasicScrollPanelTop";
    newBasicScroller.basicScrollPanelTop.setAttribute("onmousedown", "setBasicScrollerMouseDown('" + (basicScrollerArray.length) + "', true, 'up')");
    newBasicScroller.basicScrollPanelTop.setAttribute("onmouseup", "setBasicScrollerMouseDown('" + (basicScrollerArray.length) + "', false, '')");

    newBasicScroller.basicScrollPanelBot = document.createElement("div");
    newBasicScroller.basicScrollPanelBot.className = "lluBasicScrollPanelBot";
    newBasicScroller.basicScrollPanelBot.setAttribute("onmousedown", "setBasicScrollerMouseDown('" + (basicScrollerArray.length) + "', true, 'down')");
    newBasicScroller.basicScrollPanelBot.setAttribute("onmouseup", "setBasicScrollerMouseDown('" + (basicScrollerArray.length) + "', false, '')");

    newBasicScroller.basicScroller = document.createElement("div");
    newBasicScroller.basicScroller.className = "lluBasicScroller";
    newBasicScroller.basicScroller.style.top = "0px";

    newBasicScroller.basicScrollPanel.appendChild(newBasicScroller.basicScrollPanelTop);
    newBasicScroller.basicScrollPanel.appendChild(newBasicScroller.basicScrollPanelBot);
    newBasicScroller.basicScrollPanel.appendChild(newBasicScroller.basicScroller);

    newBasicScroller.container.appendChild(newBasicScroller.basicScrollPanel);
    newBasicScroller.setBasicScrollerDraggable = function () {
        var currBasicScroller = newBasicScroller;
        $(newBasicScroller.basicScroller).draggable({ containment: newBasicScroller.basicScrollPanel,
            drag: function () {
                if (this.offsetTop > 0) {
                    currBasicScroller.content.style.top = (currBasicScroller.contentOver * (this.offsetTop / currBasicScroller.basicScrollOver)) + "px";
                }
                else {
                    currBasicScroller.content.style.top = "0px";
                }
                if (this.offsetTop == (currBasicScroller.basicScrollPanel.offsetHeight - currBasicScroller.basicScroller.offsetHeight)) {
                    currBasicScroller.content.style.top = (currBasicScroller.container.offsetHeight - currBasicScroller.content.offsetHeight) + "px";
                }
            }
        });
    }
    newBasicScroller.basicScrollToElement = function (basicScrollBy, multi) {
        var top = parseInt(newBasicScroller.basicScroller.style.top.replace("px", ""));
        var difference = 0;
        if (basicScrollBy < 0) {
            difference = newBasicScroller.basicScrollPanel.offsetHeight - (newBasicScroller.basicScroller.offsetTop + newBasicScroller.basicScroller.offsetHeight);
        }
        else {
            difference = newBasicScroller.basicScroller.offsetTop;
        }
        if (difference != 0) {
            if (basicScrollBy < 0) {
                newBasicScroller.basicScroller.style.top = (top + parseInt(difference / multi)) + "px";
            }
            else {
                newBasicScroller.basicScroller.style.top = (top - parseInt(difference / multi)) + "px";
            }
        }
        newBasicScroller.content.style.top = (newBasicScroller.content.offsetTop + basicScrollBy) + "px";
    }
    newBasicScroller.setScrollVisible = function (hideShow) {
        newBasicScroller.basicScrollPanel.style.display = hideShow;
        newBasicScroller.mouseDown = false;
        newBasicScroller.mouseOver = false;
    }
    newBasicScroller.setScrollHeight = function () {
        newBasicScroller.basicScroller.style.top = "0px";
        if (newBasicScroller.container.offsetHeight > 24) {
            newBasicScroller.basicScrollPanel.style.left = ((newBasicScroller.content.offsetLeft + newBasicScroller.content.offsetWidth) + offset) + "px";
            newBasicScroller.basicScrollPanel.style.height = newBasicScroller.container.offsetHeight + "px";
            newBasicScroller.contentOver = newBasicScroller.container.offsetHeight - newBasicScroller.content.offsetHeight;
            if (newBasicScroller.contentOver < 0) {
                newBasicScroller.setScrollVisible("inline");
            }
            else {
                newBasicScroller.setScrollVisible("none");
            }
            newBasicScroller.overPercent = newBasicScroller.container.offsetHeight / newBasicScroller.content.offsetHeight;
            if (newBasicScroller.overPercent > 1) {
                newBasicScroller.overPercent = 1;
            }
            newBasicScroller.basicScroller.style.height = parseInt(newBasicScroller.basicScrollPanel.offsetHeight * newBasicScroller.overPercent) + "px";
            var tempHeight = (parseInt(newBasicScroller.basicScrollPanel.offsetHeight * newBasicScroller.overPercent) - 28);
            if (tempHeight < 1) {
                tempHeight = 1;
            }
            newBasicScroller.basicScrollOver = newBasicScroller.basicScrollPanel.offsetHeight - newBasicScroller.basicScroller.offsetHeight;
        }
    }
    basicScrollerArray[basicScrollerArray.length] = newBasicScroller;
    newBasicScroller.removeBasicScroller = function () {
        for (var iScroll = 0; iScroll < basicScrollerArray.length; iScroll++) {
            if (basicScrollerArray[iScroll] == newBasicScroller) {
                basicScrollerArray.splice(iScroll, 1);
                newBasicScroller.container.parentNode.removeChild(newBasicScroller.basicScrollPanel);
            }
        }
    }
    newBasicScroller.setBasicScrollerDraggable();
    return newBasicScroller;
}

function setBasicScrollerKeyUp(event, index) {
    var currBasicScroller = basicScrollerArray[index];
    if (event.keyCode == 40 || event.keyCode == 38) {
        currScroller.keyDown = 0;
    }
    if (event.preventDefault) event.preventDefault();
    if (event.stopPropagation) event.stopPropagation();
    if (event.returnValue) event.returnValue = false;
    return false;
}

function setBasicScrollerMouseDown(index, status, dir) {
    var currBasicScroller = basicScrollerArray[index];
    currBasicScroller.mouseDown = status;
    if (status) {
        manualScroll1(index, dir);
    }
}

function setBasicScrollerMouseOver(index, status) {
    var currBasicScroller = basicScrollerArray[index];
    currBasicScroller.mouseOver = status;
}

function basicScrollerFixOnMouseOut(element, event, index) {
    var current_mouse_target = null;
    if (event.toElement) {
        current_mouse_target = event.toElement;
    } else if (event.relatedTarget) {
        current_mouse_target = event.relatedTarget;
    }
    if (!basicScrollerIsChildOf(element, current_mouse_target) && element != current_mouse_target) {
        setBasicScrollerMouseOver(index, false);
    }
}

function basicScrollerIsChildOf(parent, child) {
    if (child != null) {
        while (child.parentNode) {
            if ((child = child.parentNode) == parent) {
                return true;
            }
        }
    }
    return false;
}

function basicScrollerMouseWheel(event) {
    for (var iScroll = 0; iScroll < basicScrollerArray.length; iScroll++) {
        var currBasicScroller = basicScrollerArray[iScroll];
        if (currBasicScroller.mouseOver) {
            var delta = 0;
            if (!event) /* For IE. */
                event = window.event;
            if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta / 120;
                /** In Opera 9, delta differs in sign as compared to IE.
                */
                if (window.opera)
                    delta = -delta;
            } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                * Also, delta is multiple of 3.
                */
                delta = -event.detail / 3;
            }
            /** If delta is nonzero, handle it.
            * Basically, delta is now positive if wheel was scrolled up,
            * and negative, if wheel was scrolled down.
            */
            if (delta)
                var top = parseInt(currBasicScroller.basicScroller.offsetTop);
            if (delta > 0) {
                if (top > 0) {
                    currBasicScroller.basicScroller.style.top = (top - 11) + "px";
                    currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                }
                else {
                    currBasicScroller.content.style.top = "0px";
                }
            }
            else {
                if (top < (currBasicScroller.basicScrollPanel.offsetHeight - currBasicScroller.basicScroller.offsetHeight)) {
                    currBasicScroller.basicScroller.style.top = (top + 11) + "px";
                    currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                }
                else {
                    currBasicScroller.content.style.top = (currBasicScroller.container.offsetHeight - currBasicScroller.content.offsetHeight) + "px";
                }
            }
            /** Prevent default actions caused by mouse wheel.
            * That might be ugly, but we handle scrolls somehow
            * anyway, so don't bother here..
            */
            if (event.preventDefault) event.preventDefault();
            if (event.stopPropagation) event.stopPropagation();
            if (event.returnValue) event.returnValue = false;
            return false;
        }
    }
}

function manualScroll1(index, dir) {
    var currBasicScroller = basicScrollerArray[index];
    if (currBasicScroller.mouseDown) {
        var top = parseInt(currBasicScroller.basicScroller.offsetTop);
        if (dir == "up") {
            if (top > 0) {
                currBasicScroller.basicScroller.style.top = (top - 1) + "px";
                currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                setTimeout("manualScroll1('" + index + "', 'up')", 10);
            }
            else {
                currBasicScroller.content.style.top = "0px";
            }
        }
        if (dir == "down") {
            if (top < (currBasicScroller.basicScrollPanel.offsetHeight - currBasicScroller.basicScroller.offsetHeight)) {
                currBasicScroller.basicScroller.style.top = (top + 1) + "px";
                currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                setTimeout("manualScroll1('" + index + "', 'down')", 10);
            }
            else {
                currBasicScroller.content.style.top = (currBasicScroller.container.offsetHeight - currBasicScroller.content.offsetHeight) + "px";
            }
        }
    }
}

function manualScroll2(index, dir) {
    var currBasicScroller = basicScrollerArray[index];
    if (currBasicScroller.mouseOver) {
        var top = parseInt(currBasicScroller.basicScroller.offsetTop);
        if (dir == "up") {
            if (top > 0) {
                currBasicScroller.basicScroller.style.top = (top - 1) + "px";
                currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                setTimeout("manualScroll1('" + index + "', 'up')", 10);
            }
            else {
                currBasicScroller.content.style.top = "0px";
            }
        }
        if (dir == "down") {
            if (top < (currBasicScroller.basicScrollPanel.offsetHeight - currBasicScroller.basicScroller.offsetHeight)) {
                currBasicScroller.basicScroller.style.top = (top + 1) + "px";
                currBasicScroller.content.style.top = (currBasicScroller.contentOver * (top / currBasicScroller.basicScrollOver)) + "px";
                setTimeout("manualScroll1('" + index + "', 'down')", 10);
            }
            else {
                currBasicScroller.content.style.top = (currBasicScroller.container.offsetHeight - currBasicScroller.content.offsetHeight) + "px";
            }
        }
    }
}
