/* =========================================================

// jquery.innerfade.js

// Datum: 2007-01-29
// Firma: Medienfreunde Hofmann & Baldes GbR
// Autor: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/

// ========================================================= */


(function($) {
$.fn.innerfade = function(options) {
	this.each(function(){ 	
		var settings = {
			animationtype: 'fade',
			speed: 'normal',
			timeout: 2000,
			type: 'sequence',
			containerheight: 'auto',
			runningclass: 'innerfade'
		};
		
		if(options)
			$.extend(settings, options);
		
		var e = $(this).children();
	
		if (e.length > 1) {
		
			$(this).css('position', 'relative');
			$(this).css('height', settings.containerheight);
			$(this).addClass(settings.runningclass);
			
			for ( var i = 0; i < e.length; i++ ) {
				$(e[i]).css('z-index', String(e.length-i)).css('position', 'absolute');
				$(e[i]).hide();
			};
			switch(settings.type){
				case 'sequence':	setTimeout(function(){ $.innerfade.next(e, settings, 1, 0)}, settings.timeout);
									$(e[0]).show();
									break;
				case 'random':	setTimeout(function(){
									do { current = Math.floor ( Math.random ( ) * ( e.length ) ); } while ( current == 0 )
									$.innerfade.next(e, settings, current, 0);
								}, settings.timeout);
								$(e[0]).show();
								break;
				default: alert('type must either be \'sequence\' or \'random\'');
			}
		}
	});
};


$.innerfade = function() {}
$.innerfade.next = function (e, settings, current, last) {
	switch(settings.animationtype){
		case 'slide':	//$(e[last]).slideUp(settings.speed, $(e[current]).slideDown(settings.speed));
						$(e[last]).slideUp(settings.speed, $(e[current]).fadeIn(settings.speed));
						break;
		case 'fade':	$(e[last]).fadeOut(settings.speed);
						$(e[current]).fadeIn(settings.speed);
						break;
		default:	alert('animationtype must either be \'slide\' or \'fade\'');
	};	
	switch(settings.type){
		case 'sequence': if ( ( current + 1 ) < e.length ) {
							current = current + 1;
							last = current - 1;
							} else {
								current = 0;
								last = e.length - 1;
							}; break;
		case 'random':	last = current;
						while (	current == last )
							current = Math.floor ( Math.random ( ) * ( e.length ) );
						break;
		default: alert('type must either be \'sequence\' or \'random\'');
	};
	setTimeout((function(){$.innerfade.next(e, settings, current, last);}), settings.timeout);
};
})(jQuery);
