function photoCache(photo, caption) {
  var image = new Image();

  image.src = photo;
  this.image = image;
  this.caption = caption;
}

function switchPhoto(photoArray, imageName, captionId, i, switchInterval, end) {
  var date = new Date();
  var now = date.getTime();

  document.getElementById(captionId).innerHTML = photoArray[i].caption;
  document.images[imageName].src = photoArray[i].image.src;

  if (end == -1 || end - now > 0) {
    setTimeout(function() {switchPhoto(photoArray, imageName, captionId, (i+1) % photoArray.length, switchInterval, end)}, switchInterval);
  }
}

function startBackgroundSwitch(photoArray, imageName, captionId, startIndex, switchInterval, numCycles) {

  var date = new Date();
  var start = date.getTime();
  var end;
  var i = startIndex;

  if (numCycles == -1) {
    end = -1;
  }
  else {
    end = start + (switchInterval * photoArray.length * numCycles);
  }

  document.images[imageName].src = photoArray[i].image.src;
  document.getElementById(captionId).innerHTML = photoArray[i].caption;
  setTimeout(function() {switchPhoto(photoArray, imageName, captionId, i+1, switchInterval, end)}, switchInterval);
}


