var mainPic = null;
var imageContainer = null;
var noPics = null;
var imageName = "images/backgrounds/"+imageDirectory+"/back_{IMAGE_NO}.jpg";
var imageNameRegEx = "images/backgrounds/"+imageDirectory+"/back_";
var defaultImageUrl = "images/backgrounds/general/back_01.jpg";
var lastImgName = "";
/**
 * Time in milliseconds between each image fading out. Decrease this
 * to rotate the images faster, increase to slow it down
 */
var timeBetweenChanges = 8000;
/**
 * Time in milliseconds between each fade pass. As the image fades out
 * the script makes multiple passes and decreases the opacity of the 
 * image. This is the time between each pass. The lower this is the
 * smoother the fade looks, too long and the fading doesn't look like
 * one motion, its very fragmented. 
 */
var timeBetweenFades = 50;
/**
 * The fade change to apply to each pass. The higher this is the less
 * passes the script will need to make the image totally see-through.
 * Too high and the fade will be too quick and the fade effect will
 * not be seen. Too low and it will take a long time to fade out.
 * Recommended to have this low and also have the timeBetweenFades
 * low, that way the fading looks good. However, if these are too low
 * it could put a large load on the client computer.
 */
var fadeDifference = 0.05;
/**
 * Global variable used in the script. DO NOT change, this is the 
 * current opacity of the image.
 */
var currentOpacity = 1.0;
/**
 * The current time. Leave this as 0.
 */
var currentTime = 0;
/**
 * Whether to fade out the image or fade in the new image. Global
 * variable, do not change.
 */
var fadeOut = true;
/**
 * Indicates if the script has loaded correctly.
 */
var okToStart = false;
/**
 * Indicates if the image is currently fading.
 */
var fading = false;
/**
 * Image that has been preloaded
 */
var preLoadedImage = null;
/**
 * Display the default image
 */
var displayDefaultImage = false;

function changePic()
{
	if (okToStart)
	{
		
		if (document.images)
		{
			if (preLoadedImage == null)
			{
				var imgSrc = imageName.replace(/{.+}/g, getRandomPic());
				var i = 0;
				while (imgSrc == lastImgName && i < 100)
				{
					imgSrc = imageName.replace(/{.+}/g, getRandomPic());
					i++;
				}
				if (i >= 100)
				{
					okToStart = false;
					displayDefaultImage = true;
					return;
				}
				lastImgName = imgSrc;
				preLoadedImage = new Image(745, 150);
				preLoadedImage.src = imgSrc;
			}
		}
		if (currentTime >= timeBetweenChanges)
		{
			currentTime = 0;
			var newImgSrc = "";
			if (preLoadedImage != null)
			{
				newImgSrc = preLoadedImage.src;
			}
			else
			{
				newImgSrc = imageName.replace(/{.+}/g, getRandomPic());
				var i = 0;
				while (newImgSrc == lastImgName && i < 100)
				{
					newImgSrc = imageName.replace(/{.+}/g, getRandomPic());
					i++;
				}
				if (i >= 100)
				{
					okToStart = false;
					displayDefaultImage = true;
					return;
				}
				lastImgName = newImgSrc;
			}
			
			fading = true;
			beginTransition(newImgSrc);
		}
		if (!fading)
			window.setTimeout("wait()", 500);				
	}
}

function wait()
{
	currentTime += 500;
	changePic();
}

function beginTransition(newImgSrc)
{
	imageContainer.style.backgroundColor = '#ffffff';
	if (currentOpacity > 0 && currentOpacity <= 1)
	{
		if (fadeOut) fadeOutImg();
		else fadeInImg();
		waitFade(newImgSrc);
	}
	else if (currentOpacity <= 0)
	{
		if (fadeOut == true)
			mainPic.src = newImgSrc;
		
		fadeOut = false;
		fadeInImg();
		waitFade(newImgSrc);
	}
	else if (currentOpacity >= 1)
	{
		imageContainer.style.backgroundColor = '#000000';
		fadeOutImg();
		fadeOut = true;
		fading = false;
		preLoadedImage = null;
		changePic();
	}
}

function waitFade(newImgSrc)
{
	window.setTimeout('beginTransition(\''+newImgSrc+'\');', timeBetweenFades);
}

function fade(fadeIn)
{
	if (fadeIn == null) fadeIn = false;
	
	currentOpacity = (fadeIn) ? currentOpacity + fadeDifference : currentOpacity - fadeDifference;
	mainPic.style.opacity = currentOpacity.toString();
	mainPic.style.filter = "alpha(opacity="+(Math.round(currentOpacity * 100)).toString()+")";
		
}

function fadeInImg()
{
	fade(true);
}

function fadeOutImg()
{
	fade(false);
}

function getRandomPic()
{
	if (okToStart)
	{

		var imgNo =  Math.ceil(Math.random() * noPics);
		if (imgNo < 10) return "0" + imgNo.toString();
		else return imgNo.toString();
	}
	return null;
}

function setUp(numberOfPics)
{
	mainPic = document.getElementById("mainPic");
	imageContainer = document.getElementById("imageContainer");
	noPics = parseInt(numberOfPics);
	if (isNaN(noPics)) throw "Number of pictures must be an integer";
	lastImgName = mainPic.src.substring(mainPic.src.indexOf(imageNameRegEx));
	if (noPics > 0) okToStart = true;
	changePic();
}

function toggleNodeDisplay(sender, targetId)
{
	var target = dojo.byId(targetId);
	if (target != null)
	{
		if (dojo.getComputedStyle(target).display == 'none')
		{	
			dojox.fx.wipeIn({
				node:target,
				duration:500,
				height:500
			}).play();
			sender.innerHTML = 'hide';
		}
		else
		{
			dojox.fx.wipeOut({
				node:target,
				duration:500
			}).play();
			sender.innerHTML = 'show';
		}
	}
}