var carouselInterval;
var allowScroll = true;

function showcaseSwitch(ele) {
	var className = String(ele.attr("class"));
	if (ele.attr("class") == $("#showcaseControls .current").attr("class")) {
		return;
	}
	$("#showcaseCarousel .title").css("top","350px").css("opacity",0);
	if (className == "prev") {
		var indexCurrent = $("#showcaseCarousel a").index($("#showcaseCarousel .currentShowcase")[0]);
		if (indexCurrent == 0) {
			var indexPrev = ($("#showcaseCarousel a").size())-2;
		} else {
			var indexPrev = indexCurrent - 1;
		}
		$("#showcaseControls .current").removeClass("current");
		$("#showcaseControls .s" + (indexPrev + 1)).addClass("current");
		$(".currentShowcase").hide().removeClass("currentShowcase");
		$("#showcaseCarousel a:eq(" + indexPrev + ")").fadeIn("slow").addClass("currentShowcase");		
		var imgTitle = String($("#showcaseCarousel a:eq(" + indexPrev + ")").attr("title"));
		animateTitle(imgTitle);		
	} else if (className == "next") {
		var indexCurrent = $("#showcaseCarousel a").index($("#showcaseCarousel .currentShowcase")[0]);
		if (indexCurrent == ($("#showcaseCarousel a").size())-2) {
			var indexNext = 0;
		} else {
			var indexNext = indexCurrent + 1;
		}				
		$("#showcaseControls .current").removeClass("current");
		$("#showcaseControls .s" + (indexNext + 1)).addClass("current");
		$(".currentShowcase").hide().removeClass("currentShowcase");
		$("#showcaseCarousel a:eq(" + indexNext + ")").fadeIn("slow").addClass("currentShowcase");
		
		var imgTitle = String($("#showcaseCarousel a:eq(" + indexNext + ")").attr("title"));		
		animateTitle(imgTitle);
	} else {		
		$("#showcaseControls .current").removeClass("current");
		$("#showcaseControls ." + className).addClass("current");
		$(".currentShowcase").hide().removeClass("currentShowcase");
		$("#showcaseCarousel ." + className).fadeIn("slow").addClass("currentShowcase");		
		var imgTitle = String($("#showcaseCarousel .currentShowcase").attr("title"));		
		animateTitle(imgTitle);
	}
}

function animateTitle(title) {
	$("#showcaseCarousel .title").text(title);
	$("#showcaseCarousel .title").attr("href", String($("#showcaseCarousel .currentShowcase").attr("href")));
	if (allowScroll) {
		allowScroll = false;
		$("#showcaseCarousel .title").animate({
			top: "420px",
			opacity: .75
	    }, 1000, function() {
			allowScroll = true;
		});
	}	
}

$().ready(function() {		
	$("#showcaseControls a").click(function () {
		showcaseSwitch($(this));
		clearInterval(carouselInterval);
	});	
	animateTitle(String($("#showcaseCarousel .currentShowcase").attr("title")));	
	carouselInterval = setInterval(function(){showcaseSwitch($("#showcaseControls a.next"));},5000);
});
