var pImages = new Array();		// An array of all the images added
var pfOpacity = new Array();		// An array of the opacity for each image
					// This is needed because dObject.style.opacity is based on the stylesheet	
	
var nImageCount = 0;			// Total number of images
var nCurrImage = 0;			// The currently fading out image
var nNextImage = 0;			// The currently fading in image

var nImageSwitchDelay = 5000;		// The number of milliseconds to wait before swapping the image.
var fOpacityPerFrame = 0.1;		// The amount to change the opacity per animation frame. 0.1 would be 10% per frame
var fAnimationRate = 33;		// The speed in milliseconds to animate each frame. 33 is 30 FPS

var pPreload = new Array();
var nPreloadCnt = 0;

var dImageRotator;			// Document object references

function PreLoad(szFile)
{
	pPreload[nPreloadCnt] = new Image();
	pPreload[nPreloadCnt].src = szFile;
	
	nPreloadCnt++;
}

function AddImage(szFileName)
{
	pImages[nImageCount] = document.createElement("img");
	pImages[nImageCount].src = szFileName;
	pfOpacity[nImageCount] = 0.0;
	dImageRotator.appendChild(pImages[nImageCount]);
	nImageCount++;
}

function IncludeImages()
{
	var x = 0;
	var d = null;
	
	if(dImageRotator && dImageRotator.hasChildNodes())
	{
		for(x = 0; x < dImageRotator.childNodes.length; x++)
		{
			d = dImageRotator.childNodes[x];
			
			if(d.tagName == "img")
			{
				pImages[nImageCount] = d;
				pfOpacity[nImageCount] = (nImageCount == 0 ? 1.0 : 0.0);
				nImageCount++;
			}
		}
	}
}

function RotateImages()
{
	if(nImageCount >= 2)
	{
		nCurrImage = nNextImage;
		nNextImage++;
		if(nNextImage >= nImageCount)
		{
			nNextImage = 0;
		}
		pfOpacity[nCurrImage] = 1.0;
		pImages[nCurrImage].style.opacity = pfOpacity[nCurrImage];
		AnimateImages();
	}
}

function AnimateImages()
{
	var bProceed = false;	// Should we rotate again?
	
	pfOpacity[nCurrImage] -= fOpacityPerFrame;
	pfOpacity[nNextImage] += fOpacityPerFrame;
	
	if(pfOpacity[nCurrImage] <= 0.0)
	{
		pfOpacity[nCurrImage] = 0.0;
		bProceed = true;
	}
	if(pfOpacity[nNextImage] >= 1.0)
	{
		pfOpacity[nNextImage] = 1.0;
		bProceed = true;
}		
	
	pImages[nCurrImage].style.opacity = pfOpacity[nCurrImage];
	pImages[nCurrImage].style.filter = "alpha(opacity=" + (pfOpacity[nCurrImage] * 100) + ")";
	pImages[nNextImage].style.opacity = pfOpacity[nNextImage];
	pImages[nNextImage].style.filter = "alpha(opacity=" + (pfOpacity[nNextImage] * 100) + ")";
	
	if(bProceed)
	{
		setTimeout("RotateImages()", nImageSwitchDelay);
	}
	else
	{
		setTimeout("AnimateImages()", fAnimationRate);
	}
}

function Init()
{
	dImageRotator = document.getElementById("dImageRotator");
	IncludeImages();
	AddImage("img/home/food1.jpg");
	AddImage("img/home/food2.jpg");
	AddImage("img/home/food3.jpg");
	AddImage("img/home/food4.jpg");
	AddImage("img/home/food5.jpg");
	AddImage("img/home/food6.jpg");
	AddImage("img/home/food7.jpg");
	AddImage("img/home/food8.jpg");
	AddImage("img/home/food9.jpg");
	AddImage("img/home/food10.jpg");
	AddImage("img/home/food11.jpg");
	AddImage("img/home/food12.jpg");
	AddImage("img/home/food13.jpg");
	AddImage("img/home/food14.jpg");
	AddImage("img/home/food15.jpg");
	AddImage("img/home/food16.jpg");
	AddImage("img/home/food0.jpg");
	AddImage("img/home/food18.jpg");
	
	CheckComplete();
}

function CheckComplete()
{
	var nLast = 0;
	
	if(pImages.length != 0)
	{
		nLast = pImages.length - 1;
		
		if(pImages[nLast].complete)
		{
			RotateImages();
		}
		else
		{
			setTimeout("CheckComplete()", 500);
		}
	}
}

PreLoad("img/home/food17.jpg");
PreLoad("img/hospitality-location-1-bw.jpg");
PreLoad("img/hospitality-location-1.jpg");
PreLoad("img/hospitality-location-2-bw.jpg");
PreLoad("img/hospitality-location-2.jpg");
PreLoad("img/hospitality-location-3-bw.jpg");
PreLoad("img/hospitality-location-3.jpg");
