// simpldeFade
// based on innerfade
(function($) {
	
	$.fn.simpleFade = function(settings, args){

		if(typeof(settings) == 'string' && $.fn.simpleFade.settings[settings]){
			$.fn.simpleFade.settings[settings] = args;
			return false;
		}
		
		// get the settings merged with defaults
		$.fn.simpleFade.settings = $.extend({}, $.fn.simpleFade.defaults, settings);
		settings = $.fn.simpleFade.settings;

		// return the container jQuery Object
		return this.each(function(){
			
			// setup the css positioning for container
			$(this).css('position','relative').css('height', settings.containerheight).addClass(settings.runningclass);
			// position and index of the elements
			$(this).children().each(function(){
				$(this).css('z-index', $(this).index()).css('position','absolute').hide();
			});

			$(this).children(':first-child').show();
			c = $(this);

			setTimeout(function() {
				$.fn.simpleFade.next(c);
            }, settings.timeout);
		});
	};

	$.fn.simpleFade.next = function(context){
		settings = $.fn.simpleFade.settings;
			
		// first make sure there are slides
		if(context.children().length){
			// get and hide the current slide
			$last = context.children(':visible').fadeOut(settings.speed, settings.afterhide);

			// we're at the end, go back to the begining
			if(context.children(':visible').index() >= context.children().length - 1){						
				context.children(':first-child').fadeIn(settings.speed);					
			// go forward one
			} else {
				$last.next().fadeIn(settings.speed);
			}
		}

		setTimeout(function() {
			$.fn.simpleFade.next(context);
		}, settings.timeout);

	};

	$.fn.simpleFade.defaults = {
		'animationtype':    'fade',
		'speed':            'normal',
		'type':             'sequence',
		'timeout':          2000,
		'containerheight':  'auto',
		'runningclass':     'simpleFade',
		'children':         null,
		'afterhide':		null
	};

})(jQuery);
