function calcParallax(tileheight, speedratio, scrollposition) {
  return ((tileheight) - (Math.floor(scrollposition / speedratio) % (tileheight+1)));
}

window.onload = function() {

  window.onscroll = function() {
    var posX = (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : window.pageXOffset;
    var posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
    
    var bg = document.getElementById('bg');
    var bgparallax = calcParallax(53, 8, posY);
    bg.style.backgroundPosition = "50% " + bgparallax + "px"; 

    var fg = document.getElementById('fg');
    var fgparallax = calcParallax(400, 2, posY);
    fg.style.backgroundPosition = "47% " + fgparallax + "px"; 
  }
}

