(function($) {

jQuery.fx.step.zIndex = function(fx) {
    fx.unit = "";
    fx.now = Math.floor( fx.now );

    jQuery.fx.step._default( fx );
};
	
var methods = {
	moveLeft: function() {
		if (animating) return false;
		animating = true;
		
		if (carouselItems[0] != "empty") {
		
			cItem = $(carouselItems[0]);
	
			cItem.animate({
				width: 329,
				height: 234,
				left: 256,
				top: 80
			}, {
				duration: 500,
				complete: function() {
					animating=false;
				} 
			});
			
			cItem.animate({
				zIndex: 5
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	queue: false		 	
			});		
						
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).animate({
				 		width: 329,
				 		height: 234
				 	}, 500);
				 } else {
					$(item).animate({
						width: 313,
						height: 193,
						top: 19,
						left: 9
					}, 500);
				 }
			});
		}		
	
		carouselItems.push(carouselItems.shift());
		
		if (carouselItems[0] != "empty") {		
			cItem = $(carouselItems[0]);
					
			cItem.css('display', 'block');
			
			cItem.animate({
				width: 439,
				height: 312,
				left: 0,
				top: 60
			}, 500);
			
			cItem.animate({
				zIndex: 10
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() { animating=false },			 	
			 	queue: false		 	
			});		
					
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {			 	
				 	$(item).animate({
				 		width: 439,
				 		height: 312
				 	}, 500);
				 } else {		
					$(item).animate({
						width: 418,
						height: 257,
						top: 25,
						left: 11
					}, 500);
				 }
			});			
		}
		
		if (carouselItems[1] != "empty") {			
			cItem = $(carouselItems[1]);
					
			cItem.animate({
					width: 585,
					height: 416,
					left: 185,
					top: 0
				}, 500);
				
			cItem.animate({
				zIndex: 1000
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() { animating=false },
			 	queue: false
			});	
			
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).animate({
				 		width: 585,
				 		height: 416
				 	}, 500);
				 } else {
					$(item).animate({
						width: 556,
						height: 342,
						top: 33,
						left: 15
					}, 500);
				 }
			});
		}
			
		if (carouselItems[2] != "empty") {						
			cItem = $(carouselItems[2]);
				
			cItem.css({display: 'block'});
				
			cItem.animate({
				width: 439,
				height: 312,
				left: 512,
				top: 60			
			}, 500);
			
			cItem.animate({
				zIndex: 10
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() { animating=false },
			 	queue: false		 	
			});			
				
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
			 	 	$(item).css({
			 	 		width: 329,
			 	 		height: 234
			 	 	});
			 	 	
			 	 	$(item).animate({
			 	 		width: 439,
			 	 		height: 312
			 	 	}, 500);
			 	 } else {
			 	 
			 	 	$(item).css({
			 	 		width: 313,
			 	 		height: 193,
			 	 		top: 19,
			 	 		left: 9
			 	 	});
			 
			 		$(item).animate({
			 			width: 418,
			 			height: 257,
			 			top: 25,
			 			left: 11
			 		}, 500);
			 	 }
			});
		}			
		
		return false;			
	},
	moveRight: function() {
		if (animating) return false;
		animating = true;
		
		carouselItems.unshift(carouselItems.pop());
		
		if (carouselItems[1] != "empty") {
			cItem = $(carouselItems[1]);
					
			cItem.animate({
					width: 585,
					height: 416,
					left: 185,
					top: 0
				}, 500);
				
			cItem.animate({
				zIndex: 1000
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() {animating=false },			 	
			 	queue: false
			});		
			
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).animate({
				 		width: 585,
				 		height: 416
				 	}, 500);
				 } else {
					$(item).animate({
						width: 556,
						height: 342,
						top: 33,
						left: 15
					}, 500);
				 }
			});
		}
		
		if (carouselItems[2] != "empty") {
			cItem = $(carouselItems[2]);
				
			cItem.animate({
				width: 439,
				height: 312,
				left: 512,
				top: 60
			}, 500);
			
			cItem.animate({
				zIndex: 10
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() {animating=false },
			 	queue: false		 	
			});
				
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).animate({
				 		width: 439,
				 		height: 312
				 	}, 500);
				 } else {
					$(item).animate({
						width: 418,
						height: 257,
						top: 25,
						left: 11
					}, 500);
				 }
			});
		}
		
		if (carouselItems[3] != "empty") {
			cItem = $(carouselItems[3]);
	
			cItem.animate({
				width: 329,
				height: 234,
				left: 256,
				top: 80
			}, {duration: 500, complete: function() { animating=false} } );
			
			cItem.animate({
				zIndex: 5
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() {animating=false },
			 	queue: false		 	
			});			
					
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).animate({
				 		width: 329,
				 		height: 234
				 	}, 500);
				 } else {
					$(item).animate({
						width: 313,
						height: 193,
						top: 19,
						left: 9
					}, 500);
				 }
			});
		}
		
		if (carouselItems[0] != "empty") {
			cItem = $(carouselItems[0]);
					
			cItem.css('display', 'block');
			
			cItem.animate({
				width: 439,
				height: 312,
				left: 0,
				top: 60
			}, 500);
			
			cItem.animate({
				zIndex: 10
			}, {
				duration: 500,
			 	step: function(num, fx) {
			 		$(this).css({
			 			zIndex: Math.round(num)
			 		});
			 	},
			 	complete: function() {animating=false },
			 	queue: false		 	
			});				
			
			cItem.find('img').each(function(index, item) {
				 if($(item).is('.carousel-background')) {
				 	$(item).css({
				 		width: 329,
				 		height: 234
				 	});
				 	
				 	$(item).animate({
				 		width: 439,
				 		height: 312
				 	}, 500);
				 } else {
				 
				 	$(item).css({
				 		width: 313,
				 		height: 193,
				 		top: 19,
				 		left: 9
				 	});
			
					$(item).animate({
						width: 418,
						height: 257,
						top: 25,
						left: 11
					}, 500);
				 }
			});		
		}
		return false;			
	}
}
	
	$.fn.carousel = function() {
			
		animating = false;
		carouselItems = [];			
									
		$(this).find('.carousel-item').each(function(index, item) {
			carouselItems.unshift(item);
		});

		var i1 = carouselItems.pop();
		carouselItems.unshift(carouselItems.pop());
		carouselItems.unshift(i1);
		carouselItems.unshift(carouselItems.pop());
		
								
		if (carouselItems.length > 1) {
		
			leftButton = $("<a href='#' id='carousel-left'>Links</a>");
			rightButton = $("<a href='#' id='carousel-right'>Rechts</a>");
			$(this).append(leftButton);
			$(this).append(rightButton);
			
			leftButton.click(methods.moveLeft);
			rightButton.click(methods.moveRight);
		}
		
		while (carouselItems.length < 4) carouselItems.push('empty');
		
		$(carouselItems).each(function(index, item) {
			item = $(item);
			if (index == 0 || index == 2) {
				item.find('img').each(function(index, item) {
					 if($(item).is('.carousel-background')) {
					 	$(item).css({
					 		width: 439,
					 		height: 312
					 	});
					 } else {
						$(item).css({
							width: 418,
							height: 257,
							top: 25,
							left: 11
						});
					 }
				});
				item.css({
					width: 439,
					height: 310
				});
				
				if (index == 0) {
					 dur = 150;
					 item.css({display: 'block', left: 256, top: 60, zIndex: 5});
					 $(item).delay(dur).fadeIn().animate({left: 0} , {duration: 500, queue: false});					 
				}
				if (index == 2) {
					 dur = 150;				
					item.css({display: 'block', left: 256, top: 60, zIndex: 5});
					$(item).delay(dur).fadeIn().animate({left: 512}, {duration: 500, queue: false});					
				}
							
			} else if (index == 1) {
				item.css({
					display: 'block',
					left: 185,
					zIndex: 1000
				});
				
				$(item).fadeIn();				
			} else {
				item.css({
					zIndex: 5,
					width: 329,
					height: 234,
					left: 256,
					top: 80,
					display: 'none'
				});
			
			}
		});			

		$('#carousel-left, #carousel-right').delay(500).fadeIn(250);
	}	
		
})(jQuery);
