// How long to wait on each item (in milliseconds)
var itemWait = 5000;

// Scrolling Speed (in milliseconds)
var animSpeed = 400;

$(document).ready(function(){

	// Show itemlinks and activate first one
	$(".itemlinks li:first").addClass("active");
	$(".items li:first").addClass("active");
	$(".featuredtitle").html($(".itemlinks li:first .title").html());
	$(".itemlinks").fadeIn(500);

	// Set interval timer
	var slideLoop = setInterval(showNext, itemWait);

	$(".itemlinks li").click(function() {

		// Clear interval timer
		clearInterval(slideLoop);

		// Show item
		if ($(".items .active").length) showItem(this);

	});

});

// Show next item function
function showNext() {

	if ($(".itemlinks .active").next().length) showItem($(".itemlinks .active").next());
	else showItem($(".itemlinks li:first").get());

}

// Show specific item number
function showItem(item) {

	// Don't do anything if this tab is active
	if ($(item).hasClass("active")) return false;

	// Stop animation
	$(".items ul").stop();

	// Set active state
	$(".itemlinks li").removeClass("active");
	$(item).addClass("active");
	$(".featuredtitle").html($(item).find(".title").html());
	featureNum = $(item).prevAll().length;
	feature = $(".items li").eq(featureNum);
	feature.css("z-index", "49");

	// Animate
	$(".items .active").removeClass("active").fadeOut(animSpeed, function() {
		$(this).css("z-index", "40").show();
		feature.css("z-index", "50").addClass("active");
	});

}
