(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, prev){
				before(index, prev);
			}
			function before(index){
				if(index < 0){
					if(o.continues){
						$this
							.find(o.ul)
							.stop()
							.css('left',(-width * slides) +'px');
					}
					
					index = slides - 1;
				}
				
				if(o.beforeSlide)
					o.beforeSlide($this, current + 1, index + 1);
					
				move(index, prev);
			}
			function move(index, prev){
				if(o.continues && !prev){
					if(current > index){
						index = slides + index;
					}
				}
				
				$this
					.find(o.ul)
					.stop()
					.animate({'left':(-width * index) +'px'}, o.speed, 'swing', function(){
						loop();
							
						after(index);
					});
			}
			function after(index){
				if(index > slides - 1){
					index = index - slides;
					
					$this
						.find(o.ul)
						.css('left',(-width * index)+'px');
				}
					
				current = index;
				
				$('.contentSliderNumbers a')
					.removeClass('contentSliderCurrent')
					.eq(index)
					.addClass('contentSliderCurrent');
				
				if(o.afterSlide)
					o.afterSlide($this, current + 1, index + 1);
			}
			
			function prev(){
				goTo(current - 1, prev);
			}
			function next(){
				goTo(current + 1);
			}
			function buttonPrev(){
				pauze();
				
				prev();
			}
			function buttonNext(){
				pauze();
				
				next();
			}
			function button(index){
				pauze();
				
				goTo(index);
			}
			function playpauze(){
				if(loping)
					pauze();
				else
					play();
			}
			function play(){
				$this.find('.contentSliderPauze').removeClass('contentSliderPlay');
				
				if(loping != true){
					loping = true;
					next();
				}
			}
			function pauze(){
				$this.find('.contentSliderPauze').addClass('contentSliderPlay');
				
				loping = false;
				clearTimeout(timeout);
			}
			
			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).innerWidth() > width)
							width = $(this).innerWidth();
						if($(this).innerHeight() > height)
							height = $(this).innerHeight();
						
						slides++;
					})
					.end()
				.css({'width':(o.continues?(width * slides * 2):(width * slides)), 'height':height});
				
				$('#knoppenbalk').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');
											})));
							}
						}));
			
			if(o.continues){
				$this.find(o.ul).find('> li').clone().appendTo($this.find(o.ul));
			}
			
			loop();
			
			if(o.prevButton)
				$(o.prevButton).click(buttonPrev);
			if(o.nextButton)
				$(o.nextButton).click(buttonNext);
		});
	};

	$.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,
		nextButton:false,
		prevButton:false,
		continues:false,
		pause:3,
		speed:2000
	};
})(jQuery);
