/********************************************
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:'',
  initialposflag:0,
  objPropTop:'',
  objPropHeight:'',
  
  findPosY: function (tempobj){
    var tempcurtop = 0;
    if(tempobj.offsetParent){
      while(1){
        tempcurtop += tempobj.offsetTop;
        if(!tempobj.offsetParent)
          break;
        tempobj = tempobj.offsetParent;
      }
    }else if(tempobj.y){
      tempcurtop += tempobj.y;
    }
    return tempcurtop;
  },
  
  moveScrollingButtons: function(){    
    var scrollHeight = window.dialogHeight || document.body.scrollHeight || document.documentElement.scrollHeight;
    var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    var scrollObjElem = document.getElementById(scrollObj.scrollingLayerName);
    
    if (scrollObj.scrolling3pos){
      if (scrollObj.initialposflag != 1){
        scrollObj.objPropHeight = scrollObjElem.scrollHeight || window.getComputedStyle(scrollObjElem, null).getPropertyValue("height").replace('px','');
        scrollObj.objPropTop = scrollObj.findPosY(scrollObjElem);
        scrollObj.initialposflag = 1;
      }
      var maxbottom = scrollHeight - scrollObj.scrolling3pos - scrollObj.objPropHeight - scrollObj.objPropTop;
    }else{
      var maxbottom = scrollHeight;
    }
    
    if (top >= scrollObj.scrolling1pos && (top-scrollObj.scrolling1pos) < maxbottom){
      scrollObjElem.style.top = top - scrollObj.scrolling2pos + 'px';
    }else if (top < scrollObj.scrolling1pos){
      scrollObjElem.style.top = '0px';
    }else if ((top+scrollObj.scrolling1pos) > maxbottom){
      scrollObjElem.style.top = maxbottom + 'px';
    }
    //document.getElementById('toptest').innerHTML += 'obj top:'+scrollObj.objPropTop+'<br>obj height:'+scrollObj.objPropHeight+'<br>pos 3:'+scrollObj.scrolling3pos+'<br>top:'+top+'<br>maxbottom:'+maxbottom+'<br>scroll height:'+scrollHeight;
  },
  
  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 when 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);
