﻿var Scroll3 = Class.create();
Object.extend(Scroll3, {
    build: function(object, speed) {
        object = $(object);

        var outer = $Class(object, "outer3");
        var inner = $Class(object, "inner3");
        var up = $Class(object, "up3");
        var barouter = $Class(object, "barouter3");
        var barinner = $Class(object, "barinner3");
        var down = $Class(object, "down3");

        if (!outer || !inner) return;
        var _textContainerHeight = outer.offsetHeight;
        var _textHeight = inner.offsetHeight;

        if (barouter) {
            var _barMaxHeight = barouter.offsetHeight - parseInt(Element.getStyle(barouter, "padding-top")) - parseInt(Element.getStyle(barouter, "padding-bottom"));
            var _barHeight = barinner.offsetHeight;

            var _maxOffsetTop = _textHeight - _textContainerHeight;
            var _maxBarTop = _barMaxHeight - _barHeight;

            Scroll3.repositionBar(object);
        }

        if (barouter) {
            Event.observe(document, 'mousemove', Mouse.FindMousePosition, false);
            iebody = document.documentElement ? document.documentElement : document.getElementsByTagName("body")[0];

            barinner.onmousedown = function(e) {
                var _barContainerTop = Position.cumulativeOffset(barouter)[1] + Math.abs(Mouse.y - Position.cumulativeOffset(barinner)[1] + (document.all ? iebody.scrollTop : 0));

                var f = function(e) {
                    Scroll3._clearDocumentSelection();
                    Mouse.FindMousePosition(e);

                    var _dY = Mouse.y - _barContainerTop;
                    _dY += document.all ? iebody.scrollTop : 0;

                    if (_dY > _maxBarTop) _dY = _maxBarTop;
                    if (_dY < 0) _dY = 0;

                    var _y = _dY * _maxOffsetTop / _maxBarTop;
                    _y = (_y > 0) ? _y : 0;

                    outer.scrollTop = Math.round(_y);
                    Element.setStyle(barinner, { "margin-top": Math.round(_dY) + "px" });
                    return true;
                }

                var g = function(e) {
                    Event.stopObserving(document, 'mousemove', f, false);
                    Event.stopObserving(document, 'mouseup', g, false);
                }

                Event.observe(document, 'mousemove', f, false);
                Event.observe(document, 'mouseup', g, false);
            };

            barinner.mouseup = function() {
                Event.stopObserving(document, 'mousemove', function() { }, false);
                Event.stopObserving(document, 'mouseup', function() { }, false);
            };
        }

        var fixedScroll = function(d) {
            outer.scrollTop = outer.scrollTop + d * speed;

            if (barinner) {
                var _dY = outer.scrollTop * _maxBarTop / _maxOffsetTop;
                _dY = Math.round(_dY);
                if (_dY > _maxBarTop) _dY = _maxBarTop;
                if (_dY < 0) _dY = 0;
                Element.setStyle(barinner, { "margin-top": _dY + "px" });
            }
        };

        if (up) {
            up.onmousedown = function() {
                if (up._t) clearInterval(up._t);
                if (down._t) clearInterval(down._t);
                up._t = setInterval(function() { fixedScroll(-1); }, 100);
            };

            up.onmouseup = function() {
                if (up._t) clearInterval(up._t);
            };

            up.onmouseout = function() {
                if (up._t) clearInterval(up._t);
            };
        }

        if (down) {
            down.onmousedown = function() {
                if (up._t) clearInterval(up._t);
                if (down._t) clearInterval(down._t);
                down._t = setInterval(function() { fixedScroll(1); }, 100);
            };

            down.onmouseup = function() {
                if (down._t) clearInterval(down._t);
            };

            down.onmouseout = function() {
                if (down._t) clearInterval(down._t);
            };
        }
    },

    repositionBar: function(object) {
        object = $(object);

        var outer = $Class(object, "outer3");
        var inner = $Class(object, "inner3");
        var barouter = $Class(object, "barouter3");
        var barinner = $Class(object, "barinner3");

        var _textContainerHeight = outer.offsetHeight;
        var _textHeight = inner.offsetHeight;
        var _barMaxHeight = barouter.offsetHeight - parseInt(Element.getStyle(barouter, "padding-top")) - parseInt(Element.getStyle(barouter, "padding-bottom"));
        var _barHeight = barinner.offsetHeight;

        var _maxOffsetTop = _textHeight - _textContainerHeight;
        var _maxBarTop = _barMaxHeight - _barHeight;

        outer.scrollTop = outer.scrollTop > _maxOffsetTop ? _maxOffsetTop : outer.scrollTop;
        var _dY = _maxBarTop * outer.scrollTop / _maxOffsetTop;
        if (_dY > _maxBarTop) _dY = _maxBarTop;
        Element.setStyle(barinner, { "margin-top": Math.round(_dY) + "px" });
    },

    /* <ClearSealections> */
    _clearDocumentSelection: function() {
        if (document.selection) {
            if (document.selection.clear) document.selection.clear();
            else if (document.selection.empty) document.selection.empty();
        } else {
            if (window.getSelection) {
                try {
                    window.getSelection().collapse();
                    window.getSelection().removeAllRanges();
                } catch (e) { };
            }
        }
    }
    /* </ClearSealections> */
});