/**************************************************************************
* Simple Javascript Slideshow
*
* Changes slides out (crossfade)
* Scott Wespi (scott@darkstardesign.com)
*
* Requires jQuery
**************************************************************************/
(function($){
	/* feel free to change these, but it may be easier to over-write them with options like so:
		$('#slider-container').dsdShow({effectDuration:1, interval:3});
	*/
	var settings = {
		interval: 6, // time (in seconds) between transitions - should be longer than effectDuration
		effectDuration: .5, // total time (in seconds) transitioning
		buttons: false,	// the button container
		autoStart: true // true if you want the slider to start right away, false if you want to wait
	};
	
	var data = {
		container: false,
		totalSlides: 0,
		index: 0,
		lastIndex: 0,
		timer: -1,
		moving: false
	};
	
	var methods = {
		init: function(options) {
			return this.each(function() {        
				// merge options and settings
				if (options) { 
					$.extend(settings, options);
				}
				
				var $this = $(this);
				
				data.totalSlides = $this.children('.slide').length; 
				data.index = 0; 
				data.lastIndex = 0; 
				data.timer = -1; 
				data.moving = false;
				data.container = $this;
				
				if (data.totalSlides > 1) {
					// hide everything besides the current slide
					$this.children('.slide').hide();
					$this.children('.slide:eq('+data.index+')').show();
				}
				
				if (settings.buttons) {
					settings.buttons.children('li').removeClass('active');
					settings.buttons.children('li:eq('+data.index+')').addClass('active');
					
					$.each(settings.buttons.children('li'), function(i, s) {
						s.onclick = function() { methods.gotoSlide(i); };
					});
				}
			
				if (settings.autoStart) {
					data.timer = window.setInterval(methods.nextSlide, settings.interval * 1000);
				}
			});
		},
		nextSlide: function() {
			if (!data.moving) {
				data.moving = true;
				data.lastIndex = data.index;
				data.index = (data.index + 1) % data.totalSlides;
				
				methods.cycleSlide();
			} else {
				window.clearInterval(data.timer);
			}
		},
		cycleSlide: function() {
			var container = data.container;
			if (settings.buttons) {
				settings.buttons.children('li:eq('+data.lastIndex+')').removeClass('active');
				settings.buttons.children('li:eq('+data.index+')').addClass('active');
			}
			if (/MSIE\s9/.test(navigator.userAgent) || /MSIE\s8/.test(navigator.userAgent) || /MSIE\s7/.test(navigator.userAgent)) {
				// fade doesn't work well in IE
				container.children('.slide:eq('+data.lastIndex+')').hide();
				container.children('.slide:eq('+data.index+')').show();
				data.moving = false;
			} else {			
				container.children('.slide:eq('+data.lastIndex+')').fadeOut(settings.effectDuration * 1000);
				container.children('.slide:eq('+data.index+')').fadeIn(settings.effectDuration * 1000, function() {
					data.moving = false;
				});
			}
		},
		gotoSlide: function(index) {
			window.clearInterval(data.timer);
			if (!data.moving) {
				data.moving = true;
				if (index != data.index) {
					data.lastIndex = data.index;
					data.index = index;
					methods.cycleSlide();
					return true;
				}
				data.moving = false;
			}
			return false;
		}
	};

	$.fn.dsdShow = function(method) {
		// generic method calling logic
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.dsdShow' );
		}
	};
})(jQuery);
