(function($) {

	var $this = null;

	var $slidesReel = null;

	var $paging = null;

	var $pagingLinks = null;

	var $active = null;

	var methods = {
		init : function(options) {

			// CODE

			var settings = {
				'activeIndex' : 0,
				'effect' : 'fade',
				'interval' : 7000,
				'pagingClass' : 'paging',
				'slideClass' : 'slide',
				'onSlideChange' : null,
				'stop' : false
			};

			// return this.each(function() {

			// If options exist, lets merge them
			// with our default settings
			if (options) {
				$.extend(settings, options);
			}

			$this = $(this);

			$slidesReel = $this.children().first().addClass("slidesReel");

			$paging = $("." + settings.pagingClass);

			$pagingLinks = $paging.find("a");

			// Show the paging and activate its first link
			$paging.show();

			$paging.find("a:first").addClass("active");

			// Get size of the image, how many images there are, then determin
			// the size of the image reel.
			var imageWidth = $this.outerWidth();

			var imageSum = $slidesReel.find("." + settings.slideClass).size();

			var imageReelWidth = imageWidth * imageSum;

			// Adjust the image reel to its new size
			$slidesReel.css({
				'width' : imageReelWidth
			});

			// Paging and Slider Function
			rotate = function() {

				if ($active.length === 0) { // If paging reaches the end...
					$active = $paging.find('a:first'); // go back to first
				}

				var triggerID = $active.attr("rel") - 1; // Get number of
				// times to slide

				var image_reelPosition = triggerID * imageWidth; // Determines
				// the distance the image reel needs to slide

				$pagingLinks.removeClass('active'); // Remove all active class
				$active.addClass('active'); // Add active class (the $active is
				// declared in the rotateSwitch function

				// Slider Animation
				switch (settings.effect) {
				// Fade effect
				case "fade":
					if (settings.onSlideChange !== null) {
						settings.onSlideChange();
					}
					$slidesReel.hide().css("left", -image_reelPosition + "px")
							.fadeIn(1000);
					break;
				// Slide effect
				default:
					$slidesReel.animate({
						left : -image_reelPosition
					}, 1000, function() {
						settings.onSlideChange()
					});
					break;
				}
			};

			// Rotation and Timing Event
			rotateSwitch = function(prev) {
				
				play = setInterval(function() { // Set timer - this will repeat
					// itself every 7 seconds

					$active = $paging.find('a.active')

					// rotate backwards

					$pagingLinks.index($active) + 1;
					$active = $pagingLinks.eq($pagingLinks.index($active) + 1); // Move

					rotate(); // Trigger the paging and slider function
				}, settings.interval); // Timer speed in milliseconds (7
										// seconds)
			};

			if (!settings.stop) {
				rotateSwitch(); // Run function on launch
			}

			// On Hover
			$slidesReel.find("a").hover(function() {
				clearInterval(play); // Stop the rotation
			}, function() {
				rotateSwitch(); // Resume rotation timer
			});

			// On Click
			$pagingLinks.click(function() {
				$active = $(this); // Activate the clicked paging
				// Reset Timer
				clearInterval(play); // Stop the rotation
				rotate(); // Trigger rotation immediately
				rotateSwitch(); // Resume rotation timer
				return false; // Prevent browser jump to link anchor
			});
			return $this;
		},
		move : function(pos) {
			$active = $pagingLinks.eq(pos - 1);
			clearInterval(play); // Stop the rotation
			rotate(); // Trigger rotation immediately
			rotateSwitch(); // Resume rotation timer
		},
		start : function() {
			rotateSwitch();
		},
		stop : function(pos) {
			clearInterval(play);
		},
		next : function() {

		//	console.log("setting:"+settings.effect);
			
			clearInterval(play); // Stop the rotation

			$active = $pagingLinks.eq($pagingLinks.index($active) + 1); // Move

			rotate(); // Trigger rotation immediately
			rotateSwitch(); // Resume rotation timer
		},
		prev : function() {
			clearInterval(play); // Stop the rotation
			$active = $pagingLinks.eq($pagingLinks.index($active) - 1); // Move

			rotate(); // Trigger rotation immediately
			rotateSwitch(); // Resume rotation timer
		}
	};

	$.fn.openCarousel = function(method) {

		// Method calling logic
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(
					arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method
					+ ' does not exist on jQuery.openCarousel');
		}

	};
})(jQuery);
