var scrollEngaged = false;
var scrollInterval;
var scrollBars = new Array();

function scrollBar(ownerID, ownerContentID, leftID, rightID) {
    this.ownerID = ownerID;
    this.ownerContentID = ownerContentID;
    this.index = scrollBars.length;
    this.leftButton = document.getElementById(leftID);
    this.rightButton = document.getElementById(rightID);
    this.leftButton.index = this.index;
    this.rightButton.index = this.index;
    
    this.ownerWidth = parseInt(DHTMLAPI.getComputedStyle(this.ownerID, "width"));

    this.contentElem = document.getElementById(ownerContentID);
    // this.contentStepSize = parseInt(DHTMLAPI.getComputedStyle(this.ownerContentID,"font-size"));
    // this.contentFastStepSize = 8;
    this.contentSlowStepSize = 3;
    this.contentStepSize = this.contentSlowStepSize; // step size

    this.contentScrollWidth = (this.contentElem.scrollWidth) ? this.contentElem.scrollWidth : this.contentElem.offsetWidth;
    setScrollEvents(this.leftButton, this.rightButton);
}

function setScrollEvents(leftButton, rightButton) {
    // addEvent(leftButton, "mousedown", handleScrollClick, false);
    addEvent(leftButton, "mouseover", handleScrollClick, false); // !
    // addEvent(leftButton, "mouseup", handleScrollStop, false);
    addEvent(leftButton, "mouseout", handleScrollStop, false); // !
    addEvent(leftButton, "contextmenu", blockEvent, false);
    
    // addEvent(rightButton, "mousedown", handleScrollClick, false);
    addEvent(rightButton, "mouseover", handleScrollClick, false); // !
    // addEvent(rightButton, "mouseup", handleScrollStop, false);
    addEvent(rightButton, "mouseout", handleScrollStop, false); // !
    addEvent(rightButton, "contextmenu", blockEvent, false);
}

function handleScrollStop() {
    scrollEngaged = false;
}

function blockEvent(evt) {
    evt = (evt) ? evt : event;
    evt.cancelBubble = true;
    return false;
}

function handleScrollClick(evt) { // or mouseover
    var StepSize;
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var index = target.index;
    StepSize = scrollBars[index].contentStepSize;
    StepSize = (target.className == "moveleft") ? StepSize : -StepSize;
    scrollEngaged = true;
    scrollBy(index, parseInt(StepSize));
    scrollInterval = setInterval("scrollBy(" + index + ", " + parseInt(StepSize) + ")", 20); // oprindelig 100
    evt.cancelBubble = true;
    return false;
}

function scrollBy(index, py) {
    var scroller = scrollBars[index];
    var elem = document.getElementById(scroller.ownerContentID);
    var left = parseInt(DHTMLAPI.getComputedStyle(elem, "left"));
    var scrollWidth = parseInt(scroller.contentScrollWidth);
    var width = scroller.ownerWidth;
    if (scrollEngaged && left + py >= -scrollWidth + width && left + py <= 0) {
        DHTMLAPI.moveBy(elem, py, 0);
        // + style change?
    } else {
        clearInterval(scrollInterval);
        // + style change?
    }
}
