// <!-- momentum kreativagentur, dynscroll.js, copyright 2004, alle rechte vorbehalten -->

// layout vars, overwrite in html
var scrollerBG = "global/images/scrollbg.gif";
var upSRC      = "global/images/scrollup.gif";

// quelle der up rollover grafik
// var up-onSRC = "images/scrollup-on.gif";
var downSRC  = "global/images/scrolldn.gif";

// quelle der down rollover grafik
// var down-onSRC = "images/scrolldn-on.gif";
var scrollSRC  = "global/images/scrollelm.gif";

var scrollerHeight = 30;
var leftOffset = 0;
var extraSpace = 20;
var scrollbarWidth = 6;
var scrollArrows = true;
var scrollCursor = 'hand';

// end of layout
var activeBar = null;
var scrollCount = 0;
var areaCount = 0;
var useBar = null;
var arrowActivator = "onmousedown"
document.bars = [];

function scrollbar(x, y, w, h, nest) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.eh = scrollerHeight;

        this.sPos = scrollArrows? this.w:0;
        this.sOff = scrollArrows? (2*this.w):0

        this.currentLayer = null;
        this.currentLyrId = "";
        this.contentHeight = 0;
        this.availHeight = 0;
        this.scrolling = false;
        this.speed = 0;
        this.nested = nest;
        this.build();
}

scrollProto = scrollbar.prototype;
scrollProto.build = function() {
        this.id = 'bar'+scrollCount;
        this.cId = this.id + 'container';

        with(this.container = new dynObject(this.cId, this.nested)) {
        
            // die neuen scrollarrows sind 9x9px gross und sollen unter dem scrollbalken angeordnet werden //
            // --> - 9 positioniert den kompletten scrollercontainer 9px weiter oben //
                // --> + 9 verlaengert den scroller 9px nach unten //
                setProperties(this.x, this.y - 9, this.w, this.h +9, 'visible', false, 1000);
                
                // scroller wird 12px weiter unten geclipt //
                clipTo(0,this.w,this.h + 12,0);
        }

        with(this.track = new dynObject(this.cId+'track', this.cId)) {
                setProperties(0,0, this.w, this.h, 'visible');
                setBackground(scrollerBG);
                
                // 0 --> 4 scrollerhintergrund beginnt dadurch erst 4px von oben //
                // --> - 20 scrollerhintergrund bekommt 20px abstand nach unten //
        }        this.setupElement(this.track, 0, 4, this.w, this.h - 20,'dynTrackScroll()');

        if(scrollArrows) {
                this.upArrow = new dynObject(this.id + 'upArrow', this.cId);
                this.downArrow = new dynObject(this.id + 'downArrow', this.cId);
                
                // 0 --> (this.h - this.w) upscroller wird unter dem scrollbalken positioniert //
                this.setupElement(this.upArrow, 0, (this.h - this.w), this.w, this.w, 'dynScrollBy(-1)', upSRC);
                
                // --> + 12 downscroller wird weiter unten positioniert (unter dem upscroller) //
                this.setupElement(this.downArrow, 0, (this.h - this.w + 12 ), this.w, this.w, 'dynScrollBy(1)', downSRC);
        }

        this.scroller  = new dynObject(this.id + 'scroller', this.cId);
        this.setupElement(this.scroller, 0, this.sPos, this.w, scrollerHeight, false, scrollSRC);
        this.availHeight = this.h - this.sOff - scrollerHeight;
        
        // --> - 12 der scrollbereich nach unten wird eingeschraenkt //
        this.scroller.limitMovement(0, this.sPos, this.w, this.h - this.sPos - 12);
        this.scroller.enableDragDrop('vertical');

        document.bars[scrollCount] = this;
        scrollCount ++;
}

