var imageWidth = 600;
var actualImage = 0;
var targetPosition = 0;
var delay = 100;
var timerID;
var autoScrollDelay = 10000;
var autoTimerID;
var isAutoScroll = 1;

function tbhScroller() {
    clearTimeout(timerID);
    var item = document.getElementById('tbhScrollArea');
    var pos = Number(String(item.style.left).replace(/px/i,""));
    pos = -pos;
    if (pos == targetPosition) {
        return;
    }
    var moveTo;
    var dx = Math.round(Math.pow(Math.abs(targetPosition - pos), 1.2)/10);
    if (dx < 1) {
        dx = 1;
    }
    if (pos < targetPosition) {
        moveTo = pos + dx;
    }
    if (pos > targetPosition) {
        moveTo = pos - dx;
    }
    moveTo = -moveTo;
    item.style.left = String(moveTo) + "px";
    timerID = setTimeout("tbhScroller()", delay);
}

function tbhAutoScroller() {
    if (isAutoScroll == 0) {
        return;
    }
    var nextImage = actualImage + 1;
    if (nextImage >= numImages) {
        nextImage = 0;
    }
    tbhScrollTo(nextImage);
    tbhResetAutoScroll();
}

function tbhEnableAutoScroll() {
    isAutoScroll = 1;
    tbhResetAutoScroll();
}

function tbhDisableAutoScroll() {
    isAutoScroll = 0;
    tbhResetAutoScroll();
}

function tbhScrollTo(toImage) {
    if (actualImage == toImage) {
        return;
    }
    targetPosition = toImage * imageWidth;
    actualImage = toImage;
    tbhScrollPaint(-1);
    tbhScroller();
}

function tbhResetAutoScroll() {
    clearTimeout(autoTimerID);
    autoTimerID = setTimeout("tbhAutoScroller()", autoScrollDelay);
}

function tbhScrollHoverIn(image) {
    tbhScrollPaint(image);
}

function tbhScrollPaint(hoverImage) {
    for (i = 0; i < numImages; i++) {
        if (i == hoverImage) {
            document.getElementById("tbh_scroll_" + i + "_link").style.backgroundImage = "url(" + hoverBg + ")";
            document.getElementById("tbh_scroll_" + i + "_link").style.color = "#ffffff";
        } else {
            if (i == actualImage) {
                document.getElementById("tbh_scroll_" + i + "_link").style.backgroundImage = "url(" + activeBg + ")";
                document.getElementById("tbh_scroll_" + i + "_link").style.color = "#ffffff";
            } else {
                document.getElementById("tbh_scroll_" + i + "_link").style.backgroundImage = "url(" + inactiveBg + ")";
                document.getElementById("tbh_scroll_" + i + "_link").style.color = "#000000";
            }
        }
    }
}

function tbhScrollHoverOut() {
    tbhScrollPaint(-1);
}

