$.fn.carousel = function(){

	var calcCarouselImg = function(container, position,min_size_multiplicator,itemMinWidth){
		var items = $('img',container);
		
		var height = container.height();
		var width = container.width();
		var origin = [width/2,height/2];
		var radius = height/2;
		var xModifier = height/width;
		//Der Radiant als Abstand zwischen den Elementen
		var distance = 2*Math.PI/items.length;
		
		//Initiale Positionierung der Elemente
		for(var i=0;i<items.length;i++){
			var x = origin[0]+Math.cos(i*distance+position)*radius/xModifier;
			var y = origin[1]-Math.sin(i*distance+position)*radius;
			var newWidth = itemMinWidth+((itemMinWidth/min_size_multiplicator-itemMinWidth)*(1/height*y));
			//Styles setyen (außer positionierung, dafür braucht man erst die breite)
			var newHeight = $(items[i])
				.css('z-index',Math.floor(y))
				.css('opacity',1/height*y<0.5?0.5:1/height*y)
				.css('width',newWidth)
				.css('margin-left',-newWidth/2+'px')
				.height()
			;			
			//Damit Bilder nicht unten überstehen
			//Damit Bilder nicht seitlich überstehen
			$(items[i]).css('top',y - newHeight*(1/height*y)+'px')
				.css('left',x - newWidth*((1/width*x)-0.5)+'px')
			;
		}
	};
	
	$(this).each(function(){
		//PARAMETER
		var MAX_TURN_SPEED = 0.1;
		var that = $(this);
		var rotation = -Math.PI/2

		var itemMinWidth = $('img',that).css('min-width');
		itemMinWidth = itemMinWidth.substring(0,itemMinWidth.indexOf('px'))*1;
		var itemMaxWidth = $('img',that).css('max-width');
		itemMaxWidth = itemMaxWidth.substring(0,itemMaxWidth.indexOf('px'))*1;
		var MIN_SIZE_MULTIPLICATOR = itemMinWidth/itemMaxWidth;
		
		//Bilderposition initialisieren
		calcCarouselImg($(this),rotation,MIN_SIZE_MULTIPLICATOR,itemMinWidth);
		
		//Closures für die Animation
		var offsetLeft = that.offset().left;
		var currentSpeed = 0;
		var width = that.width();
		var interval;
		//Handler um die Geschwindigkeit zu ändern
		$(this).bind('mousemove',function(e){
				currentSpeed = (1/(width/2) * (width/2 - (e.pageX - offsetLeft))) * MAX_TURN_SPEED;
			})
			.hover(function(){
				interval = window.setInterval(function(){
					rotation += currentSpeed;
					calcCarouselImg(that,rotation,MIN_SIZE_MULTIPLICATOR,itemMinWidth);
				},50);
			},function(){
				window.clearInterval(interval);
			})
		;
	});
	
	return this;
}
