// Image Rotator - rotate a set of images
//
// There are two objects defined here:
//   ImageRotator - takes a set of RotateImage objects and rotates them
//   RotateImage - an image that is used in an array for ImageRotator
//
function ImageRotator(imageArray, imageMain, rotateTime)
// imageArray - array of  RotateImage objects
// imageMain - base of id for image and image link (i.e. "main_image" => <a id="main_image_a" ... > and <img id="main_image_img" ... >
{
	this.imageArray = imageArray;
	this.mainDivId = imageMain; // id of div that contains the image and link
	this.mainImageId = imageMain + ImageRotator.IMG_SUFFIX; // id of image to rotate
	this.mainLinkId = imageMain + ImageRotator.A_SUFFIX; // id of anchor of image to rotate
	this.rotateTime = rotateTime;
	this.currentImg = 0;
}

function RotateImage(imageFile, imageNav, altText, linkURL)
{
	this.imageFile = RotateImage.IMAGE_DIR + imageFile; // path name for main image
	this.imageNav = null;
	if (imageNav && imageNav != "") {
		this.imageNav = imageNav; // id of nav image to swap (may be empty)
	}
	this.altText = altText;
	this.linkURL = linkURL;
}

RotateImage.IMAGE_DIR = "images/"; // Image directory
RotateImage.ON_SUFFIX = "_on.gif"; // Add to imageNav to get image file name

ImageRotator.A_SUFFIX = "_a"; // suffix for imageMain to get id for <a>
ImageRotator.IMG_SUFFIX = "_img"; // suffix for imageMain to get id for <img>

// Pre-loads any image that is not initially displayed when the page loads
RotateImage.prototype.preLoad = function()
{
	var myImg = new Image();
	myImg.src = this.imageFile;
	if (this.imageNav)
	{
		var myImg2 = new Image();
		myImg2.src = RotateImage.IMAGE_DIR + this.imageNav + RotateImage.ON_SUFFIX;
	}
}

ImageRotator.prototype.swapContentImage = function()
{
	var setImgOn = false;
	if (this.imageArray[this.currentImg].imageNav)
	{
		var imageOn = document.images[this.imageArray[this.currentImg].imageNav].src;
		if (imageOn.indexOf("_on") > 0) {
			setImgOn = true;
			btn_swap(this.imageArray[this.currentImg].imageNav, 'out');
		}
	}

	// Increment the counter to go to the next image
	if ((this.currentImg + 1) > (this.imageArray.length - 1)) {
		this.currentImg = 0;
	} else {
		this.currentImg += 1;
	}

	// Swap the image
	document.getElementById(this.mainImageId).alt = this.imageArray[this.currentImg].altText;
	document.getElementById(this.mainImageId).src = this.imageArray[this.currentImg].imageFile;

	// Swap the link
	document.getElementById(this.mainLinkId).href = this.imageArray[this.currentImg].linkURL;

	// Swap the nav button
	if (this.imageArray[this.currentImg].imageNav) {
		var thisObj = this;
		document.getElementById(this.mainLinkId).onmouseover = function(){btn_swap(thisObj.imageArray[thisObj.currentImg].imageNav, 'over');};
		document.getElementById(this.mainLinkId).onmouseout = function(){btn_swap(thisObj.imageArray[thisObj.currentImg].imageNav, 'out');};
	}

	// Reset the nav button
	if (setImgOn == true && this.imageArray[this.currentImg].imageNav) {
		btn_swap(this.imageArray[this.currentImg].imageNav, 'over');
	}
	//setTimeout(fadeIn, 100);
	this.fadeIn();
}

ImageRotator.prototype.fadeOut = function()
{
	// Fade the main content image to 0 in 1000 msec
	currentOpac(this.mainDivId, 0, 1000);

	// Swap the images after 1000 msec (when the fade is complete)
	var thisObj = this;
	setTimeout(function(){thisObj.swapContentImage();}, 1000);
}

ImageRotator.prototype.fadeIn = function()
{
	// Fade the main content image to 100 in 1000 msec
	currentOpac(this.mainDivId, 100, 1000);

	// Wait a while, then start the next fade out
	var thisObj = this;
	setTimeout(function() {thisObj.fadeOut();}, this.rotateTime);
}

ImageRotator.prototype.init = function()
{
	// Set the alt for the initial image
	document.getElementById(this.mainImageId).alt = this.imageArray[this.currentImg].altText;
	if (this.imageArray[this.currentImg].imageNav)
	{
		var thisObj = this;
		document.getElementById(this.mainLinkId).onmouseover = function(){
			btn_swap(thisObj.imageArray[thisObj.currentImg].imageNav, 'over');
		};
		document.getElementById(this.mainLinkId).onmouseout = function(){
			btn_swap(thisObj.imageArray[thisObj.currentImg].imageNav, 'out');
		};
	}

	// Set the href for the image anchor
	document.getElementById(this.mainLinkId).href = this.imageArray[this.currentImg].linkURL;

	// Preload all of the images
	for (var index = 0; index < this.imageArray.length; ++index)
	{
		this.imageArray[index].preLoad();
	}

	// Wait a while, then start the fade out
	var thisObj = this;
	setTimeout(function() {thisObj.fadeOut();}, this.rotateTime);

	// Make sure the current opacity is 100
	currentOpac(this.mainDivId, 100, 100);
}

// Call setup after creating the object
ImageRotator.prototype.setup = function()
{
	// Call init() when the window loads
	var thisObj = this;
	if (window.addEventListener) { window.addEventListener("load", function() { thisObj.init(); }, false); }
	else if (window.attachEvent) { window.attachEvent("onload", function() { thisObj.init(); }); }
}
