(function($){
	$.fn.zmContentSlider = function(options) {
		
		var $this = $(this);
		var height = $(this).height();
		var width = $(this).width();
		
		var
		  defaults = {
			contentBg: '#fff',
			showNav: true,
			displayTime: 5000
		  },
		  settings = $.extend({}, defaults, options);
		  
		  $(this).addClass('Ziteman-ContentSlider');
		  $(this).after('<div class="Ziteman-ContentSlider-Nav"></div>');
		  
		  if(settings.showNav !== true) {
			$('.Ziteman-ContentSlider-Nav').hide();
		  }
		  
		  var count = 1;
		  var count2 = 1;
		  var count3 = 1;
		  
		  $this.children('div').first().addClass('active');
		  
		  
		  $($this.children('div').get().reverse()).each(function() {
			$(this).attr('rel', count3++ );
		  });
		  
		  $this.children().each(function() {
			$('<a href="#">'+ count2++ +'</a>').appendTo('.Ziteman-ContentSlider-Nav');
		  });
		  
		  $($('.Ziteman-ContentSlider-Nav').children('a').get().reverse()).each(function() {
			$(this).attr('rel', count++);
		  });
		  
		  $('.Ziteman-ContentSlider-Nav a').click(function() {
				var rel = $(this).attr('rel');
				if($('.Ziteman-ContentSlider .active').attr('rel') == rel) {
					return false;
				}
				else
				{
				$this.find('div.active').animate({opacity: 0.0}, 1000).removeClass('active');
				$this.find('div[rel="'+ rel +'"]').animate({opacity: 0.0}, 1).animate({opacity: 1.0}, 1000).addClass('active');
				
				var rel2 = $this.find('div.active').attr('rel');
				
				$('.Ziteman-ContentSlider-Nav a.active').removeClass('active');
				$('.Ziteman-ContentSlider-Nav').find('a[rel="'+ rel2 +'"]').addClass('active');
				
				return false;
				}
		  });
		  
		  $('.Ziteman-ContentSlider-Nav').find('a').first().addClass('active');
		  
		  $this.children('div').css({
		  'background': settings.contentBg,
		  'width': width,
		  'height': height
		  });
		  
		  function slideSwitch() {
				var $active = $this.find('div.active');

				if ( $active.length == 0 ) $active = $this.find('div:last');

				var $next =  $active.next().length ? $active.next()
				: $this.find('div:first');

				$active.addClass('last-active');
				$('.Ziteman-ContentSlider-Nav').find('a.active').removeClass('active');
				$next.css({opacity: 0.0})
				.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
				}).addClass('active');
				
				var rel = $active.next('div.active').attr('rel');
				
				if(typeof rel == 'undefined') {
					var rel = $this.children().length;
				}
				
				$('.Ziteman-ContentSlider-Nav').find('a[rel="'+ rel +'"]').addClass('active');
			
			}

			$(function() {
				setInterval(slideSwitch, settings.displayTime );
			});
		 
		  return this;
	}
})(jQuery);
