/*
 * jQuery Carousel Compilation by Ryan Barr
 * http://spookyismy.name
 * v1.0.0
 */
$(document).ready(function() {

	/* Show default content within .carousel-content */
	$(".carousel-content").show();
	$(".carousel-content .carousel-content-image").animate({ opacity: 1 }, 1 );
	
	/* Set the first menu item as active. */
	$("#carousel-menu li.menu-item:first").addClass('selected');
	
	/* If menu item is clicked, switch content properly. */
	$("#carousel-menu li.menu-item").click(function(){
		
		var altText = $(this).find('.carousel-content-image img').attr("alt");
		var imgSrc = $(this).find('a.carousel').attr("href");
		var imgDesc = $(this).find('.info').html();
		
		if ($(this).is(".selected")) {
			return false;
		} else {
			$(".carousel-content").animate({ opacity: 0 }, 500 , function() {
				$(".carousel-content-info").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 1000 );
				$(".carousel-content-image img").attr({ src: imgSrc , alt: altText }).animate({ opacity: 1 }, 1000 );
				$(".carousel-content").animate({ opacity: 1 }, 500 );
			});
		}
		
		$("#carousel-menu li").removeClass('selected');
		$(this).addClass('selected');
		return false;
	
	});
	
	$("#carousel-menu li.menu-item").hover(function(){
		
		var altText = $(this).find('.carousel-content-image img').attr("alt");
		var imgSrc = $(this).find('a.carousel').attr("href");
		var imgDesc = $(this).find('.info').html();
		
		if ($(this).is(".selected")) {
			return false;
		} else {
			$(".carousel-content").animate({ opacity: 0 }, 500 , function() {
				$(".carousel-content-info").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 1000 );
				$(".carousel-content-image img").attr({ src: imgSrc , alt: altText }).animate({ opacity: 1 }, 1000 );
				$(".carousel-content").animate({ opacity: 1 }, 500 );
			});
		}
		
		$("#carousel-menu li").removeClass('selected');
		$(this).addClass('selected');
		return false;
	
	});

	/* Pause Rotation if Hovering #carousel-menu Item */
	carouselNext = false;
	$("#carousel-menu li.menu-item").hover(
		function () {
			carouselNext = true;
		},
		function () {
			carouselNext = false;
		}
	);
	
		carouselNext = false;
	$(".carousel-content-image").hover(
		function () {
			carouselNext = true;
		},
		function () {
			carouselNext = false;
		}
	);

 		carouselNext = false;
	$(".carousel-content-info").hover(
		function () {
			carouselNext = true;
		},
		function () {
			carouselNext = false;
		}
	);

	/* Rotate Carousel, Move from :last to :first if necessary. */
	var carouselNextClick = function(){
		if(!carouselNext) {

			var $nextElement = $(".selected").next("li.menu-item");

			if($("li.selected").hasClass("menu-last") ){
				$("#carousel-menu li.menu-item:first").trigger("click");
			} else {
				$nextElement.trigger("click");
			}

		}
	};


	/* Pause between rotations in milliseconds. (6000 is 6.0 seconds) */
	setInterval(carouselNextClick, 10000);

});