// relative mouse scroller
// Espen H Hjertø, Kantega AS



jQuery(document).ready(function(){
        

    if ($("#alleYrkerScrollerContainer").height() > ($("#alleYrkerScroller").height() + 20)){
        $("#alleYrkerScrollerContainer").height($("#alleYrkerScroller").height() + 20);
    }
    if ($("#alleYrkerScrollerContainer").height() <= $("#alleYrkerScroller").height()){

        var xOffsetExp = $("#alleYrkerScrollerContainer").offset().left;
        var yOffsetExp = $("#alleYrkerScrollerContainer").offset().top;
        var expWidth = $("#alleYrkerScrollerContainer").width();
        var expHeight = $("#alleYrkerScrollerContainer").height();
        dxdiff = $("#alleYrkerScroller").width() - expWidth;
        dydiff = $("#alleYrkerScroller").height() - expHeight + 20;

        dx = expWidth / dxdiff;
        dy = expHeight / dydiff;
        $("#alleYrkerScrollerContainer").mousemove(function(e){
            xo = e.pageX-xOffsetExp;
            yo = e.pageY-yOffsetExp;
            if (xo < 1) xo =0;
            if (yo < 1) yo = 0;
            if (xo > expWidth) xo = expWidth;
            if (yo > expHeight) yo = expHeight;
            $("#alleYrkerScroller").css("top", 0 - (yo / dy));
        });


        // idle list scroller
        // Espen H Hjertø, Kantega AS

        var ilsScroll = true;
        var ilsTop = 0;
        var ilsDirection = -2;
        var ilsTurnLimit =  ($("#alleYrkerScroller").height() - $("#alleYrkerScrollerContainer").height() + 20 )*-1;
        $("#alleYrkerScroller").css("top", 0);

        $.timer(100, function(timer){
            if (ilsScroll){
                ilsTop += ilsDirection;
                $("#alleYrkerScroller").css("top", ilsTop);
                if ( ilsTop > 0 || ilsTop < ilsTurnLimit ){
                    ilsDirection *= -1;
                }
            }
        });

        $("#alleYrkerScrollerContainer").hover(function(){
            $("#alleYrkerScroller").stop();
            ilsScroll = false;
        }, function(){
            $("#alleYrkerScroller").stop();
            ilsScroll = true;
            ilsTopString = $("#alleYrkerScroller").css("top");
            ilsTopStringx = $("#alleYrkerScroller").css("top").substring(0, ilsTopString.length - 2);
            ilsTop = parseInt(ilsTopStringx);
        });
    }
});