/*
	Author: Chris Jackson
	Date: 5/12/2008
	Description: Script loops through xml file, switching through images with a fade between
	Structure: fadeImageOut() -> swithImage() -> fadeImageIn() -> fadeImageOut(), etc
	Dependencies: /includes/_jQuery/_Library/jquery.1.2.js
*/

/***********************************
	Global settings
/***********************************/
var interval = 750; // Total time for image fade
var holdTime = 3000; // Time to hold image before switching

var $j = jQuery.noConflict();

$j(document).ready(function() {
	initImageFader('include/js/featured-clients.xml', 'images', 'image', 'client_logofilename', 'client_name', 'photoimg')
});

function initImageFader(xmlSrc, xmlTopLevelTag, xmlSecondLevelTag, xmlImgSrcTag, xmlImgAltTag, photoImgId) {
	var imgIndex = 0;
	var initialImgArray;
	var imgArray = new Array();
	var theimg = document.getElementById(photoImgId);
	var imgPreloadArray = new Array();
	var preloadIndex = 0;
	// Store initial image, which is picked randomly
	var initialImg = theimg.src;
	 $j.ajax({
	   url: xmlSrc,
	   dataType: "xml",
	   success: function(xml){
		   $j(xmlTopLevelTag,xml).each(function() {
				$j(xmlSecondLevelTag,this).each(function() {
					// Loop through each image, adding to multidimensional array
					imgFileName = 'images/clients/' + $j(xmlImgSrcTag,this).text();
					imgAltText = $j(xmlImgAltTag,this).text();
					// If it is the same as the initial image, don't add, we'll add it to the end later
					if (initialImg.indexOf(imgFileName) == -1) {
						imgArray.push(Array(imgFileName, imgAltText));

					} else {
						initialImgArray = Array(imgFileName, imgAltText);
					}
					// Preload Image
					if (document.images) {
						imgPreloadArray[preloadIndex] = new Image(175,75);
						imgPreloadArray[preloadIndex].src = imgFileName;
						preloadIndex++;
					}
				});
				// Add initial image to end of the array
				imgArray.push(initialImgArray);
		   });
		   	// Start first fade, after initial hold time
		    setTimeout(function() {
				fadeImageOut(theimg, imgIndex, imgArray)
			}, holdTime);
	   }
	 });
}

function switchImage(theimg, imgIndex, imgArray) {
	// Switch image, setting alt text
	theimg.src = imgArray[imgIndex][0];
	theimg.alt = imgArray[imgIndex][1];
	// If at end of count, restart, otherwise increment
	if (imgIndex == (imgArray.length - 1)) {
		imgIndex = 0;
	} else {
		imgIndex++;
	}
	// Image switched, fade it in
	fadeImageIn(theimg, imgIndex, imgArray);
}

function fadeImageOut(theimg, imgIndex, imgArray) {
	// Fail gracefully
	try {
		// Fade image until it is completely faded out
		// upon completion of fade - call switchImage()
		$j(theimg).fadeOut(interval, function() {
			switchImage(theimg, imgIndex, imgArray);
		});
	} catch(err) {
	}
}

function fadeImageIn(theimg, imgIndex, imgArray) {
	// Fade image in to 1.0 opacity, leave it there for holdTime, then fadeOut
	$j(theimg).fadeIn(interval, function() {
		setTimeout(function() {
			fadeImageOut(theimg, imgIndex, imgArray);
		}, holdTime)
	});
}