scrollProto.setupElement = function(elm, x, y, w, h, mouseFunction, img) {
        elm.setProperties(x, y, w, h, 'visible');
        elm.clipTo(0,w,h,0);
        elm.css.visibility = "inherit";
        elm.scrollbar = this;
        elm.css.cursor = scrollCursor;
        elm.attachEvent('onmousedown', 'dynGetScrollbar()');
        elm.attachEvent('onmouseup', 'dynStopScroll()');
        if(img) {
                if(is.NS4) elm.setBackground(img);
                else elm.createImage(img, 0, w, h);
        }

        if(mouseFunction) {
                elm.attachEvent(arrowActivator, 'dynGetScrollbar()');
                elm.attachEvent('onmouseout', 'dynStopScroll()')
                elm.attachEvent(arrowActivator, mouseFunction);
        }
}

scrollProto.setHeight = function(h) {
        this.h = h;
        with(this.container) {
                resizeTo(this.w, this.h);
                clipTo(0, this.w, this.h, 0);
        }

        this.downArrow.moveTo(0,(this.h-this.w));
        this.availHeight = this.h - this.sOff - scrollerHeight;
        this.setLayer(this.currentLayer.id)

        with(this.scroller) {
                moveTo(0,this.sPos);
                limitMovement(0, this.sPos, this.w, this.h - this.sPos);
        }
}

function dynScrollBy(dir, target) {
        if(useBar && useBar.needed) {
                var useDy = dir * useBar.speed;
                useBar.scroller.moveBy(0, useDy);
                dynHandleScroll();

                if(!target || Math.abs(useBar.scroller.y - target) > 7)
                setTimeout('dynScrollBy('+dir+','+(target || 0)+')', 40);
        }
}

function dynTrackScroll() {
        if(useBar && useBar.needed && arrowActivator != 'onmouseover') {
                var dy = activeObj.clickY - useBar.eh/2 - useBar.scroller.y
                dynScrollBy((dy < 0)? -2.5:2.5, activeObj.clickY - useBar.eh/2)
        }
}

        function dynWheelStep(dir) {
                useBar = activeBar || document.bars[0];
                if(useBar && useBar.needed) {
                        useBar.scroller.moveBy(0, dir);
                        dynHandleScroll();
                }
                dynStopScroll();
        }

function dynStopScroll() {
        useBar = false;
}

function dynGetScrollbar() {
        useBar = activeObj.scrollbar;
        activeBar = useBar;
}

scrollProto.setLayer = function(to) {
        if(!document.dyn[to]) {        new dynObject(to, null, true); }
        if(this.currentLayer) {        this.currentLayer.setVisible(false); }

        this.currentLayer = document.dyn[to];
        this.currentLyrId = to;

        with(this.currentLayer) {
                moveTo(leftOffset, 0);
                getDimensions();
                this.speed = (this.h/h)*7;
                this.pageHeight = this.availHeight/(h/this.h);
        }

        this.scroller.moveTo(0, this.sPos);
        this.contentHeight = this.currentLayer.h - this.h + extraSpace;
        this.currentLayer.setVisible(true);
        this.needed = (this.contentHeight <= 0)? false:true;
        this.container.setVisible(this.needed);
}

function dynHandleScroll() {
        if(useBar && useBar.currentLayer) {
                contentY = ((useBar.scroller.y - useBar.sPos)/
                        useBar.availHeight) * useBar.contentHeight;
                useBar.currentLayer.moveTo(leftOffset, - contentY)
        }
}

document.attachEvent('onmousemove', 'dynHandleScroll()');
document.attachEvent('onmouseup', 'dynStopScroll()');
document.onkeydown = doKeyDown;

if(is.IE6) { document.onmousewheel = doWheel; }

function doWheel() {
        var delta = event.wheelDelta;
        if(delta) dynWheelStep(-delta/15);
}

var KU = is.NS4? 56:38;
var KD = is.NS4? 50:40;
var PU = is.NS4? 1000:33;
var PD = is.NS4? 1000:34;

function doKeyDown(e) {
        useBar = activeBar || document.bars[0];
        var key = is.IE? event.keyCode:e.which;
        if(key == KU) dynWheelStep(-2);
        if(key == KD) dynWheelStep(2);
        if(key == PU) dynWheelStep(-useBar.pageHeight);
        if(key == PD) dynWheelStep(useBar.pageHeight);
}