/* JavaScript functions for the F.J. Kerrigan Plumbing Co. web site */

/* The web page that includes this script must do the following:
   1) Make sure the page has <img>s with ids equal to the names
      passed to addSlot() 
   2) Make sure you call addSlot() for each <img> tag, passing
      the <img> tag's id.
   3) Make sure you call addImage() for each image you want to
      rotate through.
   4) Make sure you call rotate() in your <body> tag's onload
      handler.
      
   By design, images will rotate in the order they are specified
   by the calls to addImage().  Each subsequent image will display
   in the next available slot.
*/

var iImage = 0; /* index into imageFiles[] of image to rotate */
var timerID = null;
var iSlot = 0;  /* index of next slot to rotate into */
var isFirstTime = true;
var timeoutMS = 3000; /* time between rotations in milliseconds */
var imageFiles = new Array(0); /* array of image file names/URLs */
var slots = new Array(0); /* array of <img> ids */

function setupRotation(timeout) {
	timeoutMS = timeout;
}

function addSlot(name) {
	slots[slots.length] = name;
}

function addImage(name) {
	imageFiles[imageFiles.length] = name;
}

function rotate() {
	/* check for getElementById to enable browsers >= IE 5/NS6 */
	if (document.getElementById) {
		/* first time through, don't rotate anything */
		if (!isFirstTime) {
			var image = document.getElementById(slots[iSlot]);
			image.src = imageFiles[iImage];
			iSlot++;
			if (iSlot >= slots.length) {
				iSlot = 0;
			}
			iImage++;
			if (iImage >= imageFiles.length) {
			  iImage = 0;
			}
		}
		else {
			isFirstTime = false;
		}
		timerID = window.setTimeout("rotate();", timeoutMS);
	}
}
