// 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',2,47)"
//     'cgi-photo' == the prefix of the ID 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.
//     2 == is the number of simultaneous/staggered slide images in this page.
//     47 == OPTIONAL: the number of image files in the image folder. This will be determined dynamically if not given (slower).
// --------------------------------------------------------------------------------
// WARNING: Because Internet Explorer requires the use of filters for transparency, PNG images have jagged edges during the fade period (but not before/after fading is done).
// --------------------------------------------------------------------------------

	var sld_waitPeriod=5000;

	var sld_req;
	var sld_tempName;
	var sld_currentIndex=0;
	var sld_tid,sld_tslide;
	var slideNames=new Array();
	var sld_fileName="";
	var sld_i, sld_k;
	var sld_j=0;
	var sld_divCount=0;
	var sld_imgCount=0;
	var sld_imagePath,sld_imageTagPrefix,sld_imageSufix;
	var sld_tempID="";
	var sld_loadSrc="";
	var sld_tempSrc="";
	var sld_tempIndex=0;
	var sld_tempImage;
	var sld_tempString="";
	var sld_tempReps=0;
	var sld_browser=navigator.appName;
	var sld_b_version=navigator.appVersion;
	var sld_version=parseFloat(sld_b_version);

	var sld_imgLoader= new Image();
	var sld_imgPreLoader= new Image();
	var sld_blankWait=0030;
	var sld_fadeWait=500;
	var sld_perPageCount;
	var sld_countExist= new Boolean(false);

	// 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
		{
			return 0;
		}
	}

	function startSlideshow(sld_imgTagPrefix, sld_imgPath, sld_imgSufix, sld_arrayCount, sld_photosCount)
	{
		//alert("Browser: " + sld_browser + " (Version: "+ sld_version + ")");
		//alert("Starting slide show...");
		// Set the wait period to be longer in Webkit browsers (e.g., Chrome and Safari).
		if (sld_browser=="Netscape")
		{
			//sld_blankWait=1320;
		}
		if(sld_browser=="Microsoft Internet Explorer") sld_fadeWait=150;

		// Reset variables.
		sld_divCount=0;
		sld_imgCount=0;
		
		// Load parameters into constants.
		sld_imageTagPrefix=sld_imgTagPrefix;
		sld_imagePath=sld_imgPath;
		sld_imageSufix=sld_imgSufix;
		sld_perPageCount=sld_arrayCount;
		
		if (sld_perPageCount>1)
		{
			sld_fileName=sld_imagePath + "1_" + (sld_currentIndex +1) + sld_imageSufix;
		}
		else
		{
			sld_fileName=sld_imagePath + (sld_currentIndex +1) + sld_imageSufix;
		}
		
		// If the number of photos is already known, then start preloading. Otherwise, find out.
		if (sld_photosCount)
		{
			sld_imgCount=sld_photosCount;
			sld_i=1;
			sld_j=2;
			sld_k=sld_perPageCount;
			sld_countExist=true;

	
			// Startup AJAX.
			sld_req = getRequest();
			if (sld_req!=0)
			{
				// Start the loading process.
				sld_preLoad();
			}
			else
			{
				alert("Your browser does not support AJAX. This page cannot be properly displayed.");
			}

		}
		else
		{
			sld_countExist=false;

			// Startup AJAX.
			sld_req = getRequest();
			if (sld_req!=0)
			{
				// Start the loading process.
				sld_loadFile();
			}
			else
			{
				alert("Your browser does not support AJAX. This page cannot be properly displayed.");
			}
		}
	}

	sld_imgLoader.onload = function (evt)
	{
		// Increase the image index.
		sld_imgCount++;

		// Continue looking for images to load.
		sld_loadFile();
	}
	
	sld_imgLoader.onerror = function (evt)
	{
		sld_currentIndex=2;
		
		sld_j=sld_perPageCount;
		//alert("sld_j = " + sld_j);
		sld_i=sld_imgCount+1;
		
		sld_preLoad();
	}
