///////////////////////////////////////////////
//
//	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..."
			};  
			  
			var options = $.extend(defaults, options);  

			window.crossf4deItems = new Array(this.length);
			window.crossf4deContainer = null;
			return this.each(function(index) {  
				var o = options; 
				window.crossf4deItems[index] = new Array(this.href, this.innerHTML);
				window.descriptionDiv = o.descriptionDiv;

				if (!window.crossf4deContainer)
				{
					window.crossf4deContainer = this.parentNode;
					window.crossf4deContainer.style.display = "none";
				}
				if (window.crossf4deContainer.tagName.toUpperCase() != "DIV")
				{
					window.crossf4deContainer.innerHTML = "Crossf4de Fatal error. Container must be a div";
				}
				else if (index == (window.crossf4deItems.length - 1))
				{
					var img = new Image();
					img.onload = function() {
						window.crossf4deContainer.style.display = "";
						window.crossf4deContainer.innerHTML = "<img id='crossf4deImage' style='display: none;' />";
						window.crossf4deContainer.style.width = img.width + "px";
						window.crossf4deContainer.style.height = img.height + "px";
						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); 

var crossf4deContainer;
var crossf4deItems;
var crossf4deIndex = -1;
var descriptionDiv;

function loadNextcrossf4de(pauseDuration, fadeDuration)
{
	var crossf4deImage = document.getElementById("crossf4deImage");
	crossf4deIndex++;
	if (crossf4deIndex >= crossf4deItems.length)
	{
		crossf4deIndex = 0;
	}
	crossf4deContainer.style.backgroundImage = "url('" + crossf4deItems[crossf4deIndex][0] + "')";
	var img = new Image();
	img.onload = function() {
		$(crossf4deImage).fadeOut(fadeDuration, function() {
			crossf4deImage.src = crossf4deItems[crossf4deIndex][0];
			crossf4deImage.style.display = "block";
			if (descriptionDiv)
			{
				if (crossf4deItems[crossf4deIndex][1] != "")
				{
					descriptionDiv.innerHTML = $('<div/>').html(crossf4deItems[crossf4deIndex][1]).text();
					descriptionDiv.style.display = "block";
				}
			}
			setTimeout("loadNextcrossf4de(" + pauseDuration + ", " + fadeDuration + ");", pauseDuration);
		});
	}
	img.src = crossf4deItems[crossf4deIndex][0];
}

