function jsScroller(_, $, C, index){
	if(!index) index = "";
    var B = this, A = _.getElementsByTagName("div");
    for (var D = 0; D < A.length; D++) 
        if (A[D].className.indexOf("Scroller-Container"+index) > -1) 
            _ = A[D];
    this._setPos = function(A, $){
        if (A < this.viewableWidth - this.totalWidth) 
            A = this.viewableWidth - this.totalWidth;
        if (A > 0) 
            A = 0;
        if ($ < this.viewableHeight - this.totalHeight) 
            $ = this.viewableHeight - this.totalHeight;
        if ($ > 0) 
            $ = 0;
        this._x = A;
        this._y = $;
		_.style.left = this._x + "px";
		_.style.top = this._y + "px";
    };
    this.reset = function(){
        this.content = _;
        this.totalHeight = _.offsetHeight;
        this.totalWidth = _.offsetWidth;
        this._x = 0;
        this._y = 0;
        with (_.style) {
            left = "0px";
            top = "0px"
        }
    };
    this.scrollBy = function(_, $){
        this._setPos(this._x + _, this._y + $)
    };
    this.scrollTo = function(_, $){
        this._setPos(-_, -$)
    };
    this.stopScroll = function(){
        if (this.scrollTimer) 
            window.clearInterval(this.scrollTimer)
    };
    this.startScroll = function(_, $){
        this.stopScroll();
        this.scrollTimer = window.setInterval(function(){
            B.scrollBy(_, $)
        }, 40)
    };
    this.swapContent = function(A, $, C){
        _ = A;
        var B = _.getElementsByTagName("div");
        for (var D = 0; D < B.length; D++) 
            if (B[D].className.indexOf("Scroller-Container"+index) > -1) 
                _ = B[D];
        if ($) 
            this.viewableWidth = $;
        if (C) 
            this.viewableHeight = C;
        this.reset()
    };
    this.content = _;
    this.viewableWidth = $;
    this.viewableHeight = C;
    this.totalWidth = _.offsetWidth;
    this.totalHeight = _.offsetHeight;
    this.scrollTimer = null;
    this.reset()
}

