var curPage = "fp-all"; //start page

// Scroll the page manually to the position of element "link", passed to us.
function ScrollPages(link) {
  // set curPage
  if (curPage == link) { return; }
  curPage = link;
  
  // locate div to scroll to
  var thepager = document.getElementById('pager');
  var position = findElementPos(document.getElementById(link));

  // locate start page
  offsetPos = findElementPos(document.getElementById('fp-all'));
  position[0] = position[0] - offsetPos[0];

  scroll(thepager, thepager.scrollLeft, position[0]);
}

// animate the scroll
var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scroll(elem, start, end) {
  if (scrollanim.timer != null) {
    clearInterval(scrollanim.timer);
    scrollanim.timer = null;
  }
  scrollanim.time = 0;
  scrollanim.begin = start;
  scrollanim.change = end - start;
  scrollanim.duration = 25;
  scrollanim.element = elem;
  scrollanim.timer = setInterval("animate();", 15);
}

function animate(){
  if (scrollanim.time > scrollanim.duration) {
    clearInterval(scrollanim.timer);
    scrollanim.timer = null;
  }
  else {
    move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
    scrollanim.element.scrollLeft = move;
    scrollanim.time++;
  }
}

// find vertical position of an element. returns array of the coordinates
function findElementPos(elemFind){
  var posX = 0;
  var posY = 0;
  do {
    posX += elemFind.offsetLeft;
    posY += elemFind.offsetTop;
  } while ( elemFind = elemFind.offsetParent )

  return Array(posX, posY);
}

function sineInOut(t, b, c, d){
  return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}