 function calculateYPos(item, startPos) {
    var pageHeight = document.body.clientHeight;
    var pixelsScrolled = document.body.scrollTop;

     var posY =  startPos + pixelsScrolled;
     return posY;
 }

function getPos(el) {
    // yay readability
    for (var lx=0, ly=0;
         el != null;
         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return {x: lx,y: ly};
}

function adjustMenu() {
        menu = document.getElementById('floatdiv');
        if (!menu)
            return;
        startAnchor = document.getElementById('startanchor');
        startPosition = getPos(startAnchor);
        startPosition = startPosition.y;
        anchor = document.getElementById('floatanchor');
        topPosition = calculateYPos('anchor', startPosition);
        var pixelsScrolled = document.body.scrollTop;
        rightSide = document.getElementById('rightc');
        maxHeight = rightSide.offsetHeight + 250;
        if (pixelsScrolled<380) {
//          menu.style.position='absolute';
            menu.style.top='0px';
    //      menu.style.top='370px';
        }
        if (pixelsScrolled>380 && (maxHeight < 0 || topPosition<maxHeight)) {
            //alert(menu.style);
//          menu.style.position="absolute";
            newTop = pixelsScrolled-startPosition;
            menu.style.top=(newTop) + 'px';
        }
}
    
document.body.onscroll=function(){adjustMenu()}; 

window.onscroll=function(){adjustMenu()}; 

