var restarea = 200;
var maxspeed = 25;
var maxwidth = 1000;

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset)
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}

function SetCookie(name, value) {
    document.cookie = name + "=" + escape(value);
}

function DeleteCookie(name) {
    document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}



var iedom = document.all || document.getElementById; 
var scrollspeed = 0, movestate = '', actualwidth = '', cross_scroll, ns_scroll, loadedyes = 0, lefttime, righttime;
var leftArrow, rightArrow;

function ietruebody() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function createArrows() {
    rightArrow = document.createElement("div")
    rightArrow.setAttribute("id", "rightdiv")
    document.body.appendChild(rightArrow)
    rightArrow = document.getElementById("rightdiv")
    rightArrow.style.position = "absolute";
    rightArrow.style.visibility = "hidden";
    rightArrow.innerHTML = "<img src='../images/arrow_right.gif' border='0'  alt='jump right'>";

    leftArrow = document.createElement("div")
    leftArrow.setAttribute("id", "leftdiv")
    document.body.appendChild(leftArrow)
    leftArrow = document.getElementById("leftdiv")
    leftArrow.style.position = "absolute";
    leftArrow.style.visibility = "hidden";
    leftArrow.innerHTML = "<img src='../images/arrow_left.gif' border='0' alt='jump left'>";

    positionArrows();

    leftArrow.onclick = function(e) {
        motionjump(600);
    }

    rightArrow.onclick = function(e) {
        motionjump(-600);
    }
}

function positionArrows() {
    var mainobjoffset = getposOffset(crossmain, "left"),
    menuheight = parseInt(crossmain.offsetHeight),
    mainobjoffsetH = getposOffset(crossmain, "top");
    
    leftArrow.style.left = mainobjoffset + "px";
    leftArrow.style.top = mainobjoffsetH + "px";

    rightArrow.style.left = mainobjoffset + menuwidth - 38 + "px";  // rightArrow.offsetWidth + "px";
    rightArrow.style.top = mainobjoffsetH + "px";
}

function showRight(what) {
    rightArrow.style.visibility = what;
}

function showLeft(what) {
    leftArrow.style.visibility = what;
}

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)) {
            cross_scroll.style.left = parseInt(cross_scroll.style.left) - scrollspeed + "px";
            showRight("visible");
            showLeft("visible");
        }
        else
            showRight("hidden");
    }
    lefttime = setTimeout("moveleft()", 10);
}

function moveright() {
    if (loadedyes) {
        movestate = "right";
        if (iedom && parseInt(cross_scroll.style.left) < 0) {
            cross_scroll.style.left = parseInt(cross_scroll.style.left) + scrollspeed + "px";
            showLeft("visible");
            showRight("visible");
        }
        else
            showLeft("hidden");
    }
    righttime = setTimeout("moveright()", 10);
}

function motionjump(iOffset) {
    if (iOffset < 0) {
        if ((parseInt(cross_scroll.style.left) + iOffset) < (menuwidth - actualwidth)) {
            iOffset = (menuwidth - actualwidth) - parseInt(cross_scroll.style.left)
            showRight("hidden");
        }
        showLeft("visible");
    }
    else {
        if (-parseInt(cross_scroll.style.left) < iOffset) {
            iOffset = -parseInt(cross_scroll.style.left);
            showLeft("hidden");
        }
        showRight("visible");
    }
    cross_scroll.style.left = parseInt(cross_scroll.style.left) + iOffset + "px";
    scrollspeed = 0;
}


function motionengine(e) {
    var dsocx = (window.pageXOffset) ? pageXOffset : ietruebody().scrollLeft;
    var dsocy = (window.pageYOffset) ? pageYOffset : ietruebody().scrollTop;

    // y Pos ?
    var curposy = window.event ? event.clientY : e.clientY ? e.clientY : "";
    curposy -= getposOffset(crossmain, "top") - dsocy;

    if (curposy < 50) {
        stopmotion(e);
        return;
    }

    var mainobjoffset = getposOffset(crossmain, "left");
    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) {
        scrollspeed = (curposy - rightbound) / ((menuwidth - restarea) / 2) * maxspeed;
        clearTimeout(righttime);
        if (movestate != "left") moveleft();
    }
    else if (curposy < leftbound) {
        scrollspeed = (leftbound - curposy) / ((menuwidth - restarea) / 2) * maxspeed;
        clearTimeout(lefttime);
        if (movestate != "right") 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 stopmotion(e) {
    if (!window.opera || (window.opera && e.relatedTarget !== null))
        if ((window.event && !crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget != e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))) {
        clearTimeout(lefttime);
        clearTimeout(righttime);
        movestate = "";
    }
}

function initScroll() {
    if (iedom) {
        crossmain = $get("motioncontainer");
        cross_scroll = $get("motiongallery");
        actualwidth = $get("trueContainer").offsetWidth;

        if (typeof crossmain.style.maxWidth !== 'undefined')
            crossmain.style.maxWidth = maxwidth + 'px';
        menuwidth = crossmain.offsetWidth;

        crossmain.onmousemove = function(e) {
            motionengine(e);
        }

        crossmain.onmouseout = function(e) {
            stopmotion(e);
            //showLeft("hidden");
            //showRight("hidden");
        }

    }
    loadedyes = 1
    createArrows();
    showRight(menuwidth < actualwidth ? "visible" : "hidden");

    if (document.body.filters)
        onresize()
}
window.onload = initScroll;

onresize = function() {
    if (typeof motioncontainer !== 'undefined' && motioncontainer.filters) {
        motioncontainer.style.width = "0";
        motioncontainer.style.width = "";
        motioncontainer.style.width = Math.min(motioncontainer.offsetWidth, maxwidth) + 'px';
    }
    menuwidth = crossmain.offsetWidth;
    positionArrows();
}


function saveposition() {
    var scope = $get("scope");
    SetCookie(scope.value.replace(".", "-"), cross_scroll.style.left);
}


