var debug = document.location.toString().indexOf('debug') >
    document.location.toString().indexOf('?');

function addDebug() {
  var info = document.createElement('span');
  info.style.display = 'block';
  info.style.position = 'fixed';
  info.style.top = 0;
  info.style.left = 0;
  info.appendChild(document.createTextNode('Debug mode'));
  document.body.appendChild(info);
}

var selectedEntry = -1;

function highlight(index) {
  if (index < 0) {
    window.scroll(0,0);
    return;
  }
  var entry = getEntries()[index];
  entry.style.backgroundColor = '#eef';
  entry.scrollIntoView();
}

function unhighlight(index) {
  if (index < 0) {
    return;
  }
  var entry = getEntries()[index];
  entry.style.backgroundColor = '#fff';
}

function getEntries() {
  return document.getElementsByClassName('entry');
}

function processKeyPress(e) {
  if (e.charCode == 106 && selectedEntry < getEntries().length - 1) {
    unhighlight(selectedEntry);
    highlight(++selectedEntry);
  } else if (e.charCode == 107 && selectedEntry >= 0) {
    unhighlight(selectedEntry);
    highlight(--selectedEntry);
  }
}

if (debug) {
  window.addEventListener('load', addDebug, false);
}

window.addEventListener('keypress', processKeyPress, false);

