(function($){
	
	$.fn.slider = function(options){
		
		var defaults = {
			speed : 1000,
			pause : 2000,
			transition : "fade"
		};
		
		options = $.extend(defaults, options);
		
		console.log("defaults.speed" + defaults.speed);
		console.log("options.speed" + options.speed);
		console.log("options.transition" + options.transition);
		
		if (options.pause <= options.speed) {
			options.pause = options.speed + 100;
		}
		
		return this.each(function(){
			console.log("init instance");
			
			var $this = $(this);
			
			$this.wrap('<div class="slider-wrap" />');
			
			if (options.transition === 'slide') {
				
				$this.css({
					'width': '9999px',
					'position': 'relative',
					'padding' : 0
				});
				
				$this.children().css({
					'float': 'left',
					'listStyle': 'none'
				});
				
				$this.parent().css({
					'width': $this.children().width(),
					'overflow':'hidden'
				});
				
				
				slide();
			}
			
			if (options.transition === 'fade') {
				$this.children().css({
					'width': $this.find('img').width(),
					'position' : 'absolute',
					'left' : 0,
					'top' : 0
				});
				
				var imgWidth = $this.find('img').width();
				
				$this.css({
					'width': $this.find('img').width(),
					'height': $this.find('img').height(),
					'position': 'relative',
					'padding' : 0,
					'margin' : 0
				});
				
				$this.find('img').css({
					'border' : 'none'
				});
				
				
				$this.parent().css({
					'width': $this.width(),
					'overflow': 'hidden'
				});
				
				for (var i = $this.children().length, y = 0; i > 0; i--, y++){
					$this.children().eq(y).css('zIndex', i + 99999);
				}
				
				fade();
			}
			
			function slide(){
				
				console.log("SLIDE");
				setInterval(
					function(){
						$this.animate(
							{'left': '-' + $this.parent().width()},
							options.speed,
							function(){
								$this.css("left", 0)
									.children(":first")
									.appendTo($this);
							}
						);
					}, 
					options.pause
				);
			}
			
			function fade(){
				setInterval(function(){
					console.log("fade");
					var firstElement = $this.children().first();
					firstElement.animate(
						{'opacity': 0}, 
						options.speed, 
						function(){
							var self = $(this);
							self.css(
								{
									'zIndex' : $this.children().last().css('zIndex') - 1,
									'opacity': 1
								}
							);
							$this.append(self);
						}
					);
				}, options.pause);
			}
			
			
			
		});
	};
	
}) (jQuery);
