// JavaScript Slide Presentation

// Keypress navigation
function onKeyPress(e)
{
  // IE doesn't see the event argument passed in, so get it this way:
  if (window.event) e = window.event;

  // Mozilla uses charCode for printable characters, keyCode for unprintables:
  if (e.charCode && e.DOM_VK_SPACE && e.charCode == e.DOM_VK_SPACE) {
      nextSlide()
      e.preventDefault();
      return;
  }
//alert("key press, char code " + e.charCode + ", key code " + e.keyCode );
  // Use numeric values rather than DOM_VK_* so that non-mozilla browsers
  // might work.
  switch (e.keyCode) {
    case 32:    // e.DOM_VK_SPACE:
    case 39:    // e.DOM_VK_RIGHT:
      nextSlide()
      e.preventDefault();
      return;
    case 8:     // e.DOM_VK_BACK_SPACE:
    case 37:    // e.DOM_VK_LEFT:
      prevSlide();
      e.preventDefault();
      return;
    case 36:    // e.DOM_VK_HOME:
    case 38:    // e.DOM_VK_UP:
      firstSlide()
      e.preventDefault();
      return;
    case 35:    // e.DOM_VK_END:
      lastSlide()
      e.preventDefault();
      return;
    case 33:    // e.DOM_VK_PAGE_UP:
      tableOfContents();
      e.preventDefault();
      return;
  }
}

// Actually add it.  This actually only has to be done for the first slide.
if (document.addEventListener)		// DOM way
  document.addEventListener("keypress", onKeyPress, false);
else if (document.all)			// IE way
  document.attachEvent("onkeypress", onKeyPress);

// Slide navigation. Maintains a list of slide names.

var slides = new Array ( "bugfixing.html",
                         "purpose.html",
                         "dontpanic.html",
                         "why.html",
                         "firststeps.html",
                         "bugzilla.html",
                         "steps.html",
                         "usefultools.html", "pipelines.html",
                         "recursive-grep.html",
                         "recursive2.html",
                         "buildtools.html",
                         "texteditor.html", "texteditorbells.html",
                         "emacs-match-paren.html",
                         "csteps.html", "csteps2.html",
                         "example.html",
                         "makepatch.html",
                         "applypatch.html",
                         "otherways.html",
                         "summary.html"
                          );

function indexOfPage() {
  var url = document.URL;
  var lastslash = document.URL.lastIndexOf("/");
  var filename = url.substring(lastslash+1, url.length);
//alert(filename);

  // JS 1.6 has Array.indexOf, but that doesn't work in Opera/Konq/etc.
  if (slides.indexOf) return slides.indexOf(filename);
  var i;
  for (i=0; i<slides.length; ++i) {
    if (slides[i] == filename) return i;
  }
  return 0;
}

function nextSlide() {
  var i = indexOfPage();
  if (i >= slides.length - 1) {    // last slide
    //window.alert("That's the last slide");
    return;
  }
  window.location = slides[i+1];
}

function firstSlide() {
  window.location=slides[0];
}

function lastSlide() {
  window.location=slides[slides.length-1];
}

function tableOfContents() {
  // First make a list of all our slides:
  var text = "<h2>Table of Contents</h2>\n<small>\n";
  var i;
  for (i=0; i<slides.length; ++i)
    text += '<a href="' + slides[i] + '">' + slides[i] + '</a><br>\n'
  text += "</small>\n"
  body = document.getElementsByTagName("body")[0];
  body.innerHTML = text;
}

function prevSlide() {
  i = indexOfPage();
  if (i <= 0) {    // last slide
    //window.alert("Already on the first slide");
    return;
  }
  window.location = slides[i-1];
}


