// Author: C.T. Mousset, Mousset Consultancy BV
// This script expects:
// an image called "placeholder"
// a paragraph called "description" which should not be empty
// a toggle button: "toggle-slideshow"

// keep photo and description together
function SlideListElement(loc, desc)
{
  this.loc = loc;
  this.desc = desc;
}

// initialisatie functie voor de SlideShow class
// VARS:
// slideList: instantie van de image-lijst
// speed: tijd tussen de fotos in ms
// name: naam van deze instantie
function SlideShow(slideList, speed, name)
{
    this.slideList = slideList;
    this.speed = speed;
    this.name = name;
    this.current = 0;
    this.timer = 0;
    this.running = false;
}
// zet de class functies
SlideShow.prototype.play = SlideShow_play;
SlideShow.prototype.start_stop = SlideShow_start_stop;
SlideShow.prototype.toggleStartStopButtons = SlideShow_toggle;

// de play functie van de SlideShow class
function SlideShow_play()
{
  with(this)
  {
    if(current==slideList.length)
    {
      current=0;
    }
    showPic(slideList[current].loc, slideList[current].desc);
    clearTimeout(timer);
    timer = setTimeout(name+'.play()', speed);
    current++;
    running = true;
  }
} 

// de start en stop functie van de SlideShow class
function SlideShow_start_stop()
{
   with(this)
   {
      if (running == true) {
         running = false;
         clearTimeout(timer);
      }
      else {
        play();
      }
      toggleStartStopButtons();
   }
}

// Given an anchor called toggle-slideshow:
// change its text
function SlideShow_toggle()
{
  with(this)
  {
    toggle = document.getElementById("toggle-slideshow");
    var toggletext = "Start slideshow";
    if(toggle)
    {
      if(running)
      {
        toggletext = "Stop slideshow";
      }
      toggle.replaceChild(document.createTextNode(toggletext), toggle.childNodes[0]);
    }
  }
}

// Show a picture at placeholders location with a description
function showAnchorPic (anAnchor) {
  return showPic(anAnchor.href,anAnchor.title);
}

// Show a picture at placeholders location with a description
function showPic (aLocation,aTitle) {
  if (document.getElementById) {
    document.getElementById('placeholder') .src = aLocation;
    descNode = document.getElementById('desc');
    if(descNode){
      if (aTitle) {
        descNode.childNodes[0].nodeValue = aTitle;
      } else {
        // maintain the non breaking space...
        descNode.replaceChild(document.createTextNode('\u00a0'), descNode.childNodes[0]);
      }
    }
    return false;
  } else {
    return true;
  }
}