/*
 * jQuery diewebdesigner.com Slider 1.0
 * http://www.diewebdesigner.com
 *
 * Copyright 2011, Leo Gerber
 */
(function($){
	var pluginName = 'dwSlider';
	
	var settings = {
		pauseTime: 6000,
		fadeTime: 2000,
		zIndex: 10
	};
	
	var cSlide = 0;
	var nSlide = 0;
	var numSlides = 0;
	var timer = 0;
	
	var methods = {
		init: function(options) {
			return this.each(function() {
				var $this = $(this);
				if(options) {
					$.extend(settings, options);	
				}
				var data = $this.data(pluginName);
				
				if (!data) {
					//
					$(this).data(pluginName, {
						target: $this,
						settings: settings
					});
				}
				
				$this.addClass('dwSlider');
				var children = $this.children('img');
				numSlides = children.length;
				children.each(function() {
					var child = $(this);
					child.addClass('dwSlideImage');
					child.fadeOut(0);
					child.css({
						'display': 'none',
						'z-index': settings.zIndex,
						'position': 'absolute',
						'width': '100%'
					});
				});
				$(children[0]).fadeIn(0);
				$(children[0]).css('display','block');
				$(children[0]).css('z-index',settings.zIndex+2);
				
				if(numSlides > 1) {
					timer = setInterval(function() {
						if(cSlide < (numSlides - 1)) {
							nSlide = cSlide+1;
						} else {
							nSlide = 0;
						}
						
						// Show next slide below current
						$(children[nSlide]).css({
							'z-index': settings.zIndex+1,
							'display': 'block'
						}).fadeIn(0);
						
						$(children[cSlide]).fadeOut(settings.fadeTime, function() {
							$(children[cSlide]).css({
								'z-index': settings.zIndex,
								'display': 'none'
							});
							$(children[nSlide]).css('z-index', settings.zIndex+2);
							cSlide = nSlide;
						});
					}, settings.pauseTime);
				}
			});
		},
		destroy: function() {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data(pluginName);
				
				$(window).unbind('.'+pluginName);
				eval('data.'+pluginName+'.remove();');
				$this.removeData(pluginName);
			});
		}
	};
	
	$.fn.dwSlider = function(method) {
		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.'+pluginName);	
		}
	};
	
})(jQuery);
