(function($){
	$.fn.carousel = function(options) {  
		// If options exist, merge them with the default settings
		options = $.extend({
			itemDuration:	8500,		// How long each item is shown before moving to the next one
			transitionDuration:	700,	// Duration of transition effects
			transitionType: 'swing',	// Should be able to set this, but so far, swing is the only easing effect that works
			stopOnClick: true			// If true, the animations will stop permanently if the user physically clicks on a navigation item
		}, options);
		
		return this.each(function() {
			var obj = $(this);
			
			var timer = '';
			
			// If javascript is enabled, display navigation
			$('#pagingNav').show();
			$('#featureholder').css('marginTop', '12px');
			
			// Hide all images that aren't selected by default
			$('.feature:not(#image' + $('.selected', '#pagingNav').attr('id') + ')', '#featureholder').each(function() {
				$(this).animate({ 'opacity': 0 }, options.transitionDuration);
			});
			
			// Start the timer
			clearTimeout(timer);
			timer = setTimeout(changeItem, options.itemDuration);
			
			function changeItem() {
				var active = $('.selected', '#pagingNav');
				var next = active.next('.btnCarousel', '#pagingNav');
				if (!next.length) {
					next = $('.btnCarousel', '#pagingNav').first();
				}
				
				// Emulate a navigation click
				next.trigger('click', ['emulated']);
			}
			
			$('.btnCarousel', '#pagingNav').click(function(e, emulated) {
				e.preventDefault();
				
				// Set variables
				var shownID = $('.selected', '#pagingNav').attr('id');
				var shownImg = $('#image' + shownID);
				var clickedEl = $(this);
				var clickedID = clickedEl.attr('id');
				
				// Highlight selected navigation element
				$('.selected', '#pagingNav').each(function() {
					$(this).removeClass('selected');
				});
				clickedEl.addClass('selected');
				
				// Fade images
				shownImg.animate({ 'opacity': 0 }, options.transitionDuration);
				$('#image' + clickedID).animate({ 'opacity': 1 }, options.transitionDuration);
				
				// Calculate and set margins
				varMargin = -603 * (parseInt(shownID) - 1);
				if (parseInt(clickedID) > parseInt(shownID)) {
					varLarger = parseInt(clickedID);
					varSmaller = parseInt(shownID);
					varNewMargin = (varMargin - (603 * (varLarger - varSmaller)));
				} else {
					varLarger = parseInt(shownID);
					varSmaller = parseInt(clickedID);
					varNewMargin = (varMargin + (603 * (varLarger - varSmaller)));
				}
				
				// Do the Slide
				$('#copyholder').animate({ marginLeft: varNewMargin }, { duration: options.transitionDuration, easing: options.transitionType });
				
				// If the user clicks on one of the navigation items, stop the auto-scrolling
				clearTimeout(timer);
				if (!options.stopOnClick || emulated !== undefined) {
					timer = setTimeout(changeItem, options.itemDuration);
				}
			});
			
			$mysjquery('.btnArrowLeft').click(function(e) {
				// Stop the link from actually sending us to the new page
				e.preventDefault();
				
				var active = $('.selected', '#pagingNav');
				var prev = active.prev('.btnCarousel', '#pagingNav');
				if (!prev.length) {
					prev = $('.btnCarousel', '#pagingNav').last();
				}
				
				// Emulate a navigation click
				prev.trigger('click');
			});
			
			$mysjquery('.btnArrowRight').click(function(e) {
				// Stop the link from actually sending us to the new page
				e.preventDefault();
				
				var active = $('.selected', '#pagingNav');
				var next = active.next('.btnCarousel', '#pagingNav');
				if (!next.length) {
					next = $('.btnCarousel', '#pagingNav').first();
				}
				
				// Emulate a navigation click
				next.trigger('click');
			});
		});
	};
})(jQuery);
