var g_direction = 0;
var g_scrollInterval = 0;

function isScrollable(t)
{
    var ob = document.getElementById("scrollarea");
    var parent = ob.parentNode;
    return ! (t > 0 || t < -((ob.clientHeight + 100) - parent.clientHeight));
}

function doScroll(delta)
{
    if (!document.getElementById) return false;
    var ob_style = document.getElementById("scrollarea").style;
    var t = parseInt(ob_style.marginTop);

    if (isNaN(t))
    {
        t = -0;
    }

    t += delta;
    if (!isScrollable(t))
    {
        return false;
    }
    ob_style.marginTop = t + "px";
    return true;
}

function scrollInterval()
{
    if (!doScroll(g_direction))
    {
        stopScroll();
        return;
    }
}

function startScroll(dir)
{
    g_direction = dir * 5;
    if (g_scrollInterval == 0)
    {
        g_scrollInterval = setInterval("scrollInterval()", 50);
    }
}

function stopScroll()
{
    clearInterval(g_scrollInterval);
    g_direction = 0;
    g_scrollInterval = 0;
}

function handleKey(e)
{
    var key = (e) ? e.which : window.event.keyCode;
    if (key == 40)
    {
        startScroll(-1);
    }
    else if (key == 38)
    {
        startScroll(1);
    }
    return true;
}

function disableJS()
{
    if (!document.getElementById) return;

    var ob = document.getElementById("scrollarea");
    var obContainer = document.getElementById("scrollareaContainer");

    ob.style.marginTop = "0px";
    obContainer.style.overflow = "auto";
    obContainer.style.height = null;
}

function handleMouseWheel(delta)
{
    doScroll(delta * 20);
}

function wheel(event){
    var delta = 0;
    if (!event)
    {
        // Internet Exploder
        event = window.event;
    }

    if (event.wheelDelta)
    {
        // IE and Opera
        delta = event.wheelDelta / 120;

        // In Opera 9, delta differs in sign as compared to IE.
        if (window.opera)
        {
            delta = -delta;
        }
    }
    else if (event.detail)
    {
        // In Mozilla, sign of delta is different to IE.
        delta = -event.detail / 3;
    }

    if (delta)
    {
        handleMouseWheel(delta);
    }

    // Prevent default actions
    if (event.preventDefault)
    {
        event.preventDefault();
    }
    event.returnValue = false;
}

function initScrolling()
{
    // If there isn't enough content to scroll, hide the buttons
    if (!isScrollable(-0))
    {
        var ob = document.getElementById("articlesButtonsContainer");
        ob.style.display = "none";
        return;
    }

    document.onkeydown = handleKey;
    document.onkeyup = stopScroll;
    var sa = document.getElementById("scrollarea");

    if (1)
    {
        if (window.addEventListener)
        {
            // Mozilla
            window.addEventListener('DOMMouseScroll', wheel, false);
        }
        // IE and Opera
        window.onmousewheel = document.onmousewheel = wheel;
    }
    else
    {
        if (sa.addEventListener)
        {
            // Mozilla
            sa.addEventListener('DOMMouseScroll', wheel, false);
        }
        // IE and Opera
        sa.onmousewheel = wheel;
    }
}

