/********************************************
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 scrollFlashObj = {
  scrollingLayerName:'',
  scrolling1pos:'',
  scrolling2pos:'',
  scrolling3pos:'',
  scrollingSide:'',
  scrollingClose:'',
  
  moveScrollingButtons: function(){
    var maxWidth = document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth;
    var scrollHeight = window.dialogHeight || document.body.scrollHeight || document.documentElement.scrollHeight;
    var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
        
    if (scrollFlashObj.scrolling3pos){
      var maxbottom = scrollHeight - scrollFlashObj.scrolling3pos;
    }else{
      var maxbottom = scrollHeight;
    }
    
    document.getElementById(scrollFlashObj.scrollingLayerName).style.left = (maxWidth/2)-165;
    
    if(scrollFlashObj.scrollingSide != 1){
      if (top >= scrollFlashObj.scrolling1pos && top < maxbottom){
        document.getElementById(scrollFlashObj.scrollingLayerName).style.top = top - scrollFlashObj.scrolling2pos;
      }
    }else{
      if(top < maxbottom){
        document.getElementById(scrollFlashObj.scrollingLayerName).style.top = scrollFlashObj.scrolling2pos + top;
      }
    }
  },
  
  moveButtons: function(){
    var maxWidth = document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth;
    var maxHeight = document.body.clientHeight || document.documentElement.clientHeight || window.innerHeight;
    var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    
    document.getElementById(scrollFlashObj.scrollingLayerName).style.left = (maxWidth/2)-165;
      
    if(scrollFlashObj.scrollingClose != 1){
      document.getElementById(scrollFlashObj.scrollingLayerName).style.top = maxHeight+top-330;
    }else{
      document.getElementById(scrollFlashObj.scrollingLayerName).style.top = maxHeight+top-38;
    }
  },
  
  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(scrollFlashObj.scrollingLayerName){
      scrollFlashObj.moveScrollingButtons();
    }
    if (scrollFlashObj.scrollingLayerName != ''){
      //Start the scroll and someone scroll the page
      scrollFlashObj.addEventScroll(window, "scroll", scrollFlashObj.moveButtons);
      scrollFlashObj.addEventScroll(window, "resize", scrollFlashObj.moveButtons);
    }
  }
};

function moveUpDownFlash(elem, pos1, pos2, pos3, side){
  scrollFlashObj.scrollingLayerName = elem;
  scrollFlashObj.scrolling1pos = pos1;
  scrollFlashObj.scrolling2pos = pos2;
  scrollFlashObj.scrolling3pos = pos3;
  scrollFlashObj.scrollingSide = side;
}

//enabled the scroll on load of the page
scrollFlashObj.addEventScroll(window, "load", scrollFlashObj.moveUpDown2);
