//--------------------------------functions to manage scrolling menu -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
window.onresize = resizeEvent;
window.onscroll = scrollEvent;
window.onload = loadEvent;

var _scrollTop=0;
var mTop=90; // default top position of floating menu
var theMenu;
var theLeftArea;
var repoTimerID=0;

function resizeEvent() { 
  // fix top floater
  document.getElementById("topfloater").style.width=document.getElementById("mainpage").offsetWidth;
  _scrollTop = document.body.scrollTop;
  repositionMenu();
  if (window.onResize)
  { onResize(); } // found on main page
}
function scrollEvent() { 
  _scrollTop = document.body.scrollTop;
  repositionMenu();
  if (window.onScroll)
  { onScroll(); } // found on main page

}
function loadEvent() {
  document.getElementById("topfloater").style.width=document.getElementById("mainpage").offsetWidth;
  theMenu=document.getElementById("menu");
  theLeftArea=document.getElementById("border_left");
  if (window.onLoad)
  { onLoad(); } // found on main page
}

function repositionMenu() {
  if (repoTimerID!=0) { clearTimeout(repoTimerID); repoTimerID = 0; }
  repoTimerID=setTimeout("repoTimer()",100);
}

function repoTimer() {
  clearTimeout(repoTimerID);
  var g = theLeftArea.offsetHeight;
  var m = parseInt(theMenu.style.top);
  var a = (_scrollTop>mTop) ? _scrollTop : mTop;
  var dist;
  // now let's see if that is out of bounds
  if ( a + theMenu.offsetHeight > g )
  { a = g - theMenu.offsetHeight; }
  if (m != a)
  { dist = Math.abs(a-m);
    if (dist > 1000)
    { if (m < a) m+=1000; else m-=1000; }
    else
    { if (dist > 30)
      { if (m < a) m+=(dist/15); else m-=(dist/15); }
      else
      { if (m < a) m++; else m--; }
    }
    theMenu.style.top = m + "px";
    repoTimerID=setTimeout("repoTimer()",10);
  }
}
