///////////////////////////////////////////////
//
//	Simple cross fader that doesn't expect all 
//	images to be the exact same size.
//	Adjusts container to size of 1st image and
//  crops the rest.
//
//
//	Released under the MIT license
//	http://www.adeointernetmarketing.com
//
///////////////////////////////////////////////

(function($){  
	$.fn.extend({   
		//plugin name - animatemenu  
		crossf4de: function(options) {  
  
			//Settings list and the default values  
			var defaults = {  
				fadeDuration: 2000,
				pauseDuration: 2000,
				descriptionDiv: null,
				loadingMessage: "Loading...",
				callBack: null,
			};  
			  
			var options = $.extend(defaults, options);  

			window.crossf4deItems = new Array();
			window.crossf4deContainer = null;
			return this.each(function() {  
				var o = options; 
				window.crossf4deCallback = o.callBack;
				window.crossf4deItems[window.crossf4deItems.length] = new Array(this.href, this.innerHTML);
				window.descriptionDiv = o.descriptionDiv;
				if (!window.crossf4deContainer)
				{
					window.crossf4deContainer = this.parentNode;
					if (window.crossf4deContainer.tagName.toUpperCase() != "DIV")
					{
						window.crossf4deContainer.innerHTML = "Crossf4de Fatal error. Container must be a div";
					}
					else
					{
						window.crossf4deContainer.innerHTML = o.loadingMessage;
						var img = new Image();
						img.onload = function() {
							window.crossf4deContainer.innerHTML = "<img id='crossf4deImage' style='display: none;' />";
							window.crossf4deContainer.style.overflow = "hidden";
							//window.crossf4deContainer.style.display = "block"; //if hidden by default to avoid the flicker
							window.crossf4deContainer.style.backgroundRepeat = "no-repeat";
							loadNextcrossf4de(o.pauseDuration, o.fadeDuration);
						}
						img.src = window.crossf4deItems[0][0];
					}
				} 
			});  
		}  
	});  
})(jQuery); 

/* All this is global. Besides being ugly code, it only allows one crossf4ader per page.
I know it's bad but not sure how to make it work w/ JQuery object. So for now, like this it will stay. */
var crossf4deContainer;
var crossf4deItems;
var crossf4deIndex = -1;
var descriptionDiv;
var crossf4deCallback;
var imgWidth;
var imgHeight;

function loadNextcrossf4de(pauseDuration, fadeDuration)
{
	var crossf4deImage = document.getElementById("crossf4deImage");
	crossf4deIndex++;
	if (crossf4deIndex >= crossf4deItems.length)
	{
		crossf4deIndex = 0;
	}
	crossf4deContainer.style.backgroundImage = "url('" + crossf4deItems[crossf4deIndex][0] + "')";
	$(crossf4deImage).fadeOut(fadeDuration, function() {
		crossf4deImage.src = crossf4deItems[crossf4deIndex][0];
		crossf4deImage.style.display = "block";
		if (descriptionDiv)
		{
			descriptionDiv.innerHTML = $('<div/>').html(crossf4deItems[crossf4deIndex][1]).text();
		}
		if (crossf4deCallback)
		{
			crossf4deCallback(crossf4deItems, crossf4deIndex);
		}
		setTimeout("loadNextcrossf4de(" + pauseDuration + ", " + fadeDuration + ");", pauseDuration);
	});

}

