/**
 * Mimetize 0.1.5.1 - jQuery simple slideshow widget
 */

/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <choan.galvez@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return 
 * ----------------------------------------------------------------------------
 */


/**
 * Make the selected elements Mimetize widgets (simple slideshows with fadeIn/fadeOut transitions).
 * 
 * @param Array col Array of image paths
 * @param Map o Configuration options
 * @option Number firstTimeOut Pause before the first animation, default is 5000 ms
 * @option Number timeOut Pause between frames, default is 5000 ms
 * @option Number fadeIn Speed of the fadeIn effect for the appearing image. Default is FALSE (no effect)
 * @options Number fadeOut Speed of the fadeOut effect for the disappearing image. Default is 1500 ms
 * @type jQuery
 * @name mimetize
 */

(function($) {


function play(cont, config) {
	var te = (new Date()).getTime();
	// ensure all images are loaded (or failed) before starting the slideshow
	cont._mimetizeInterval = setTimeout(function() {
		next(cont, config);
	}, config.firstTimeOut - (te - cont._ts));
	
}

function next(cont, config) {
	clearTimeout(cont._mimetizeInterval);
	var current = $(cont).find("img:visible");
	var nexti = current.next("img").eq(0);
	if (!nexti.length) {
		nexti = $(cont).find("img:eq(0)");
	}
	
	if (!nexti[0]._loaded) {
		cont._mimetizeInterval = setTimeout(function() {
			next(cont, config);
		}, 50);
		return;
	}

	nexti.css("z-index", "");
	current.css("z-index", "1000");
	if (config.fadeIn) {
		nexti.fadeIn(config.fadeIn);
	} else {
		nexti.css("display", "");
	}
	current.fadeOut(config.fadeOut);
	cont._mimetizeInterval = setTimeout(function() {
		next(cont, config);
	}, config.timeOut);	
}

function preloadImages(cont, col) {
	cont._ts = (new Date()).getTime();
	var cssimg = {
		display: "none",
		position: "absolute"
	};
	var first = $(cont).find("img")[0];
	first._loaded = true;
	var imgs = [], img;
	var c = col.length;
	// alert(c);
	cont._imgsToLoad = c;
	for (var i = 0, img; i < c; i++) {
		// be nice to Safari, do not use `new Image()`
		imgs[i] = img = document.createElement("img");
		$(img)
			.css(cssimg)
			// use counters of images loaded or failed
			.bind("load", function() {
				this._loaded = true;
			})
			.bind("error", function() {
				this.parentNode.removeChild(this);
			});
		$(i == 0 ? first : imgs[i - 1]).after(img);
		img.src = col[i];
	}	
}


$.fn.mimetize = function(col, o) {

	var config = {
		
		timeOut : 5000,
		fadeIn : false,
		fadeOut : 1500
		
	};


	config = $.extend(config, o);
	
	if (config.firstTimeOut === window.undefined)
		config.firstTimeOut = config.timeOut;
	
	this.each(function() {
		preloadImages(this, col);
		play(this, config);
	});		
	
	
	return this;


};

})(jQuery);