/**
 * jQuery slideshow
 * Tony Milne (Inlight Media).
 */

(function($) {
	$.fn.slideShow = function(params) {

		var defaults = {
			slideSelector: ".recent-project",
			directNavigationSelector: "#project-buttons a", // not previous + next is handled by the component.
			previousSelector: ".navigation a.previous",
			nextSelector: ".navigation a.next",
			delay: 5000
		};

		var options = $.extend(defaults, params);

		this.each(function() {

			var $obj = jQuery(this);

			var $slides = jQuery(options["slideSelector"], $obj);
    		var $firstSlide = $slides.eq(0);
			var $currentSlide = $firstSlide;
			var slideCount = $slides.length;

			var $previous = jQuery(options["previousSelector"], $obj);
			var $next = jQuery(options["nextSelector"], $obj);
			var $directNavigationLinks = jQuery(options["directNavigationSelector"], $obj).not($previous, $next);

			var timeoutId = -1;

			// Attach navigation event handlers.
			$previous.click(previousHandler);
			$next.click(nextHandler);
			$directNavigationLinks.click(directNavigationHandler);

			// Hide non-first slides and kickoff the automated rotation.
			$slides.not($firstSlide).hide();
			$directNavigationLinks.eq(0).addClass('active');
			timeoutId = setTimeout(rotateSlides, options["delay"]);

			function previousHandler() {
		        var index = $slides.index($currentSlide);
		        index--;
		        if (index < 0) {
		            index = slideCount-1;
		        }
		        $previousSlide = $slides.eq(index);
		        showSlide($previousSlide);
			}

			function nextHandler() {
		        var index = $slides.index($currentSlide);
		        index++;
		        if (index >= slideCount) {
		            index = 0;
		        }
		        $nextSlide = $slides.eq(index);
		        showSlide($nextSlide);
			}

			function directNavigationHandler() {
		        var targetIndex = parseInt($(this).text());
		        $targetSlide = $slides.eq(targetIndex-1);
		        showSlide($targetSlide);
			}

			function rotateSlides() {
			    // We're running the automated rotation, so clear out the timeoutId to -1.
			    timeoutId = -1;
			    nextHandler();
			}

			function showSlide($newSlide) {
			    $currentSlide.fadeOut('normal', function() {
			        $newSlide.fadeIn();
			    });

			    $currentSlide = $newSlide;

				$directNavigationLinks.removeClass('active');
				var slideIndex = $slides.index($newSlide);
				$directNavigationLinks.eq(slideIndex).addClass('active');

			    // @NOTE: this code was originally used for maintaining the active state on paginated links.
			    // Set the corresponding navigation link to active (removing other active states).
			    // +1 to offset the << previous link.
			    // var i = $slides.index($newSlide) + 1;
			    // jQuery("div.pagination a").removeClass("active");
			    // jQuery("div.pagination a:eq(" + i + ")").addClass("active");

			    // We've just shown a new slide so avoid animating too quickly,
			    // but set the automated rotation to run x seconds from now.
			    if (timeoutId != -1) {
			        clearTimeout(timeoutId);
			        timeoutId = -1;
			    }
			    timeoutId = setTimeout(rotateSlides, options["delay"]);
			}
		});
	};
})(jQuery);
