(function($) { 
	
	$.fn.wtdcarousel = function(options) {
		
		var opts = $.extend({}, $.fn.wtdcarousel.defaults, options);

		if(this.length){
			return this.each(function(){
				setCarousel($(this), opts);				  
			});
		}
		
		function repeat(str, num) {
			return new Array( num + 1 ).join( str );
		}

		function setCarousel($obj, opts){
			
			$obj.prepend('<div class=\'carousel-buttons\'><a id=\'prev\'>Previous item</a><a id=\'next\'>Next item</a></div>');
			$obj.append('<div class=\'carousel-overflow-left\'></div><div class=\'carousel-overflow-right\'></div>');   
			var carouselWidth = ($obj.width() - ($obj.find('div.carousel-buttons a').width() * 2));
			var carouselLeft = ($obj.find('div.carousel-buttons a').width())
			
 			$obj.find('div.carousel').css({
				'width': carouselWidth + 'px',
				'margin': '0 0 0 ' + carouselLeft + 'px'
			});

			$carousel = $obj.find('div.carousel').css('overflow', 'hidden'),
            $slider = $carousel.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($carousel.innerWidth() / singleWidth), 
            currentPage = 1,
            pages = Math.ceil($items.length / visible);    

			// If there are less numbers than can fit in the visible are create empty list items
			if (($items.length % visible) != 0) {
				$missingItems = visible - ($items.length % visible);
				$missingItems = $missingItems - 1;
				for(i=0;i<=$missingItems;i++){
					$slider.append($items.eq(i).clone())
				}
				//$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> li');
				//$slider.find('li.empty').css('visibility', 'hidden');
			}
			
			// Hide carousel buttons if there are there are not more items than can be visible
			if ($items.length / visible == 1) {
				$('div.carousel-buttons').hide();
			}
			
			// Clone items and insert at the beginning and at the end of the list of carousel items
			$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> li'); // reselect
        
			// Set the left position to the first item
			$carousel.scrollLeft(singleWidth * visible);
        	
			
        	// Function to move carousel
			function moveCarousel(page) {
				var dir = page < currentPage ? -1 : 1,
					n = Math.abs(currentPage - page),
					left = singleWidth * dir * visible * n;

				if(opts.slide == false){
					opts.speed = 0;		
				}
				
				$obj.find('div.carousel-overflow-left, div.carousel-overflow-right').css('display', 'block');
				
				$carousel.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, opts.speed, function () {
					if (page == 0) {
						$carousel.scrollLeft(singleWidth * visible * pages);
						page = pages;
					} else if (page > pages) {
						$carousel.scrollLeft(singleWidth * visible);
						// reset back to start position
						page = 1;
					} 
					$obj.find('div.carousel-overflow-left, div.carousel-overflow-right').css('display', 'none');
					currentPage = page;
				});  
				
				return false;
			}
        
			// Click events for forward and backward carousel buttons
			$('div.carousel-buttons #next').click(function () {
				return moveCarousel(currentPage - 1);                
			});
			
			$('div.carousel-buttons #prev').click(function () {
				return moveCarousel(currentPage + 1);
			});	

		}
		
	};
	
	// Carousel Defaults
	$.fn.wtdcarousel.defaults = {
		speed: 500,
		easing: 'easeOutQuint',
		slide: true
	};
	
	
})(jQuery);
