//jQuery(function ($) {

jQuery(document).ready(function($){
		
		$.fn.mdSlider = function(options) {
			var defaults = {
				speed : 600,
				pause : 500,
				transition : 'slide'
			},
			
			options = $.extend(defaults, options);
			
			this.each(function() {
				
				var $this = $(this);
				var $children = $this.children();
				
				$this.wrap('<div class="slider-wrap" />');
				
				if(options.pause <= options.speed) { options.pause = options.speed + 100; }
				
				$this.css({
					/*'width' : '99999px',*/
					'width' : '773px',
					'position' : 'relative',
					'padding' : 0
				});
				
				if(options.transition === 'slide') {
					$this.children().css({
						'float' : 'left',
						'list-style' : 'none'
					});
					
					$('.slider-wrap').css({
						'width' : $this.children().width(),
						'overflow' : 'hidden'
					});
				}
				
				if(options.transition === 'fade') {
					$this.children().css({
						'width' : $this.children().width(),
						/*'width' : $this.parent().width(),*/
						'position' : 'absolute',
						'left' : 0
					});
					
					for(var i = $this.children().length - 1, y = 0; i >= 0; i--, y++){
						//console.log('i: ' + i + ', y: ' + y);
						$this.children().eq(y).css('zIndex', i + 99999);
					}

					fade();

				} // end fade if
				
				if(options.transition === 'slide') { slide(); }
				
				function fade() {
					/*setInterval(function(){
							$this.children(':first').animate({ 'opacity' : 0}, options.speed, function(){
								$this
									.children(':first')
									.css('opacity', 1)
									.css('zIndex', $this.children(':last').css('zIndex') - 1)
									.appendTo($this);
							})								 
					}, options.pause);*/
					
					var intval = setInterval(function(){
							$this.children(':first').animate({ 'opacity' : 0}, options.speed, function(){
								$this
									.children(':first')
									.css('opacity', 1)
									.css('zIndex', $this.children(':last').css('zIndex') - 1)
									.appendTo($this);
								
								var currentZindex = $this.children(':last').css('zIndex');
								if(currentZindex <= 99999 - $this.children().length){
									$this.children(':last').css('zIndex', 999999)
									clearInterval(intval);
								}
																
							})								 
					}, options.pause);
					
					//alert(currentZindex);
					
				} // end fade
				
				
				
				/*function fade() {
					for(var j=0; j <= $this.children().length; j++){
						setInterval(function(){
								$this.children(':first').animate({ 'opacity' : 0}, options.speed, function(){
									$this
										.children(':first')
										.css('opacity', 1)
										.css('zIndex', $this.children(':last').css('zIndex') - 1)
										.appendTo($this);
								})								 
						}, options.pause);
					}
				} // end fade*/
				
				function slide() {
					setInterval(function(){
							$this.animate({ 'left' : '-' + $this.parent().width() }, options.speed, function(){
								$this
									.css('left', 0)
									.children(':first')
									.appendTo($this);
							})								 
					}, options.pause);
				} // end slide
				
			});
		}
		
});

jQuery(document).ready(function($){
	jQuery('.mdSlider').mdSlider({
			speed : 800,
			pause : 1000,
			transition : 'fade'
	});
});