<script>
(function(){
	var first = document.getElementById('slide7'),
	body = document.getElementsByTagName('body')[0],
	slides = [], slide, i;

  %SLIDE_DATA%

  for (i=0; i<slides.length; i++) {
    slides[i].step = 0;
    if (!slides[i].hasOwnProperty('steps'))
      slides[i].steps = [];
  }

  function debug_slide() { //<<<
    var node = document.getElementById('debug');
    node.innerHTML = (slide+1)+'/'+slides.length+', '+slides[slide].step+'/'+slides[slide].steps.length;
  }

  //>>>

  function next_slide() { //<<<
    var old_slide_node, new_slide_node, new_slide, old_slide;

    if (slide != null && slide < slides.length-1) {
      old_slide = slides[slide];
      old_slide_node = document.getElementById(old_slide.id);
      old_slide_node.style.display = 'none';
      if (old_slide.hide != null) {
        old_slide.hide(old_slide_node);
      }
    }
    if (slide == null || slide < slides.length-1) {
      slide = slide == null ? 0 : slide+1;
      new_slide = slides[slide];
      new_slide_node = document.getElementById(new_slide.id);
      if (new_slide.show != null) {
        new_slide.show(new_slide_node);
      }
      new_slide_node.style.display = 'block';
      debug_slide();
    }
  }
  //>>>
  function prev_slide() { //<<<
    var old_slide_node, new_slide_node, new_slide, old_slide;

    if (slide != null && slide > 0) {
      old_slide = slides[slide];
      old_slide_node = document.getElementById(old_slide.id);
      old_slide_node.style.display = 'none';
      if (old_slide.hide != null) {
        old_slide.hide(old_slide_node);
      }
    }
    if (slide == null || slide > 0) {
      slide = slide == null ? 0 : slide-1;
      new_slide = slides[slide];
      new_slide_node = document.getElementById(new_slide.id);
      if (new_slide.show = null) {
        new_slide.show(new_slide_node);
      }
      new_slide_node.style.display = 'block';
      debug_slide();
    }
  }
  //>>>

  function next_step() { //<<<
    var s = slides[slide];

    if (s.step >= s.steps.length) {
      next_slide();
      return;
    }

    s.step++;
    document.getElementById(s.steps[s.step-1]).className = 'step shown';
    debug_slide();
  }

  //>>>
  function prev_step() { //<<<
    var s = slides[slide];

    if (s.step <= 0) {
      prev_slide();
      slides[slide].step = slides[slide].steps.length
      return;
    }

    document.getElementById(s.steps[s.step-1]).className = 'step';
    s.step--;
    debug_slide();
  }

  //>>>

  function home() { //<<<
    var i, j, node;

    while (slide > 0) prev_slide();
    for (i=0; i<slides.length; i++) {
      for (j=0; j<slides[i].steps.length; j++)
        node = document.getElementById(slides[i].steps[j]).className = 'step';
      slides[i].step = 0;
    }
  }

  //>>>
  function end() { //<<<
    var i, j, node;

    while (slide < slides.length-1) next_slide();
    for (i=0; i<slides.length; i++) {
      for (j=0; j<slides[i].steps.length; j++)
        node = document.getElementById(slides[i].steps[j]).className = 'step shown';
      slides[i].step = slides[i].steps.length;
    }
  }

  //>>>

  next_slide();

  body.addEventListener('keydown', function(ev){
    console.log('keydown: ', ev);
    switch (ev.keyCode) {
      case 37: // left
        prev_step();
        break;
      case 39: // right
        next_step();
        break;
      case 33: // page up
        prev_slide();
        break;
      case 34: // page down
        next_slide();
        break;
      case 65: // a
        home();
        ev.preventDefault();
        break;
      case 36: // home
        home();
        ev.preventDefault();
        break;
      case 90: // z
        end();
        ev.preventDefault();
        break;
      case 35: // end
        end();
        ev.preventDefault();
        break;
    }
  });
})();
</script>
