/********************************************
Optimize javascript for scrolling buttons
Optimize by FPB and PR
scrollingLayerName = the name of the element
scrolling1pos      = position where it start to scroll
scrolling2pos      = position of the element in the window while scrolling
scrolling3pos      = (optionnal) final position of the scroll if there's a too big footer
********************************************/
var scrollObj = {
  scrollingLayerName:'',
  scrolling1pos:'',
  scrolling2pos:'',
  scrolling3pos:'',
  
  moveScrollingButtons: function(){    
    var scrollHeight = window.dialogHeight || document.body.scrollHeight || document.documentElement.scrollHeight;
    var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    
    if (scrollObj.scrolling3pos){
      var maxbottom = scrollHeight - scrollObj.scrolling3pos;
    }else{
      var maxbottom = scrollHeight;
    }
    
    if (top >= scrollObj.scrolling1pos && top < maxbottom){
      document.getElementById(scrollObj.scrollingLayerName).style.top = top - scrollObj.scrolling2pos;
    }
  },
  
  addEventScroll: function(elem, evtType, func){
    if(elem.addEventListener){
      elem.addEventListener(evtType, func, false);
    }else if(elem.attachEvent){
      elem.attachEvent("on" + evtType, func);
    }else{
      elem["on" + evtType] = func;
    }
  },
  
  moveUpDown2: function(){
    if(scrollObj.scrollingLayerName){
      scrollObj.moveScrollingButtons();
    }
    if (scrollObj.scrollingLayerName != ''){
      //Start the scroll and someone scroll the page
      scrollObj.addEventScroll(window, "scroll", scrollObj.moveScrollingButtons);
    }
  }
};

function moveUpDown(elem, pos1, pos2, pos3){
  scrollObj.scrollingLayerName = elem;
  scrollObj.scrolling1pos = pos1;
  scrollObj.scrolling2pos = pos2;
  scrollObj.scrolling3pos = pos3;
}

//enabled the scroll on load of the page
scrollObj.addEventScroll(window, "load", scrollObj.moveUpDown2);