﻿var scrolldelay = null;
var timeout = 10;

var moveCount = 0;
var moveFactor = 35.0;

var useMoveFactor = true;
var useDebug = false;
var useFading = true;

var velocityFactor;

var sr = 7;
var sl = -7;

var vis = false;

function xBeginScroll(destination) {

    if (destination && !vis) {
        xSetOpacity(false);
        vis = true;
    }

    if (destination == 'l') {

        if (useMoveFactor) {

            velocityFactor = (++moveCount) / moveFactor;

            if (velocityFactor < 1)
                velocityFactor = 1;

            sr = velocityFactor * 7;
        }

        if (useDebug)
            $(".scroll-right").text(moveCount);

        scrolldelay = setTimeout('xBeginScroll(\"l\")', timeout);

        window.scrollBy(sr, 0);

    }
    else if (destination == 'r') {

        if (useMoveFactor) {

            velocityFactor = (++moveCount) / moveFactor;

            if (velocityFactor < 1)
                velocityFactor = 1;

            sl = velocityFactor * (-7);
        }

        if (useDebug)
            $(".scroll-left").text(moveCount);


        scrolldelay = setTimeout('xBeginScroll(\"r\")', timeout);

        window.scrollBy(sl, 0);

    }
    else {
        xStopScroll();
        xSetOpacity(true);
        vis = false;
    }
};

function xStopScroll() {

    clearTimeout(scrolldelay);

    xSetOpacity(true);

    if (useMoveFactor) {
        sr = sl = 0;
        moveCount = 0;
    }

    if (useDebug) {
        $(".scroll-left").text('');
        $(".scroll-right").text('');
    }
};

function xReset() {

    window.scrollTo(0, 0);

    xSetOpacity(true);

    return false;
};

function xSetOpacity(hide) {

    if (!useFading)
        return;

    if (hide) {
        $(".scroll-left").fadeTo('slow', 0.5, function () {
        });
        $(".scroll-right").fadeTo('slow', 0.5, function () {
        });
    }
    else {
        $(".scroll-left").fadeTo('slow', 1.0, function () {
        });
        $(".scroll-right").fadeTo('slow', 1.0, function () {
        });
    }
};
