﻿var interval = 3000; // delay between rotating images (in milliseconds)

/* basic object to hold information on each image rotator
*  imgIndex: current position in the array to grab image from
*  imgList: array of images to rotate through
*  urlList: array of URLs to go with each image
*/
function imageObj(){
 this.imgIndex = 1;
 this.imgList = new Array();
 this.urlList = new Array();
}

function getNextImage(controlID, imgObj) {
  var image_index = (imgObj.imgIndex) % imgObj.imgList.length;
  return(imgObj.imgList[image_index]);
}

function getNextURL(controlID, imgObj) {
  var image_index = (imgObj.imgIndex) % imgObj.urlList.length;
  return(imgObj.urlList[image_index]);
}
//main routine.  Grabs the imageObj from the array of global variables,
//grabs the next image and URL, and spits out the new HTML.
function rotateImage(controlID) {

  var imgObj = window['img'+ controlID];
  var new_image = getNextImage(controlID, imgObj);
  var new_URL = getNextURL(controlID, imgObj);
  imgObj.imgIndex += 1;
  var newHtml = '<a href=\"'+new_URL+'\"><img src=\"'+new_image+'\" /></a>';
  document.getElementById(controlID).innerHTML = newHtml;

  var recur_call = "rotateImage('"+controlID+"')";
  setTimeout(recur_call,interval);
}
