// 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'},
			'altspeed' : 200,
			'altdelay' : 8000
			};
		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();
			var indic = $('<div class="indicate2"></div>').appendTo($('<div class="indicate"></div>').appendTo(it));
			for(var x = 0; x < list.length; x++) {
				indic.append('<div class="indic"></div>');
			}
			$('.indicate2',this).children('.indic').eq(first).addClass('active').siblings().removeClass('active');
			$(this).data('slideShowTimeout',
				setTimeout(function() {
					it.slideShowSwitch();
				}, defaults['delay'])
			);
			it.append('<span class="slideshow-prev"></span><span class="slideshow-next"></span>');
			it.data('slideShowNext', $('.slideshow-next', it).click(
				function(){
					clearTimeout(it.data('slideShowTimeout'));
					it.slideShowSwitch((it.data('slideShowIndex') + 1) % it.data('slideShowElements').length);
				}));
			it.data('slideShowPrev', $('.slideshow-prev', it).click(
				function(){
					clearTimeout(it.data('slideShowTimeout'));
					it.slideShowSwitch((it.data('slideShowIndex') + it.data('slideShowElements').length - 1) % it.data('slideShowElements').length);
				}));
		});
	},
	slideShowSwitch : function(override) {
		var switchto = this.data('slideShowIndex');
		var list = this.data('slideShowElements');
		var opts = this.data('slideShowSettings');
		if(override != undefined) {
			switchto = override;
		} else if(opts['random']) {
			while(switchto == this.data('slideShowIndex')) {
				switchto = Math.floor(Math.random() * list.length);
			}
		} else {
			switchto = (switchto + 1) % list.length;
		}
		if($.fn.effect && opts.effect != 'fade') {
			$(list[this.data('slideShowIndex')]).hide(opts.effect, opts.optionsout, (switchto == override) ? opts.altspeed : opts.speed);
			$(list[switchto]).show(opts.effect, opts.optionsin, (switchto == override) ? opts.altspeed : opts.speed);
		} else {
			$(list[this.data('slideShowIndex')]).fadeOut((switchto == override) ? opts.altspeed : opts.speed);
			$(list[switchto]).fadeIn((switchto == override) ? opts.altspeed : opts.speed);
		}
		$('.indicate2',this).children('.indic').eq(switchto).addClass('active').siblings().removeClass('active');
		this.data('slideShowIndex', switchto);
		var it = this;
		this.data('slideShowTimeout', 
			setTimeout(function() {
				it.slideShowSwitch();
			}, it.data('slideShowSettings')[(switchto == override) ? 'altdelay' : 'delay'])
		);
	}
});

})(jQuery);
