// endless_page.js
var currentPage = 2;

function checkScroll() {
  if (nearBottomOfPage()) {
    currentPage++;
    new Ajax.Request('/posts.js', {method: 'get', parameters: {page: currentPage}});
  } else {
    setTimeout("checkScroll()", 250);
  }
}

function nearBottomOfPage() {
  return scrollDistanceFromBottom() < 150;
}

function scrollDistanceFromBottom(argument) {
  return pageHeight() - (window.pageYOffset + self.innerHeight);
}

function pageHeight() {
  return Math.max(document.body.scrollHeight, document.body.offsetHeight);
}

document.observe('dom:loaded', checkScroll);


var currentScriptID = 1;

function addScriptTag( src ) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.id = currentScriptID;
  script.type = 'text/javascript';
  script.src = src;
  head.appendChild(script);
  currentScriptID++;
}

