var mainImage;
var mainImageContainer;
var lastImgName = "";
var imageUrl = ('undefined' != typeof(_picConfig.imageUrl)) ? _picConfig.imageUrl : '/image/image/';
/**
 * Time in milliseconds between each image fading out. Decrease this
 * to rotate the images faster, increase to slow it down
 */
var timeBetweenChanges = ('undefined' != typeof(_picConfig.timeBetweenChanges)) ? parseInt(_picConfig.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 = ('undefined' != typeof(_picConfig.timeBetweenFades)) ? parseInt(_picConfig.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 = ('undefined' != typeof(_picConfig.fadeDifference)) ? parseFloat(_picConfig.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 = imageUrl + Math.random();
				
				lastImgName = imgSrc;
				preLoadedImage = new Image(745, 150);
				preLoadedImage.src = imgSrc;
				imageUrl = imgSrc.substring(0, imgSrc.lastIndexOf('/') + 1);
			}
		}
		if (currentTime >= timeBetweenChanges)
		{
			currentTime = 0;
			var newImgSrc = "";
			if (preLoadedImage != null)
			{
				newImgSrc = preLoadedImage.src;
			}
			else
			{
				newImgSrc = imageUrl + Math.random();				
				lastImgName = newImgSrc;
				imageUrl = newImgSrc;
			}
			
			fading = true;
			beginTransition(newImgSrc);
		}
		if (!fading)
			window.setTimeout("wait()", 500);				
	}
}

function wait()
{
	currentTime += 500;
	changePic();
}

function beginTransition(newImgSrc)
{
	mainImageContainer.style.backgroundColor = '#ffffff';
	if (currentOpacity > 0 && currentOpacity <= 1)
	{
		if (fadeOut) fadeOutImg();
		else fadeInImg();
		waitFade(newImgSrc);
	}
	else if (currentOpacity <= 0)
	{
		if (fadeOut == true)
			mainImage.src = newImgSrc;
		
		fadeOut = false;
		fadeInImg();
		waitFade(newImgSrc);
	}
	else if (currentOpacity >= 1)
	{
		mainImageContainer.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;
	mainImage.style.opacity = currentOpacity.toString();
	mainImage.style.filter = "alpha(opacity="+(Math.round(currentOpacity * 100)).toString()+")";
		
}

function fadeInImg()
{
	fade(true);
}

function fadeOutImg()
{
	fade(false);
}

function setUp()
{
	mainImage = document.getElementById("mainImage");
	mainImageContainer = document.getElementById("mainImageContainer");
	//noPics = parseInt(_picConfig.noFiles);
	//if (isNaN(noPics)) throw "Number of pictures must be an integer";
	lastImgName = mainImage.src;
	//if (noPics > 0) okToStart = true;
	okToStart = true;
	changePic();
}
