;(function($) {

$.fn.extend({
	slideShow : function(settings) {
		defaults = {
			'speed' : 400,
			'delay' : 4000,
			'random' : false
			};
		if(!settings) settings = defaults;
		$.extend(defaults, settings);
		return this.each(function() {
			var it = $(this);
			var list = $(this).children();
			if(list.length <= 1) {return;}
			list.hide();
			$(this).data('slideShowSettings', defaults);
			$(this).data('slideShowElements', list);
			var first = Math.floor(Math.random() * list.length);
			$(this).data('slideShowIndex', first);
			$(list[first]).show();
			setTimeout(function() {
				it.slideShowSwitch();
			}, defaults['delay']);
		});
	},
	slideShowSwitch : function() {
		var switchto = this.data('slideShowIndex');
		var list = this.data('slideShowElements');
		var settings = this.data('slideShowSettings');
		if(settings['random']) {
			while(switchto == this.data('slideShowIndex')) {
				switchto = Math.floor(Math.random() * list.length);
			}
		} else {
			switchto = (switchto + 1) % list.length;
		}
		$(list[this.data('slideShowIndex')]).fadeOut(settings['speed']);
		$(list[switchto]).fadeIn(settings['speed']);
		this.data('slideShowIndex', switchto);
		var it = this;
		setTimeout(function() {
			it.slideShowSwitch();
		}, settings['delay']);
	}
});

})(jQuery);
