
// REQUIRES
// jQuery

var multifaderDebugFlag = 0;

var portraitImageArray = new Array(
	"images/header_faces_1.jpg",
	"images/header_faces_2.jpg",
	"images/header_faces_3.jpg",
	"images/header_faces_4.jpg",
	"images/header_faces_5.jpg",
	"images/header_faces_6.jpg",
	"images/header_faces_7.jpg",
	"images/header_faces_8.jpg"
);

function MultiFader(pDisplayElementClassName,pDisplayElementInteriorClassName,pPortraitImageArray) {
	var statusString = 'Object Created';
	var totalFaderCount = 0;
	var currentFaderIndex = 0;
	var currentImageIndex = 0;
	var faderIntervalLength = 2500;
	var fadeLength = 1250;
	var intervalId = -1;
	var isInitializedFlag = 0;
	var displayElementClassName = '';
	var displayElementInteriorClassName = '';
	var portraitImageArray = new Array();

	this.init = function init() {
		// Find elements matching display base
		faderContainerSelector = '.' + pDisplayElementClassName;
		var faderContainerCount = $(faderContainerSelector).length;
		var faderInteriorCount = $("." + pDisplayElementClassName + " ." + pDisplayElementInteriorClassName).length;

		// If there aren't FG and BG elements or a matching number of them, return
		if (faderContainerCount==0) { fail("Bad Container Count"); return; }
		if (faderInteriorCount==0) { fail("Bad Interior Count"); return; }
		if (faderContainerCount != faderInteriorCount) { fail("Element Count Mismatch -- container: " + faderContainerCount + " interior: " + faderInteriorCount); return; }
		if (pPortraitImageArray.length < 1) { fail("Empty image array"); return; }

		totalFaderCount = faderContainerCount;
		displayElementClassName = pDisplayElementClassName;
		displayElementInteriorClassName = pDisplayElementInteriorClassName;
		portraitImageArray = pPortraitImageArray;

		isInitializedFlag = 1;

		if (multifaderDebugFlag) alert("Initialized!");
	}

	function setIntervalFaderInterval(intervalLength) {
		faderIntervalLength = intervalLength;
	}

	this.run = function run() {
		// Check to see if there's already a running timer
		if (!isInitializedFlag) return;
		if (intervalId != -1) return;

		// Start the timer
		intervalId = window.setInterval( faderCallback,faderIntervalLength );
	}

	function stop() {
		if (intervalId != -1)
		{
			window.clearInterval(intervalId);
			intervalId = -1;
		}
	}

	function fail(failureMessage) {
		statusString = failureMessage;

		if (multifaderDebugFlag) alert(failureMessage);
	}

	function faderCallback() {
		// Get current background object
		var bgObject = $('.' + displayElementClassName)[currentFaderIndex];

		if (!bgObject)
		{
			stop();
			fail('No background object found at index: ' + currentFaderIndex);
		}

		// Get current foreground object
		var fgObject = $(bgObject).children(" ." + displayElementInteriorClassName)[0];

		if (!fgObject)
		{
			stop();
			fail('No foreground object found at index: ' + currentFaderIndex);
		}

		// Set the source of the foreground object to the background
		if ($(bgObject).css('background-image') != 'none')
		{
//			fgSrcString = String($(bgObject).css('background-image'));
//			fgSrcString = fgSrcString.substring(4,fgSrcString.length-1);
//			fgObject.src = '';
//			$(fgObject).attr('alt', fgSrcString );

			fgSrcIndex = currentImageIndex - totalFaderCount;
			if (fgSrcIndex<0) fgSrcIndex = portraitImageArray.length + fgSrcIndex;
			fgSrcString = portraitImageArray[fgSrcIndex];
			$(fgObject).attr('alt', fgSrcString );
			$(fgObject).attr('src', fgSrcString );

			// Set current foreground object to opaque
			$(fgObject).fadeIn(1, function() {
				// Change the background source of the background object
				$(bgObject).css('background-image','url('+portraitImageArray [currentImageIndex]+')');

				// Fade out the foreground object
				$(fgObject).fadeOut(fadeLength);					

				// Adjust indices
				currentFaderIndex++;
				if (currentFaderIndex == totalFaderCount)
					currentFaderIndex = 0;

				currentImageIndex++;
				if (currentImageIndex == portraitImageArray.length)
					currentImageIndex = 0;
			});
		}
		else
		{
			// Change the background source of the background object
			$(bgObject).css('background-image','url('+portraitImageArray [currentImageIndex]+')');

			// Fade out the foreground object
			$(fgObject).fadeOut(1000);					

			// Adjust indices
			currentFaderIndex++;
			if (currentFaderIndex == totalFaderCount)
				currentFaderIndex = 0;

			currentImageIndex++;
			if (currentImageIndex == portraitImageArray.length)
				currentImageIndex = 0;
		}
	}

	function isInitialized() {
		return isInitializedFlag;
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

$(document).ready(function() {
	var myFader = new MultiFader('faderContainer','faderInterior',portraitImageArray);
	myFader.init();
	myFader.run();
});