function jsScrollbar(B, _, A, index, $){
	if(!index) index = "";
    var self = this;
    this.reset = function(){
        this._parent = B;
        this._src = _;
        this.auto = A ? A : false;
        this.eventHandler = $ ? $ : function(){
        };
        this._up = this._findComponent("Scrollbar-Up"+index, this._parent);
        this._down = this._findComponent("Scrollbar-Down"+index, this._parent);
        this._yTrack = this._findComponent("Scrollbar-Track"+index, this._parent);
        this._yHandle = this._findComponent("Scrollbar-Handle"+index, this._yTrack);
        this._trackTop = C(this._yTrack);
        this._trackHeight = this._yTrack.offsetHeight;
        this._handleHeight = this._yHandle.offsetHeight;
        this._x = 0;
        this._y = 0;
        this._scrollDist = 5;
        this._scrollTimer = null;
        this._selectFunc = null;
        this._grabPoint = null;
        this._tempTarget = null;
        this._tempDistX = 0;
        this._tempDistY = 0;
        this._disabled = false;
        this._ratio = (this._src.totalHeight - this._src.viewableHeight) / (this._trackHeight - this._handleHeight);
        this._yHandle.ondragstart = function(){
            return false
        };
        this._yHandle.onmousedown = function(){
            return false
        };
        //this._addEvent(this._src.content, "mousewheel", this._scrollbarWheel);
        this._removeEvent(this._parent, "mousedown", this._scrollbarClick);
        this._addEvent(this._parent, "mousedown", this._scrollbarClick);
        this._src.reset();
        with (this._yHandle.style) {
            top = "0px";
            left = "0px"
        }
        this._moveContent();
        if (this._src.totalHeight < this._src.viewableHeight) {
            this._disabled = true;
            this._yHandle.style.visibility = "hidden";
            if (this.auto) 
                this._parent.style.visibility = "hidden"
        }
        else {
            this._disabled = false;
            this._yHandle.style.visibility = "visible";
            this._parent.style.visibility = "visible"
        }
    };
    this._addEvent = function(A, _, $){
        if (A.addEventListener) 
            A.addEventListener(_, $, false);
        else 
            if (A.attachEvent) 
                A.attachEvent("on" + _, $);
            else 
                A["on" + _] = $
    };
    this._removeEvent = function(A, _, $){
        if (A.removeEventListener) 
            A.removeEventListener(_, $, false);
        else 
            if (A.detachEvent) 
                A.detachEvent("on" + _, $);
            else 
                A["on" + _] = null
    };
    this._findComponent = function($, _){
        var A = _.childNodes;
        for (var B = 0; B < A.length; B++) 
            if (A[B].className && A[B].className == $) 
                return A[B]
    };
    function C(_){
        var $ = 0;
        if (_.offsetParent) 
            while (_.offsetParent) {
                $ += _.offsetTop;
                _ = _.offsetParent
            }
        return $
    }
    this._scrollbarClick = function($){
        if (self._disabled) 
            return false;
        $ = $ ? $ : event;
        if (!$.target) 
            $.target = $.srcElement;
        if ($.target.className.indexOf("Scrollbar-Up"+index) > -1) 
            self._scrollUp($);
        else 
            if ($.target.className.indexOf("Scrollbar-Down"+index) > -1) 
                self._scrollDown($);
            else 
                if ($.target.className.indexOf("Scrollbar-Track"+index) > -1) 
                    self._scrollTrack($);
                else 
                    if ($.target.className.indexOf("Scrollbar-Handle"+index) > -1) 
                        self._scrollHandle($);
        self._tempTarget = $.target;
        self._selectFunc = document.onselectstart;
        document.onselectstart = function(){
            return false
        };
        self.eventHandler($.target, "mousedown");
        self._addEvent(document, "mouseup", self._stopScroll, false);
        return false
    };
    this._scrollbarDrag = function(_){
        _ = _ ? _ : event;
        var $ = parseInt(self._yHandle.style.top), v = _.clientY + document.body.scrollTop - self._trackTop;
        with (self._yHandle.style) {
            if (v >= self._trackHeight - self._handleHeight + self._grabPoint) 
                top = self._trackHeight - self._handleHeight + "px";
            else 
                if (v <= self._grabPoint) 
                    top = "0px";
                else 
                    top = v - self._grabPoint + "px";
            self._y = parseInt(top)
        }
        self._moveContent()
    };
    this._scrollbarWheel = function($){
        $ = $ ? $ : event;
        var _ = 0;
        if ($.wheelDelta >= 120) 
            _ = -1;
        if ($.wheelDelta <= -120) 
            _ = 1;
        self.scrollBy(0, _ * 20);
        $.returnValue = false
    };
    this._startScroll = function(_, $){
        this._tempDistX = _;
        this._tempDistY = $;
        this._scrollTimer = window.setInterval(function(){
            self.scrollBy(self._tempDistX, self._tempDistY)
        }, 20)
    };
    this._stopScroll = function(){
        self._removeEvent(document, "mousemove", self._scrollbarDrag, false);
        self._removeEvent(document, "mouseup", self._stopScroll, false);
        if (self._selectFunc) 
            document.onselectstart = self._selectFunc;
        else 
            document.onselectstart = function(){
                return true
            };
        if (self._scrollTimer) 
            window.clearInterval(self._scrollTimer);
        self.eventHandler(self._tempTarget, "mouseup")
    };
    this._scrollUp = function($){
        this._startScroll(0, -this._scrollDist)
    };
    this._scrollDown = function($){
        this._startScroll(0, this._scrollDist)
    };
    this._scrollTrack = function(_){
        var $ = _.clientY + document.body.scrollTop;
        this._scroll(0, $ - this._trackTop - this._handleHeight / 2)
    };
    this._scrollHandle = function(_){
        var $ = _.clientY + document.body.scrollTop;
        this._grabPoint = $ - C(this._yHandle);
        this._addEvent(document, "mousemove", this._scrollbarDrag, false)
    };
    this._scroll = function(_, $){
        if ($ > this._trackHeight - this._handleHeight) 
            $ = this._trackHeight - this._handleHeight;
        if ($ < 0) 
            $ = 0;
        this._yHandle.style.top = $ + "px";
        this._y = $;
        this._moveContent()
    };
    this._moveContent = function(){
        this._src.scrollTo(0, Math.round(this._y * this._ratio))
    };
    this.scrollBy = function(_, $){
        this._scroll(0, (-this._src._y + $) / this._ratio)
    };
    this.scrollTo = function(_, $){
        this._scroll(0, $ / this._ratio)
    };
    this.swapContent = function(_, $, A){
        this._removeEvent(this._src.content, "mousewheel", this._scrollbarWheel, false);
        this._src.swapContent(_, $, A);
        this.reset()
    };
    this.reset()
}
