(function($){
	$.fn.contentSlider = function(options){
		var o = $.extend({}, $.fn.contentSlider.defaults, options);
		
		return $(this).each(function(){
			function loop(){
				if(loping)
					timeout = setTimeout(next, o.pause * 1000);
			}
			function goTo(index){
				if(o.before)
					o.before($this, current + 1, index + 1);
				
				$this
					.find(o.ul)
					.stop()
					.animate({'left':(width * index * -1) +'px'}, o.speed, 'swing', function(){
						loop();
						current = index;
						
						$this
							.find('.contentSliderNumbers a')
							.removeClass('contentSliderCurrent')
							.eq(index)
							.addClass('contentSliderCurrent');
						
						if(o.after)
							o.after($this, current + 1, index + 1);
					});
			}
			function next(){
				if((current + 1) == slides)
					goTo(0);
				else
					goTo(current + 1);
			}
			function button(index){
				loping = false;
				clearTimeout(timeout);
				
				goTo(index);
			}
			
			var $this = $(this);
			var loping = true;
			var current = 0;
			var slides = 0;
			var timeout = false;
			
			var width = 0;
			var height = 0;
			
			$this
				.addClass('contentSlider')
				.find(o.ul)
					.addClass('contentSliderUl')
					.find('> li')
					.each(function(){
						if($(this).width() > width)
							width = $(this).width();
						if($(this).height() > height)
							height = $(this).height();
						
						slides++;
					})
					.css({'width':width +'px', 'height':height +'px'})
					.end()
				.css({'width':(width * slides), 'height':height})
				.end()
				.append($(document.createElement('div'))
					.addClass('contentSliderNumbers')
					.append($(document.createElement('ul'))
						.each(function(){
							for(var i=0; i<slides; i++){
								$(this)
									.append($(document.createElement('li'))
										.append($(document.createElement('a'))
											.attr('href','javascript:void(0);')
											.click(function(){
												button($(this).parent().index());
											})
											.text(i + 1)
											.each(function(){
												if(current == i)
													$(this).addClass('contentSliderCurrent');
											})));
							}
						})));
			
			loop();
		});
	};

	$.fn.contentSliderGoTo = function(index){
		return $(this).each(function(){
			$(this).find('.contentSliderNumbers a:eq('+ (index - 1) +')').click();
		});
	};
	$.fn.contentSlider.defaults = {
		ul:'> ul:eq(0)',
		beforeSlide:false,
		afterSlide:false,
		pause:3,
		speed:2000
	};
})(jQuery);
