// Dynamic slideshow effect for any number of image tags in the page (the number of tags is also determined dynamically).
// To implement, you must enter the following onload event into the Body tag of the page:
//     onload="startSlideshow('cgi-photo','images/structure/cgi-photo','.jpg')"
//     'cgi-photo' == the prefix of the IMG tags that will contain the slides (MUST be numbered, e.g., cgi-photo1, cgi-photo2, etc).
//     'images/structure/cgi-photo' == the path and naming prefix of the photos used in the slides.
//     '.jpg' == is the sufix of the images.
// --------------------------------------------------------------------------------

	var sld_waitPeriod=4500;

	var sld_currentIndex=0;
	var sld_tid,sld_tslide;
	var slideNames=new Array();
	var sld_fileName="";
	var sld_i;
	var sld_j=1;
	var sld_divCount=0;
	var sld_imgCount=0;
	var sld_imagePath,sld_imageTagPrefix,sld_imageSufix;
	var sld_tempID="";
	var sld_tempSrc="";
	var sld_tempIndex=0;
	var sld_tempImage;
	var sld_tempString="";
	var sld_tempReps=0;
	var sld_browser=navigator.appName;
	var sld_imgLoader= new Image();
	var sld_imgPreLoader= new Image();
	var sld_effectDone= new Boolean(false);
	
	function startSlideshow(sld_imgTagPrefix, sld_imgPath, sld_imgSufix)
	{
		// Startup AJAX.
		sld_req = getRequest();
		
		// Reset variables.
		sld_divCount=0;
		sld_imgCount=0;
		
		// Load parameters into constants.
		sld_imageTagPrefix=sld_imgTagPrefix;
		sld_imagePath=sld_imgPath;
		sld_imageSufix=sld_imgSufix;
		
		// Find out how many photo DIVs there are.
		for (sld_i=0;sld_i<=sld_divCount;sld_i++)
		{
			if(document.getElementById(sld_imageTagPrefix + (sld_i+1))) sld_divCount++;
		}
		
		// Find out how many photos there are (by looking for photos one at a time).
		sld_fileName=sld_imagePath + "1_" + (sld_currentIndex +1) + sld_imageSufix;
		loadFile();
	}

	sld_imgLoader.onload = function (evt)
	{
		// Load the file into the photo array.
		slideNames[sld_currentIndex]=sld_fileName;
		
		// Increase the image index.
		sld_currentIndex++;
		sld_imgCount++;
		sld_fileName=sld_imagePath + "1_" + (sld_currentIndex +1) + sld_imageSufix;
		
		// Continue looking for images to load.
		loadFile();
	}
	
	sld_imgLoader.onerror = function (evt)
	{
		sld_currentIndex=2;
		
		sld_j=sld_divCount;
		sld_i=sld_imgCount+1;
		
		sld_preLoad();
	}
	
	function loadFile()
	{	
		// Load images into temporary loading object.
		sld_imgLoader.src=sld_fileName;
	}
	
	// Creates the XMLHttpRequest object to start AJAX.
	function getRequest()
	{
		if(window.XMLHttpRequest)
		{
			return new XMLHttpRequest;
		}
		else if(window.ActiveXObject)
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			alert("Your sld_browser does not support dymanic content using AJAX. Please upgrade to a modern sld_browser. /n This page will not display properly.");
			return 0;
		}
	}

	function sld_preLoad()
	{
		if (sld_i>=1)
		{
			sld_i--;
		}
		else
		{
			sld_i=sld_imgCount+1;
			if (sld_j>=1)
			{
				sld_j--;
			}
		}
		sld_tempSrc=sld_imagePath + sld_j + "_" + sld_i + sld_imageSufix;
		sld_imgPreLoader.src=sld_tempSrc;
	}
	
	sld_imgPreLoader.onload = function (evt)
	{
		sld_preLoad();
	}
	
	sld_imgPreLoader.onerror = function (evt)
	{
		sld_tslide=setTimeout("loopSlideshow()", (sld_waitPeriod-2000));
	}

	function loopSlideshow()
	{
		// Reset sld_j if we run out of DIVs to load images into.
		if (sld_j>sld_divCount) sld_j=1;
		if (sld_j<1) sld_j=1;
		if (sld_currentIndex<1) sld_currentIndex=1;
		
		sld_tempSrc=sld_imagePath + sld_j + "_" + sld_currentIndex + sld_imageSufix
		
		// Define the current image ID and image object.
		sld_tempID=sld_imageTagPrefix + sld_j;
		sld_tempImage = document.getElementById(sld_tempID);

		// Fade the current image.
		sld_effectsFade(sld_tempID, 100, 0, 250, "nextSlide()");
	}
	
	function nextSlide() // Move on to the next slide.
	{
		// Define the next image, and load it.
		sld_tempImage.style.visibility = "hidden";
		sld_tempImage.src = sld_tempSrc;
		
		sld_effectsFade(sld_tempID, 0, 10, 2000, "showImageDiv()");
		
		// Move index to next set of images.
		if (sld_j>=sld_divCount)
		{
			if (sld_currentIndex>=sld_imgCount)
			{
				// Cycles back to the first set of images.
				sld_currentIndex=1;
			}
			else
			{
				// Move on to the next set of images.
				sld_currentIndex++;
			}
		}
		sld_j++;

		// Ensure that previous running of this task is done.
		clearTimeout(sld_tslide);
		// Keep the slideshow going.
		sld_tslide=setTimeout("loopSlideshow()", sld_waitPeriod);
	}
	
	function showImageDiv ()
	{
		sld_tempImage.style.visibility = "visible";
		sld_effectsFade(sld_tempID, 10, 100, 500);
	}
	
	
// ------------------------------------------------------------------
// Fade functions
// ------------------------------------------------------------------
	
function sld_effectsFade (id, opacStart, opacEnd, duration, callback)
{
	sld_changeOpacity(opacStart, id);
	var speed = Math.round(duration/100);
	var timer = 0;

	if(opacStart > opacEnd)
	{
		for(var i=opacStart; i>=opacEnd; i--)
		{
			setTimeout("sld_changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +"')", (timer*speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd)
	{
		for(var i=opacStart; i<=opacEnd; i++)
		{
			setTimeout("sld_changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +"')", (timer*speed));
			timer++;
		}
	}
}

function sld_changeOpacity (opacity, id, endPoint, callback)
{
	var _style = document.getElementById(id).style;
    _style.opacity = (opacity / 100);
    _style.MozOpacity = (opacity / 100);
    _style.KhtmlOpacity = (opacity / 100);
    _style.filter = "alpha(opacity=" + opacity + ")";
	
	if(opacity == endPoint && callback != null)
	{
		eval(callback);
	}
}