/*	
	function sld_loadFile()
	{	
		// Load images into temporary loading object.
		if (sld_browser=="Netscape")
		{
			sld_imgLoader.src=sld_fileName+"?" + (new Date()).getTime();
		}
		else
		{
			sld_imgLoader.src=sld_fileName;
		}
	}
*/	
	function sld_preLoad()
	{
		if (sld_perPageCount>1) // We have an array of images in our hands.
		{
			if (sld_i<sld_imgCount)
			{
				sld_i++;
			}
			else
			{
				sld_i=1;
				if (sld_k<=sld_perPageCount)
				{
					sld_k++;
				}
			}
			sld_loadSrc=sld_imagePath + sld_k + "_" + sld_i + sld_imageSufix;
		}
		else // There is only one slideshow image per page.
		{
			sld_i++;
			sld_loadSrc=sld_imagePath + sld_i + sld_imageSufix;
		}

/*
		if (sld_browser=="Netscape")
		{
			//sld_imgPreLoader.src=sld_loadSrc+"?" + (new Date()).getTime();
			sld_imgPreLoader.src=sld_loadSrc; //+"?" + (new Date()).getTime();
		}
		else
		{
			sld_imgPreLoader.src=sld_loadSrc;
		}
*/
		sld_tempName=sld_loadSrc;
		if(sld_browser=="Microsoft Internet Explorer" && sld_version>=4) // Loads the gallery using AJAX if the user's browser is IE.
		{
			//alert("sld_tempName = "+sld_tempName);
			//alert("IE version "+ sld_version);
			sld_req = getRequest();
			if (sld_req!=0)
			{
				setTimeout(sld_ajaxStateImg_pre, 100);
			}
			else
			{
				alert("Your browser does not support AJAX. This page cannot be properly displayed.");
			}

		}
		else // Otherwise, just use normal javascript.
		{
			sld_imgPreLoader.src=sld_tempName;
		}


	}
	
	sld_imgPreLoader.onload = function (evt)
	{
		if (sld_i >= sld_imgCount)
		{
			sld_j=2;
			//loopSlideshow();
			// Ensure that previous running of this task is done.
			clearTimeout(sld_tslide);
			// Keep the slideshow going.
			sld_tslide=setTimeout("loopSlideshow()", 3500);
			//alert("Start...");
		}
		else
		{
			sld_preLoad();
		} 
	}
	
	sld_imgPreLoader.onerror = function (evt)
	{
			sld_j=2;
			loopSlideshow();

			// Ensure that previous running of this task is done.
			//clearTimeout(sld_tslide);
			// Keep the slideshow going.
			//sld_tslide=setTimeout("loopSlideshow()", 2000);
			//alert("Start...");
	}







	// --------------------------------------------------------------------------------------------
	// AJAX Version (for IE issues).
	// IMPORTANT: IE gives you a "Stack overflow error on line: 0" error without this function.
	function sld_imageExistsIE_pre()
	{
		// if the XMLHttpRequest is finished loading
		if(sld_req.readyState == 4)
		{
			//alert("sld_req.status = " + sld_req.status);
			//alert("sld_req.readyState = " + sld_req.readyState);
			
			// and the file actually exists
			if(sld_req.status == 200)
			{
				//alert("image found");
				if (sld_i >= sld_imgCount)
				{
					sld_j=2;
					//loopSlideshow();
					// Ensure that previous running of this task is done.
					clearTimeout(sld_tslide);
					// Keep the slideshow going.
					sld_tslide=setTimeout("loopSlideshow()", 3500);
					//alert("Start...");
				}
				else
				{
					sld_preLoad();
				} 
			}
			else
			{
				//alert("Image does not exist.");
				sld_j=2;
				loopSlideshow();
			}
		}
	}
	
	function sld_ajaxStateImg_pre()
	{
		//alert("Loading...");
		sld_req.onreadystatechange = sld_imageExistsIE_pre;
		sld_req.open("get", sld_tempName, true);
		sld_req.send(null);
	}



	function sld_imageExistsIE()
	{
		// if the XMLHttpRequest is finished loading
		if(sld_req.readyState == 4)
		{
			//alert("sld_req.status = " + sld_req.status);
			//alert("sld_req.readyState = " + sld_req.readyState);
			
			// and the file actually exists
			if(sld_req.status == 200)
			{
				//alert("image found");
				// Increase the image index.
				sld_imgCount++;
		
				// Continue looking for images to load.
				sld_loadFile();
			}
			else
			{
				//alert("Image does not exist.");
				// Done loading.
				sld_currentIndex=2;
				
				sld_j=sld_perPageCount;
				//alert("sld_j = " + sld_j);
				sld_i=sld_imgCount+1;
				
				sld_preLoad();
			}
		}
	}
	
	function sld_ajaxStateImg()
	{
		//alert("Loading...");
		sld_req.onreadystatechange = sld_imageExistsIE;
		sld_req.open("get", sld_tempName, true);
		sld_req.send(null);
	}

	function sld_loadFile()
	{
		sld_tempName=sld_fileName;
		if(sld_browser=="Microsoft Internet Explorer" && sld_version>=4) // Loads the gallery using AJAX if the user's browser is IE.
		{
			//alert("sld_tempName = "+sld_tempName);
			sld_req = getRequest();
			if (sld_req!=0)
			{
				setTimeout(sld_ajaxStateImg, 100);
			}
			else
			{
				alert("Your browser does not support AJAX. This page cannot be properly displayed.");
			}

		}
		else // Otherwise, just use normal javascript.
		{
			sld_imgLoader.src=sld_tempName;
		}
	}








	function loopSlideshow()
	{
		//alert("Loop...");
		if (sld_perPageCount>1) // We have an array of images in our hands.
		{
			// Reset sld_j if we run out of DIVs to load images into.
			if (sld_j>sld_perPageCount)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);
		}
		else // There is only one slideshow image per page.
		{
			if (sld_j<1 || sld_j>sld_imgCount) sld_j=1;
			
			sld_tempSrc=sld_imagePath + sld_j + sld_imageSufix;
			
			// Define the current image ID and image object.
			sld_tempID=sld_imageTagPrefix + "1";
			sld_tempImage = document.getElementById(sld_tempID);
		}

		// Fade the current image.
		sld_effectsFade(sld_tempID, 100, 0, sld_fadeWait, "sld_nextSlide()");
	}
	
	function sld_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, 100, sld_blankWait, "showImageDiv()");
	}
	
	function delayedLoop()
	{
		// Fix the image's transparency if it is a PNG.
		if (sld_imageSufix==".png" || sld_imageSufix==".PNG") sld_tempImage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + sld_tempSrc + ")";

		if (sld_perPageCount>1) // We have an array of images in our hands.
		{
			// Move index to next set of images.
			if (sld_j>=sld_perPageCount)
			{
				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, 0, 100, (sld_fadeWait*2), "delayedLoop()");
	}
	
	
// ------------------------------------------------------------------
// Fade functions
// ------------------------------------------------------------------
	
function sld_effectsFade(id, opacStart, opacEnd, duration, callback)
{
	sld_changeOpacity(opacStart, id);
	var speed = Math.round(duration*0.01);
	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*0.01);
    _style.MozOpacity = (opacity*0.01);
    _style.KhtmlOpacity = (opacity*0.01);
    _style.filter = "alpha(opacity=" + opacity + ")";
	
	if(opacity == endPoint && callback != null)
	{
		eval(callback);
	}
}