var restarea=200;
var maxspeed=30;
var maxwidth=800;
var startpos=0;
// Anzahl Pixel die pro Klick gescrollt werden soll.
var scrollperklick=755;

var iedom=document.all||document.getElementById,
    scrollspeed=0, movestate='', actualwidth='', cross_scroll, ns_scroll, statusdiv,
    loadedyes=0, lefttime, righttime;
var pixelToGo, leftArrow, rightArrow;

function ietruebody()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function showarrows()
{
    if(parseInt(cross_scroll.style.left)<0)
    {
       leftArrow.style.visibility="visible";
    }
    else
    {
       leftArrow.style.visibility="hidden";
    }

    if(parseInt(cross_scroll.style.left)>(menuwidth-actualwidth))
    {
       rightArrow.style.visibility="visible";
    }
    else
    {
       rightArrow.style.visibility="hidden";
    }
}

function hidearrows()
{
    leftArrow.style.visibility="hidden";
    rightArrow.style.visibility="hidden";
}

function getposOffset(what, offsettype)
{
    var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
    var parentEl=what.offsetParent;

    while (parentEl!=null)
    {
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }

    return totaloffset;
}


function moveleft()
{
    if (loadedyes)
    {
        movestate="left";
    
        if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth) && pixelToGo>0)
        {
            if(scrollspeed > pixelToGo) scrollspeed = pixelToGo;
            cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px";
            pixelToGo -= scrollspeed;
            lefttime=setTimeout("moveleft()",10);
        }
        else
        {
            showarrows();
            clearTimeout(lefttime);
            clearTimeout(righttime);
            movestate="";
        }
    }
    // lefttime=setTimeout("moveleft()",10);
}

function moveright()
{
    if (loadedyes)
    {
        movestate="right";

        if (iedom&&parseInt(cross_scroll.style.left)<0 && pixelToGo>0)
        {
            if(scrollspeed > pixelToGo) scrollspeed = pixelToGo;
            cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
            pixelToGo -= scrollspeed;
            righttime=setTimeout("moveright()",10);
        }
        else
        {
            showarrows();
            clearTimeout(lefttime);
            clearTimeout(righttime);
            movestate="";

        }
    }
    // righttime=setTimeout("moveright()",10);
}

function motionengine(e)
{
    var mainobjoffset=getposOffset(crossmain, "left"),
    dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
    dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
    curposy=window.event? event.clientX : e.clientX? e.clientX: "";
    curposy-=mainobjoffset-dsocx;

    var leftbound=(menuwidth-restarea)/2;
    var rightbound=(menuwidth+restarea)/2;
    
    if (curposy>rightbound && parseInt(cross_scroll.style.left)>(menuwidth-actualwidth))
    {
        //hidearrows();
        var thismovestate="left"
        //scrollspeed=Math.ceil((curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed);
        scrollspeed=20;
        clearTimeout(righttime);
        pixelToGo=scrollperklick;
        if (movestate!=thismovestate) moveleft();
    }
    else if (curposy<leftbound && parseInt(cross_scroll.style.left)<0)
    {
        //hidearrows();
        var thismovestate="right"
        //scrollspeed=Math.ceil((leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed);
        scrollspeed=20;
        clearTimeout(lefttime);
        pixelToGo=scrollperklick;
        if (movestate!=thismovestate) moveright();
    }
    else
    {
        scrollspeed=0;
    }
}

function contains_ns6(a, b)
{
    if (b!==null)
    while (b.parentNode)
    {
        if ((b = b.parentNode) == a)
        {
            return true;
        }
    }

    return false;
}

function fillup()
{
    if (iedom)
    {
        crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer;

        if(typeof crossmain.style.maxWidth!=='undefined')
        {
            crossmain.style.maxWidth=maxwidth+'px';
        }

        menuwidth=crossmain.offsetWidth;
        cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery;
        actualwidth=document.getElementById? document.getElementById("trueContainer").offsetWidth : document.all['trueContainer'].offsetWidth;

        leftArrow = document.getElementById? document.getElementById("left_arrow") : document.all.left_arrow;
        rightArrow = document.getElementById? document.getElementById("right_arrow") : document.all.right_arrow;
        //showarrows();

        if (startpos)
        {
            cross_scroll.style.left=(menuwidth-actualwidth)/startpos+'px';
        }

        crossmain.onclick=function(e)
        {
            if(!movestate) motionengine(e);
        }

        crossmain.onmouseover=function(e) {
          showarrows();
        }

        crossmain.onmouseout=function(e) {
          hidearrows();
        }

    }

    loadedyes=1;

    /*
    if (document.body.filters)
    {
        onresize();
    }
    */
}

/*
window.onload=fillup;

onresize=function()
{
    menuwidth=crossmain.offsetWidth;
    cross_scroll.style.left=startpos? (menuwidth-actualwidth)/startpos+'px' : 0;
    hidearrows();
    showarrows();
}
*/