﻿// Verwendung in Main.aspx
var doScroll = false;
var scrollPos = 0;
var acceleration = 1;
var curSpeed = 0;
var targetSpeed = 0;
var rightMostPos = -1;
var isScrolling = false;
var enterScrollField = '0';

var debug = '';

function Scroll(dir, speed, btnId) {
    targetSpeed = speed;
    enterScrollField = btnId;
    if(!isScrolling) {
        doScroll = true;
        isScrolling = true;
        if(dir == 'Left') {
            forceCancelRightScroll = true;
            window.setTimeout('scrollLeftLoop()',0);
        }
        if(dir == 'Right') {
            forceCancelLeftScroll = true;
            window.setTimeout('scrollRightLoop()',0);
        }
    }
}

function StopScroll(btnId) {
    window.setTimeout('TOStopScroll(\'' + btnId + '\')',2);
}
function TOStopScroll(btnId) {
    if(enterScrollField == btnId || enterScrollField == '0')
        doScroll = false;
    else
        enterScrollField = '0';
}

function getCurScrollPos() {
    scrollPos = frames['booksBrowser'].window.pageXOffset;
    if(scrollPos == null)
        scrollPos = frames['booksBrowser'].window.document.body.scrollLeft;
    return scrollPos;
}

function setRightMostPos(val) {
    rightMostPos = val;
}

function ScrollBtnVis() {
    if(getCurScrollPos() < rightMostPos) {
        document.getElementById('btnScrollRight').style.visibility = 'visible';
        document.getElementById('divMaxBooksText').style.display = 'none';
    }
    else {
        document.getElementById('btnScrollRight').style.visibility = 'hidden';
        document.getElementById('divMaxBooksText').style.display = 'none';
    }
    if(getCurScrollPos() > 0) {
        document.getElementById('btnScrollLeft').style.visibility = 'visible';
        document.getElementById('divMaxBooksText').style.display = 'none';
    }
    else {
        document.getElementById('btnScrollLeft').style.visibility = 'hidden';
        var href = frames['booksBrowser'].window.document.location.href;
        if(href.indexOf('FilterName') == -1 && href.indexOf('SearchModule') == -1)
            document.getElementById('divMaxBooksText').style.display = 'inline';
    }
}

function accelerate() {
    if(curSpeed + acceleration <= targetSpeed) {
        curSpeed += acceleration;
    }
    else if(curSpeed > targetSpeed) {
        if(curSpeed - acceleration >= targetSpeed) {
            curSpeed = curSpeed - acceleration;
        }
        else {
            curSpeed = targetSpeed;
        }
    }
}

function scrollLeftLoop() {
    accelerate();
    frames['booksBrowser'].window.scrollBy(Math.round(curSpeed * (-1)),0);
    ScrollBtnVis();
    if(doScroll || (!doScroll && ((getCurScrollPos() % frames['booksBrowser'].BuchBreite) - Math.round(curSpeed) >= 0)))
        window.setTimeout('scrollLeftLoop()',50);
    else {
        isScrolling = false;
        frames['booksBrowser'].window.scrollBy((getCurScrollPos() % frames['booksBrowser'].BuchBreite) * (-1),0);
        curSpeed = 0;
    }
}

function scrollRightLoop() {
    accelerate();
    frames['booksBrowser'].window.scrollBy(Math.round(curSpeed),0);
    ScrollBtnVis();
    if(doScroll || (!doScroll && ((getCurScrollPos() % frames['booksBrowser'].BuchBreite) - Math.round(curSpeed) >= 0)))
        window.setTimeout('scrollRightLoop()',50);
    else {
        isScrolling = false;
        frames['booksBrowser'].window.scrollBy((getCurScrollPos() % frames['booksBrowser'].BuchBreite) * (-1),0);
        curSpeed = 0;
    }
}

