/*
    scroll.js     30.10.2006  (c) Johannes Frank <jfrank@aejf.de>


    CSS of container and content elements:

        div#scrollcontainer
        {
            position: relative;
            width: 300px;
            height: 200px;
            overflow: hidden;
        }
        
        div#scrollcontent
        {
            position: absolute;
            top: 0px;
            left: 0px;
        }
*/

var scrollingFPS = 25;

var updateX_cb;

function scrollX(
    containerId,    /* ID of container element */
    contentId,      /* ID of content element */
    speed,          /* initial scrolling speed [pixels/second] */
    maxSpeed,       /* maximum scrolling speed [pixels/second] */
    accel,          /* scrolling acceleration [pixels/seconds^2] */
    bounce)         /* revert scrolling direction when end is reached */
{
    var containerObj = document.getElementById(containerId);
    var contentObj = document.getElementById(contentId);

    var nextX, nextSpeed;

    if (containerObj.scrollXTimer)
        window.clearTimeout(containerObj.scrollXTimer);

    if (speed)
    {
        if (!containerObj.currentScrollX)
            containerObj.currentScrollX = 0;

        speed = (speed < 0) ? Math.max(speed, maxSpeed) :
            Math.min(speed, maxSpeed);

        nextSpeed = speed + accel;//FIXME

        nextX = containerObj.currentScrollX - speed / scrollingFPS;

        nextX = (speed < 0) ? Math.min(nextX, 0) :
            Math.max(nextX, containerObj.offsetWidth - contentObj.offsetWidth);

        if ((nextX == containerObj.currentScrollX) && bounce)
        {
            nextSpeed = speed * -1;
            maxSpeed *= -1;
            accel *= -1;
        }

        if ((nextX != containerObj.currentScrollX) || bounce)
        {
            moveToX(containerId, contentId, nextX);
            containerObj.scrollXTimer = window.setTimeout(
                "scrollX('"+containerId+"','"+contentId+"',"+nextSpeed+","+
                maxSpeed+","+accel+","+bounce+")", 1000 / scrollingFPS);
        }
    }
}

function moveToX(containerId, contentId, x)
{
    var containerObj = document.getElementById(containerId);
    var contentObj = document.getElementById(contentId);

    containerObj.currentScrollX = x;
    contentObj.style.left = Math.round(containerObj.currentScrollX) + "px";

    if (updateX_cb)
        updateX_cb(containerId, contentId, x);
}

function scrollY(
    containerId,    /* ID of container element */
    contentId,      /* ID of content element */
    speed,          /* initial scrolling speed [pixels/second] */
    maxSpeed,       /* maximum scrolling speed [pixels/second] */
    accel,          /* scrolling acceleration [pixels/seconds^2] */
    bounce)         /* revert scrolling direction when end is reached */
{
    var containerObj = document.getElementById(containerId);
    var contentObj = document.getElementById(contentId);

    var nextY, nextSpeed;

    if (containerObj.scrollYTimer)
        window.clearTimeout(containerObj.scrollYTimer);

    if (speed)
    {
        if (!containerObj.currentScrollY)
            containerObj.currentScrollY = 0;

        speed = (speed < 0) ? Math.max(speed, maxSpeed) :
            Math.min(speed, maxSpeed);

        nextSpeed = speed + accel;//FIXME

        nextY = containerObj.currentScrollY - speed / scrollingFPS;

        nextY = (speed < 0) ? Math.min(nextY, 0) :
            Math.max(nextY, containerObj.offsetHeight - contentObj.offsetHeight);

        if ((nextY == containerObj.currentScrollY) && bounce)
        {
            nextSpeed = speed * -1;
            maxSpeed *= -1;
            accel *= -1;
        }

        if ((nextY != containerObj.currentScrollY) || bounce)
        {
            moveToY(containerId, contentId, nextY);
            containerObj.scrollYTimer = window.setTimeout(
                "scrollY('"+containerId+"','"+contentId+"',"+nextSpeed+","+
                maxSpeed+","+accel+","+bounce+")", 1000 / scrollingFPS);
        }
    }
}

function moveToY(containerId, contentId, y)
{
    var containerObj = document.getElementById(containerId);
    var contentObj = document.getElementById(contentId);

    containerObj.currentScrollY = y;
    contentObj.style.top = Math.round(containerObj.currentScrollY) + "px";
}
