// -------------------------------------------------------------------
// Simple image crossfade using jQuery
// -------------------------------------------------------------------

if(typeof(RandomOrder)=="undefined")
{
	var RandomOrder = 0; // Linear=0 | Random=1
}

var fadeSpeed   = 500; // Speed of fade in/out
var fadeWait    = 3000; // Time between each fade

var imgInit   = 1; // Do not change
var imgNow    = 1; // Do not change
var timeoutId = 1;

$(document).ready(function() {
	timeoutId = setTimeout('CrossFade()', fadeWait);
	BindStrip();
});

function CrossFade()
{
	if (RandomOrder)
	{
		var imgNext = Math.floor(Math.random()*TotalImages+1)
		if (imgNext==imgNow) { imgNext++; }
	}
	else
	{
		var imgNext = imgNow+1;
	}
	
	if (imgNext>TotalImages) { imgNext=imgInit; }
	
	ChangeImage(imgNow, imgNext);
	timeoutId = setTimeout('CrossFade()', fadeWait);
	imgNow = imgNext;
}

function Jumpto(newimg)
{
	// Change image and stop cycling
	if (timeoutId>0)
	{
		clearTimeout(timeoutId);
		timeoutId=0;
	}
	ChangeImage(imgNow, newimg);
	imgNow = newimg;
}


function ChangeImage(now, next)
{
	$("#photo"+now).css("z-index", "2").fadeOut(fadeSpeed);
	$("#photo"+next).css("display", "none").css("z-index", "1").fadeIn(fadeSpeed);
}

function BindStrip()
{
	// Dynamically creates thumbnail buttons for each of the images defined in a page.
	// The thumbnails are the original images resized into a smaller area.  Size is
	// controlled in common.css. This is not a true resize where the thumbnails are
	// smaller in file size.  All you need to add is "<div id="strip"></div>"
	var i = 0;
	$("#fader").children("img").each(function(){
		i++;
		$("#strip").append('<img src="'+$(this).attr("src")+'" onclick="Jumpto('+i+')" />');
	});
}
