// Functions for a simple image slide show

function SlideShow(interval, place) {
  this.interval = interval;
  this.place = place;
  this.imageNum = 0;
  this.timerID = 0;
  this.random_display = 0;
  this.imageArray = new Array();
}

function addImage(slideShow, location, caption) {
  slideShow.imageArray[slideShow.imageNum++] =
    new imageItem(location, caption);
}

function imageItem(image_location, caption) {
  this.image_item = new Image();
  this.image_item.src = image_location;
  this.image_item.caption = caption;
}

function get_ImageItemLocation(imageObj) {
  return(imageObj.image_item.src)
}

function get_ImageItemCaption(imageObj) {
  return(imageObj.image_item.caption)
}

function randNum(x, y) {
  var range = y - x + 1;
  return Math.floor(Math.random() * range) + x;
}

function getNextImageNumber(slideShow) {
  if (slideShow.random_display) {
    slideShow.imageNum = randNum(0, slideShow.imageArray.length-1);
  } else {
    slideShow.imageNum = (slideShow.imageNum+1) % slideShow.imageArray.length;
  }
  return slideShow.imageNum;
}

function getPrevImageNumber(slideShow) {
  slideShow.imageNum = slideShow.imageNum-1;
  if ( slideShow.imageNum < 0 ) {
    slideShow.imageNum = slideShow.imageArray.length - 1;
  }
  return slideShow.imageNum;
}

function setImage(slideShow) {
  num = slideShow.imageNum;
  var new_image = get_ImageItemLocation(slideShow.imageArray[num]);
  var caption = get_ImageItemCaption(slideShow.imageArray[num]);
  document[slideShow.place].src = new_image;
  document.getElementById("caption").innerHTML = caption;
}

function prevImage( slideShow ) {
  getPrevImageNumber( slideShow );
  setImage( slideShow );
}

function nextImage( slideShow ) {
  getNextImageNumber( slideShow );
  setImage( slideShow );
}

var sShow;
function switchImage() {
  slideShow = sShow;
  nextImage( slideShow );
  slideShow.timerID = setTimeout("switchImage()", slideShow.interval);
}

function startPlaying( slideShow ) {
  sShow = slideShow;
  if ( slideShow.timerID == 0 ) {
    slideShow.timerID = setTimeout("switchImage()", slideShow.interval);
  }
}

function pause( slideShow ) {
  if ( slideShow.timerID != 0 ) {
    clearTimeout(slideShow.timerID);
    slideShow.timerID = 0;
  }
}

function changeInterval( slideShow, interval ) {
  slideShow.interval = interval;
}
