/* The total number of images to rotate through. */
var imageRotate_TotalImages = 0;

/*
If imageRotate_Preloading is "NO", then images will not be preloaded before they are displayed.
If imageRotate_Preloading is "NEXT", the next image will be preloaded half-way through the pause between images.
If imageRotate_Preloading is "ALL", all the images will be preloaded half-way through the first pause after the first image.
*/
var imageRotate_Preloading = "NEXT";

/* The number if seconds between each image. Decimal values will be treated as miliseconds. */
var imageRotate_NumSecondsPause = 10;

/* The current image we are on. */
var imageRotate_RotatingImageNum = 1;

/* Function called when ImageRotate needs to determine the full image URL for preloading. */
var imageRotate_GetFullURL = null;

/* Function called when the image is changed. */
var imageRotate_OnImageChange = null;

/* Randomize the first image and also return it's value if it needs to be used on the webpage itself. */
function randomizeFirstImage() {
	imageRotate_RotatingImageNum = Math.round( (Math.random() * (imageRotate_TotalImages-1)) + 1);
	return imageRotate_RotatingImageNum;
}

/* Create an array to hold all the preloaded images */
document.imageRotate_Preload = new Array();

/* If the image rotation is RUNing or STOPed. */
var imageRotate_State = "STOP";

/* Holds the ID for the currently running setTimeout */
var imageRotate_NextRotatingImageID = null;

function imageRotate_StartRotation() {
	// Start the rotation if the total number of images is greater than 0, and the person using a recent browser.
	if (document.getElementById && imageRotate_TotalImages > 0) {
		imageRotate_State = "RUN";
		
		// Start the rotation.
		imageRotate_NextRotatingImageID = setTimeout("imageRotate_nextRotatingImage();", imageRotate_NumSecondsPause * 1000);
		
		if (imageRotate_Preloading == "ALL") {
			preloadAllImages();
			//setTimeout("preloadAllImages();", Math.round(imageRotate_NumSecondsPause / 2) * 1000);
		}
		else if (imageRotate_Preloading == "NEXT") {
			// Preload the next image that will be loaded.
			imageRotate_preloadNextImage();
		}
	}
}

function imageRotate_StopRotation() {
	imageRotate_State = "STOP";
	if (!(imageRotate_NextRotatingImageID === null)) {
		clearTimeout(imageRotate_NextRotatingImageID);
	}
	imageRotate_NextRotatingImageID = null;
}

// Preload all the rotating images.
function preloadAllImages() {
	for (i = 1; i <= imageRotate_TotalImages; i++) {
		document.imageRotate_Preload[i] = new Image();
		document.imageRotate_Preload[i].src = imageRotate_GetFullURL(i);
	}
}

// Preload an image.
function imageRotate_preloadNextImage() {
	if (!document.imageRotate_Preload[imageRotate_GetNextNum(true)]) {
		document.imageRotate_Preload[imageRotate_GetNextNum(true)] = new Image();
		document.imageRotate_Preload[imageRotate_GetNextNum(true)].src = imageRotate_GetFullURL(imageRotate_GetNextNum(true));
	}
}

/* Handler for rotating to the next picture after the duration. */
function imageRotate_nextRotatingImage() {
	if (imageRotate_State == "RUN") {
		imageRotate_RotatingImageNum = imageRotate_GetNextNum(true);
		imageRotate_GotoImage(imageRotate_RotatingImageNum);
		imageRotate_NextRotatingImageID = setTimeout("imageRotate_nextRotatingImage();", imageRotate_NumSecondsPause * 1000);
	}
}

function imageRotate_GotoImage(imageNumber) {
	// Set the current rotating image number to the passed image number.
	imageRotate_RotatingImageNum = imageNumber;
	
	// Call the function that changes the image on the web page.
	imageRotate_OnImageChange(imageRotate_RotatingImageNum);
	
	// Preload the next image that will be loaded.
	imageRotate_preloadNextImage();
}

function imageRotate_GotoNext(loop) {
	imageRotate_GotoImage(imageRotate_GetNextNum(loop));
}

function imageRotate_GotoPrevious(loop) {
	imageRotate_GotoImage(imageRotate_GetPrevNum(loop));
}

function imageRotate_IsRunning() {
	return (imageRotate_State == "RUN");
}

function imageRotate_GetCurrentNum() {
	return imageRotate_RotatingImageNum;
}

function imageRotate_GetNextNum(loop) {
	if (imageRotate_RotatingImageNum >= imageRotate_TotalImages && loop == true) {
		return 1;
	}
	else if (imageRotate_RotatingImageNum < imageRotate_TotalImages) {
		return imageRotate_RotatingImageNum + 1;
	}
}

function imageRotate_GetPrevNum(loop) {
	if (imageRotate_RotatingImageNum <= 1 && loop == true) {
		return imageRotate_TotalImages;
	}
	else if (imageRotate_RotatingImageNum > 1) {
		return imageRotate_RotatingImageNum - 1;
	}
}
