/* - - - - - - - - - - - - - - - - - - - - 
S U P A H F A D E . J S
Date: 2007-06-26
Author: Fabio Ottaviani
Mail: fabio.ottaviani@gmail.com
Web: http://www.supah.it
- - - - - - - - - - - - - - - - - - - - */

// $(elementContainer).supahfade({speed, timeout, height});

(function() {
	jQuery.fn.supahfade = function(settings) {

		var fader = $(this);
		
		// default value
		var settings = jQuery.extend({
			speed: "normal",
			timeout: 2000,
			height: "auto"
        }, settings);
		
		var objs = fader.children();
		
		// making container
		fader.css({position:"relative", height:settings.height});
		var zInd = objs.size();
		objs.each(function(){
			$(this).css({position:"absolute", top:"0px", left:"0px", zIndex:zInd});
			zInd = zInd - 1;
		});
		
		// setting animation
		var num = 0;
		var start;
		var start2;
		var looppa;
		function loopThis(){
			if (num < objs.size()-1) {
				$(objs[num]).fadeOut(settings.speed);
				num++;
			} else {
				$(objs[0]).fadeIn(settings.speed,function(){
					$(objs).show();
				});
				num = 0;
			}
			clearInterval(start);
			looppa = setTimeout(loopThis,settings.timeout);
		}
		
		// starting animation
		start = setTimeout(loopThis,settings.timeout);
		
		// pause on mouseover
		$(objs).mouseover(function(){
			clearTimeout(start);
			clearTimeout(looppa);
			start = null;
			looppa = null
		});
		$(objs).mouseout(function(){
			start = setTimeout(loopThis,500);
		});

	};
})(jQuery);