
(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 elements = $(this).children();
	
		if (elements.length > 1) {
		
			$(this).css('position', 'relative');
	
			$(this).css('height', settings.containerheight);
			$(this).addClass(settings.runningclass);
			

			
			for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				$(elements[i]).hide();
			};
			timeout =$(elements[0]).attr('pause') ? $(elements[0]).attr('pause') : settings.speed;
			setTimeout(function(){
				$.innerfade.next(elements, settings, 1, 0);
			}, timeout);

			$(elements[0]).show();
			
		}
		
	});
};


$.innerfade = function() {}
$.innerfade.next = function (elements, settings, current, last) {
	
		if ($('#TB_overlay').length == 0){
			
			$(elements[last]).fadeOut(settings.timeout);
			$(elements[current]).fadeIn(settings.timeout);
			if ( ( current + 1 ) < elements.length ) {
				current = current + 1;
				last = current - 1;
			} else {
				current = 0;
				last = elements.length - 1;
			};
	

			timeout = $(elements[current]).attr('pause') ? $(elements[current]).attr('pause') : settings.speed;
			setTimeout((function(){	$.innerfade.next(elements, settings, current, last);	}), timeout);
			
		} else {
			
			timeout = $(elements[current]).attr('pause') ? $(elements[current]).attr('pause') : settings.speed;
			setTimeout((function(){	$.innerfade.next(elements, settings, current, last);	}), timeout);
		}
};
})(jQuery);