// when the DOM is ready...
var activeI, scTimer, scTimeoutMin = 10000, scTimeoutMax = 10000, scTimeout = 10000;

$(document).ready(function () {
	
	var showCaseMargin;
	
	$("#navigation li a").click(function() {
		showCaseNext(this);
	});
	
	
	function initShowCase() {
		w = $(window).width();
		w = (w - 990) / 2;
		if(w < 80) {
			w = 80;
		}
		
		showCaseMargin = w - 80;
		
		$("#sections div.item").css("marginLeft", w + "px");
		$("#sections div.item").css("marginRight", w + "px");
		$("#navigation li a").eq(0).click();
	}
	initShowCase();
	
	$(window).resize(function() {
		initShowCase();
	});
});

function showCaseClick() {
	activeI++;
	if(activeI >= $("#navigation li").length) {
		activeI = 0;
	}
	$("#navigation li").eq(activeI).find("a").click();
}
function showCaseNext(w) {
	clearTimeout(scTimer);
	activeI = $(w).parent("li").index();
	activeTab($(w).parent("li"));
	
	$(w).parent("li").parent("ul").find("a").removeClass("hasTab");
	
	i = $(w).attr("rel");
	p = $("#sections div.item").eq(i).position();
	t = 0 - p.top;
	l = 40 - p.left ;
	
	//$(".showcase").stop();
	$(".showcase").animate({"top": t + "px", "left": l + "px"}, 1500, function() {
		
		if(activeI == 0) {
			scTimeout = scTimeoutMax;
		} else {
			scTimeout = scTimeoutMin;
		}
		slideTab($(w).parent("li"));
		scTimer = setTimeout("showCaseClick()", scTimeout);
	});
}
function slideTab(w) {
	
	l = $(w).children("a");
	
	$(l).addClass("hasTab");
	$(l).css("background-position", "-160px 0");
	w = $(w).width();
	w = 0 - (150 - w);
	
	$(l).animate({"background-position":w + "px 0px"}, scTimeout, 'linear'); 
}
