/* Smooth scrolling */

//activeScrolling contains the window.setInterval value
var activeScrolling;

//currentScrollTime is the time which passed since the scrolling started
var currentScrollTime;

function menBtn(val){
  //val contains the id of the target div

  //find the top menu links and set class="active" to the correct link
  var allLinks = document.getElementsByTagName('a');
  for (var i=0;i<allLinks.length;i++) {
    //the top menu links end with '#sX', X a number
    var str = allLinks[i].href.substr(allLinks[i].href.length-3,3);
    if (str.substr(0,2) == '#s') {
      //a top menu link was found, check if it is the one that was clicked
      if (str == ('#'+val)) {
        allLinks[i].className='active';
      } else {
        allLinks[i].className='';
      }
    }
  }

  //Get the target's x-position
  var tarPosX = document.getElementById(val).offsetLeft;

  //Get the current x-position
  var curPosX;
  if (document.documentElement && document.documentElement.scrollLeft){
    curPosX = document.documentElement.scrollLeft;
  } else if (document.body && document.body.scrollLeft){
    curPosX = document.body.scrollLeft;
  } else if (typeof(window.pageXOffset) == 'number'){
    curPosX = window.pageXOffset;
  } else {
    curPosX = 0;
  }
  
  //Get the amount of pixels to scroll
  var amount = tarPosX - curPosX;
  if (amount == 0) return false;
  
  //Get the current y-position
  var curPosY;
  if (document.documentElement && document.documentElement.scrollTop){
    curPosY = document.documentElement.scrollTop;
  } else if (document.body && document.body.scrollTop){
    curPosY = document.body.scrollTop;
  } else if (typeof(window.pageYOffset) == 'number'){
    curPosY = window.pageYOffset;
  } else {
    curPosY = 0;
  }
  
  //Reset the timer
  currentScrollTime = 0;
  window.clearInterval(activeScrolling);
  
  //Write the command which will be used in the timer
  //var cmd = "smoothScrollX("+curPosX+", "+curPosY+", "+amount+", 2000, 10)";
  var cmd = "smoothScrollX("+curPosX+", "+curPosY+", "+amount+", 1000, 20)";
  //Start the timer
  //activeScrolling = window.setInterval(cmd, 10);
  activeScrolling = window.setInterval(cmd, 20);
  //Return false, so the browser won't use the standard "jump to anchor" function
  return false;
}


function smoothScrollX(startPosX, startPosY, amount, scrollTime, interval){
  //Note: to make the scrolling smoother, a polynom is used
  
  //Increase the timer
  currentScrollTime += interval;
  
  //If scroll time is over, deactivate the timer and scroll directly to the target
  if (currentScrollTime >= scrollTime) {
    window.scrollTo(startPosX + amount, startPosY);
    window.clearInterval(activeScrolling);
  } else {
    //Get the time passed in per cent (so t goes linear from 0 to 1)
    var t = currentScrollTime/scrollTime;
    
    //Get the x-position to scroll at
    var scrollTar = startPosX + amount * (-0.25 * Math.pow((2*t-1), 3) + 1.5*t - 0.25);
    
    //Scroll to the target
    window.scrollTo(scrollTar, startPosY);
  }
}


