/* 
 * See http://mozpoint.mozdev.org
 *
 * slides.js -- a collection of scripts for use with mozpoint. 
 * - Gagan Saksena 07/08/01
 *
 * Added rginda's hack for onkeyup events to enable keyboard 
 * navigation.  - 02/23/02 
 *
 * Added blank page for 'b' key. Like powerpoint. 03/08/02
 *
 * Added IE support as well. 03/11/02
 *
 */
// --------------------------------------------------

var keyTimeOut = 500;
var keyStack = new Array();
var ie = document.all ? true: false;

// quick function for creating colors.
function rgbc(a, b, c) {
  return 'rgb(' + a + ',' + b + ',' + c + ')';
}
// fadecolor
function fadecolor(rgbfrom, rgbto, step)
{
  var rgbformat = /rgb\((\d+),(\d+),(\d+)\)/g
  rgbformat.exec(rgbfrom);
  return rgbc(RegExp.$1, RegExp.$2, RegExp.$3 + step)
}

function currentFile() {
  k = String(document.location).split('/');
  k = k[k.length-1];
  
  if (k == '') {
    k = 'index.html';
  }
  return k;
}

function currentFileIndex() {
  f = currentFile();
  for (i=0; i<toc.length; ++i) {
    if (toc[i] == f)
      return i;
  }
  return -1;
}

function loadslide(c) {
  if (c>=0 && c <toc.length) {
    document.location = toc[c];
  }
}

function loadSlideFromKeys() {
  if (keyStack.length == 0) return;
  c = 0;
  i = 0;
  do {
    d = keyStack.pop();
    c += Math.pow(10, i++)*d;
  } while (keyStack.length);
  loadslide(parseInt(c));
}

function fireload(count) {
  if (count != keyStack.length) return;
  loadSlideFromKeys();
}

function loadotherstuff() {
  if (ie) {
    document.write('<DIV ID=other></DIV>');
    return;
  }

  var other = document.createElement("div");
  other.setAttribute("id", "other");

  document.firstChild.appendChild(other);
}

// toggle_blank switches the black/white blank div to front or back
function toggle_blank(black) {
  var other = ie ? document.all["other"] : document.getElementById("other");
  var color = black ? "black" : "white";
  other.style.backgroundColor = (other.style.backgroundColor == color) ? "transparent" : color;
}

// Capture all key events-- thx for this hack to rginda. I extended it for the
// keyboard binding of PP
// TODo punch in keys numbers for slides nums
function keyEvents(e) {
  switch (e.which) {
    case 48: // 0
    case 49: // 1
    case 50: // 2
    case 51: // 3
    case 52: // 4
    case 53: // 5
    case 54: // 6
    case 55: // 7
    case 56: // 8
    case 57: // 9
      setTimeout(fireload, keyTimeOut, keyStack.push(e.which-48)); 
      break;
    case 66: // b
    case 87: // w
			toggle_blank(e.which == 66);
      break;
    case 32: // spacebar
    case 39: // rightkey
    case 40: // downkey
    case 78: // n
      loadslide(currentFileIndex()+1);
      break;
    case 37: // leftkey
    case 38: // upkey
    case 80: // p
      loadslide(currentFileIndex()-1);
      break;
    case 82: // r
    case 84: // t
      // loadslide(0);
      break;
    default: 
      break;
  }
  ie ? e.returnValue = false : e.preventDefault();
}

// Yeah we're called mozpoint but what the heck-- we'll be nice.
function keyEventsIE() {
  e = event;
  e.which = e.keyCode;
  keyEvents(e);
}

function init_all() {
  // initialize all the stuff... 
  document.onkeyup = ie ? keyEventsIE : keyEvents;
}


