// Global variables
	var imageArray = [], currentImage, previousImage, imageCycle = 5;
	

// Initialize
function loadRotator(inNumImages) {
	var i;
	
	// Load image array and set up each image
	for (i = 0; i < inNumImages - 1; i++) {
		imageArray[i] = document.getElementById('homeimage' + i);
		imageArray[i].style.position = 'absolute';
		imageArray[i].style.display= 'block';
		fader(i, 0);
	}
	
	// Initialize with random first image
	currentImage = getRandomInt(imageArray.length - 1);
	previousImage = currentImage - 1;
	if (previousImage < 0) { previousImage = imageArray.length - 1; }	
	fader(currentImage, 100);
	
	// Crossfade with timer
	window.setTimeout('crossfade(100)', 1000);
}

// Crossfade
function crossfade(inOpacity) {		

	// If opacity of current image is not 100
	if (inOpacity < 110) {
		fader(currentImage, inOpacity);
		inOpacity += 10;
		window.setTimeout('crossfade(' + inOpacity + ')', 30);
	} 

	// Otherwise, make the previous image transparent
	else {
		fader(previousImage, 0);
		
		// Make current image the previous image
		previousImage = currentImage;
		currentImage += 1;
		if (currentImage >= imageArray.length) { currentImage = 0; }

		imageArray[previousImage].style.zIndex = 0;
		imageArray[currentImage].style.zIndex = 100;

		window.setTimeout('crossfade(0)', imageCycle * 1000);
		

	}
}

// Crossbrowser fade image 
function fader(inImageId, inOpacity) {
	var obj = imageArray[inImageId];
	
	// Firefox, etc.
	if (obj.style.opacity != null) { obj.style.opacity = (inOpacity/100) - 0.001;	} 
	
	// IE
	else if (obj.style.filter != null) { obj.style.filter = 'alpha(opacity = ' + inOpacity + ')';}
}

// Return random integer between 0 and inLimit
function getRandomInt(inLimit) { return ( Math.floor(Math.random() * (inLimit + 1) ) );}