((function ($) {
	$.fn.carousel = function (options) {
	    $.fn.carousel = $.extend({}, $.fn.carousel.defaults, options);

		$.fn.carousel.width_int = $(this).outerWidth();
		
		// Correct invalid easing values.
		if ('swing' !== $.fn.carousel.easing || 'linear' !== $.fn.carousel.easing){$.fn.carousel.easing = 'swing';}
		
		$.fn.carousel.slidecount = $($.fn.carousel.carousel_items).children().length;

		// If there's more than one slide...
		if (1 < $.fn.carousel.slidecount) {
			// Create a clone of the first item and put at at the end of the slides - we'll use this to create the illusion of a smooth, infinite carousel.
			$($.fn.carousel.carousel_items + '>div:last-child').after($($.fn.carousel.carousel_items + '>div:first-child').clone());
			
			// Make the slide container wide enough so all the slides sit side-by-side on one line.
			$($.fn.carousel.carousel_items).css('width', ($.fn.carousel.width_int * $($.fn.carousel.carousel_items + '>div').length) + 'px');
			
			// Store the number of items
			$.fn.carousel.slidecount = $($.fn.carousel.carousel_items).children().length;

			// use a variable to track which item we're on. We need it to detect the edges of the carousel and enable a smooth wraparound.
			$.fn.carousel.selected = 0;
			$.fn.carousel.anim_lock = 0;
			$($.fn.carousel.carousel_items).css({
											'position' : 'relative',
											'left' : '-' + ($.fn.carousel.selected * $.fn.carousel.width_int) + 'px'
										});
			$($.fn.carousel.carousel_items).children().css({'float' : 'left', 'zoom' : '1'});
			
			$.fn.carousel.scrollprev = function() {
				if (0 === $.fn.carousel.anim_lock) {
					$.fn.carousel.anim_lock = 1;

					// Check to see if we're on the first item.
					if (0 >= $.fn.carousel.selected) {
						// We're looking at the first element but the user wan't to scroll to the left. Snap to the clone of the first element so that we can scroll left onto the real last element.
						$.fn.carousel.selected = $.fn.carousel.slidecount - 1;
						$($.fn.carousel.carousel_items).css('left','-' + ($.fn.carousel.selected * $.fn.carousel.width_int) + 'px');
						
					}

					--$.fn.carousel.selected;
					$($.fn.carousel.carousel_items).animate(
												{'left' : '-' + ($.fn.carousel.selected * $.fn.carousel.width_int) + 'px'},
												$.fn.carousel.duration,
												$.fn.carousel.easing,
												function (){
													$.fn.carousel.anim_lock=0;
												}
										);
				}			
			}
			
			$.fn.carousel.scrollnext = function() {
				if (0 === $.fn.carousel.anim_lock) {
					$.fn.carousel.anim_lock = 1;
					++$.fn.carousel.selected;
					$($.fn.carousel.carousel_items).animate(
												{'left' : '-' + ($.fn.carousel.selected * $.fn.carousel.width_int) + 'px'},
												$.fn.carousel.duration,
												$.fn.carousel.easing,
												function (){
													if (($.fn.carousel.slidecount - 1) <= $.fn.carousel.selected) {
														// We're on the clone of the first element. Snap to the real first element.
														$.fn.carousel.selected = 0;
														$($.fn.carousel.carousel_items).css('left','-' + ($.fn.carousel.selected * $.fn.carousel.width_int) + 'px');
														
													}
													$.fn.carousel.anim_lock = 0;
												}
										);
				}
			}

			// Attach Prev button click event.
			if ($($.fn.carousel.prev)) {
				$($.fn.carousel.prev).click(function () {
					$.fn.carousel.autoscroll_reset();
					$.fn.carousel.scrollprev();
					return false;
				});
			}

			// Attach next button click event.
			if ($($.fn.carousel.next)) {
				$($.fn.carousel.next).click(function () {
					$.fn.carousel.autoscroll_reset();
					$.fn.carousel.scrollnext();
					return false;
				});
			}
			
			$.fn.carousel.autoscroll_reset = function () {
				// Set up auto scrolling
				if($.fn.carousel.autoscroll_hook){ clearInterval($.fn.carousel.autoscroll_hook); }
				$.fn.carousel.autoscroll_hook = setInterval($.fn.carousel.scrollnext, $.fn.carousel.autoscroll_time);
			}
			
			$.fn.carousel.autoscroll_reset();
		}
	};
})(jQuery));

$.fn.carousel.defaults = {
	carousel: '#carousel',
	carousel_items: '#slides',
	easing: 'swing',
	duration: 750,
	prev: ".carouselPrev", // must be class, use . sign
	next: ".carouselNext", // must be class, use . sign
	autoscroll: true,
	autoscroll_time: 9000,
	autoscroll_hook:{}
};
