(function($){

	$.fn.extend({
	
		myCarousel: function() {

			/*
				******** Licence ********
				
				Created by Zoltan Csontos 2010 - 2011
				Feel free to use, modify this plugin without any restrictions.
				Just leave the text within the Licence comments untouched
				
				******** Licence end *******
				
				******** Configuration settings ********
				
				parrentItem: the parret element containing the carouselContent
				animatedItem: the sliding item
				animatedItemChild: the animated heading inside the sliding item
				descriptionContainer: the element where the description is hosted
				descriptionItem: element which is changing in the desc. container
				carouselTiming: time interval between carousel moving
				displayDescription: If setted to false only the images are animated, without heading
				
				******** Configuration settings END********
				
			*/		
		
			var defaults = {
				parrentItem: "#myCarouselContent > ul",
				animatedItem: "#myCarouselContent > ul > li",
				animatedItemChild: "h2",
				positionContainer: "#myCarouselPosition",
				descriptionContainer: "#myCarouselDescription",
				descriptionItem: "p",
				carouselTiming: 10000,
				displayDescription: true
			};
			
			var options = $.extend(defaults, options);
			var o = options;
			var itemsAmount = $(o.animatedItem).size();
			var defaultDirection = "left";
			
			var interval = setInterval( function(){ moveCarousel(defaultDirection); }, o.carouselTiming );

			var rollOverActions = function() {
				$('#myCarouselContent, #left_arrow, #right_arrow').mouseleave(function() {
					clearInterval(interval);
					interval = setInterval( function(){ moveCarousel(defaultDirection); }, o.carouselTiming );
					$('#arrow_container').fadeOut('slow');
				}).mouseenter(function() {
					clearInterval(interval);
					$('#arrow_container').fadeIn();
				}).click(function() {
					clearInterval(interval);
					$('#arrow_container').fadeIn();				
				});
				$('#right_arrow').click(function() {
					$(this).next().stop();
					moveCarousel("left");
				});
				$('#left_arrow').click(function() {					
					moveCarousel("right");
				});
			};			
			
			var createPositionIcons = function() {
				var positionIco = "<span class=\"myCarouselPositionIco\">&nbsp;</span>";
				for (i = 0; i < itemsAmount; i++) {
					$(o.positionContainer).append(positionIco);
				}
			};
			
			var carouselPosition = function(direction) {
				var actualPosition = $('.current').index();
				var endPosition = $('.myCarouselPositionIco').last().index();
				if (actualPosition == -1) {
					var newPosition = (actualPosition + 2);
				}
				else if (actualPosition == endPosition && direction != "right") {
					var newPosition = 0;
				}
				else if (actualPosition == 0 && direction == "right") {
					var newPosition = endPosition;
				}
				else 
				{
					if (direction == "right") {
						var newPosition = (actualPosition - 1);
					}
					else
					{
						var newPosition = (actualPosition + 1);
					}
				}
				$('.current').css('background-position', '0px 0px');
				$('.myCarouselPositionIco').removeClass('current');
				$('.myCarouselPositionIco:eq('+newPosition+')').addClass('current');
				$('.current').css('background-position', '-10px 0px');
			};
			
			var headingAnimation = function(direction) {
				this.delay = Math.round(o.carouselTiming - (o.carouselTiming * 0.07));
				if(direction == "left") {
					child = ':nth-child(2)';
				}
				else if (direction == "right")
				{	
					child = ':last';
				}
				$(o.animatedItem).children(o.animatedItemChild).css('filter', 'alpha(opacity=80)');
				$(o.animatedItem+child).children(o.animatedItemChild).queue(function() {
					$(this).fadeIn('slow');
					$(this).dequeue();
				}); 
			};
			
			var moveCarousel = function(direction) {
				var carouselItemWidth = $(o.animatedItem).width();
				if (direction == undefined) {
					var direction = "left";
					var slideValue = parseInt($(o.parrentItem).css("left")) + carouselItemWidth;
				}
				else if (direction == "right")
				{
					var direction = direction;
					var slideValue = parseInt($(o.parrentItem).css("left")) - carouselItemWidth;
				}
				else
				{
					var direction = "left";
					var slideValue = parseInt($(o.parrentItem).css("left")) + carouselItemWidth;					
				}
				
				carouselPosition(direction);
				headingAnimation(direction);
				changeDescription();			
				
				if (direction == "left") {
					$(o.parrentItem).animate({"left": "-="+slideValue, queue: false, duration:1200}, function() {
						var position = $(o.parrentItem).position();
						$(o.animatedItem+':last').after($(o.animatedItem+':first'));
						$(o.parrentItem).css({"left" : (position.left + carouselItemWidth)});
						$(o.animatedItem+':nth-child(2)').children(o.animatedItemChild).queue(function() {
							$(this).fadeOut('slow');
							$(this).dequeue();
						});						
					});
				}
				else if (direction == "right") {
					$(o.animatedItem+':first').before($(o.animatedItem+':last'));
					var position = $(o.parrentItem).position();
					$(o.parrentItem).css({"left" : (position.left -= carouselItemWidth)});
					$(o.parrentItem).animate({"left": "-="+slideValue, queue: false, duration:1200}, function() {
						$(o.animatedItem+':last').children(o.animatedItemChild).queue(function() {
							$(this).fadeOut('slow');
							$(this).dequeue();
						});	
					})				
				}
			};			
			
			var changeDescription = function() {
				$(o.descriptionContainer+' > '+o.descriptionItem+':first').css('display', 'block');
				$(o.descriptionContainer+' > '+o.descriptionItem+':first').queue(function() {
					$(this).fadeOut('slow');
					$(this).dequeue();
				});
				$(o.descriptionContainer+' > '+o.descriptionItem+':last').after($(o.descriptionContainer+" > "+o.descriptionItem+':first'));
				$(o.descriptionContainer+' > '+o.descriptionItem+':first').queue(function() {
					$(this).fadeIn('slow');
					$(this).dequeue();
				});
			};
			
			return this.each(function() {
				rollOverActions();
				createPositionIcons();
				moveCarousel();
			});
			
		}
	
	});

})(jQuery);
