// Slideshow jQuery extension
// Jason D Brown
// v1.0.1
// 26-May-2010
;(function($) {

$.fn.extend({
	slideShow : function(settings) {
		defaults = {
			'speed' : 400,
			'delay' : 4000,
			'random' : false,
			'effect' : 'fade',
			'optionsout' : {'direction':'left'},
			'optionsin' : {'direction':'right'}
			};
		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 opts = this.data('slideShowSettings');
		if(opts['random']) {
			while(switchto == this.data('slideShowIndex')) {
				switchto = Math.floor(Math.random() * list.length);
			}
		} else {
			switchto = (switchto + 1) % list.length;
		}
		if($.fn.effect) {
			$(list[this.data('slideShowIndex')]).hide(opts.effect, opts.optionsout, opts.speed);
			$(list[switchto]).show(opts.effect, opts.optionsin, opts.speed);
		} else {
			$(list[this.data('slideShowIndex')]).fadeOut(opts.speed);
			$(list[switchto]).fadeIn(opts.speed);
		}
		this.data('slideShowIndex', switchto);
		var it = this;
		setTimeout(function() {
			it.slideShowSwitch();
		}, it.data('slideShowSettings')['delay']);
	}
});

})(jQuery);
