var winSize;
var navPlace;
var navItems = new Array();
var emptySpace = 0;
var navTimer;
var navRolloutWidth = 400;
var rollInX = 0;
var navTime = 1000;

$(function(){
	getNav();
	$("#navigation").css({left: navPlace}).animate({height: "10px"});
	$("#navigation").hover(
		function(){
			clearTimeout(navTimer);
			navRollout();
		},
		function(){
			navTimer = setTimeout('navRollin()', 750);
		}
	);
	navRollin();
	setCovers();
	$(window).resize(function(){setCovers();});
});

function getNav() {
	winSize = $("#navigation").parent().width();
	var navSize = $("#navigation").width();
	navPlace = winSize/2 - navSize/2;
	var itemWidth = 0;
	var widthSum = 0;
	$(".navItem a").each(function(){
		navItems.splice(navItems.length,0,$(this).text());
		$(this).parent().attr({id:$(this).text()});
		$(this).click(function(){
			loadContent($(this).text());
			$(".active").removeClass("active");
			$(this).parent().addClass("active");
			return false;
		});
		itemWidth = $(this).parent().width();
		widthSum += itemWidth;
	});
	emptySpace = navRolloutWidth - widthSum;
	rollInX = navSize/2 - itemWidth/2;
}

function loadContent(page){
	var moveMe;
	switch(page) {
		case "Home":
			moveMe = 0;
			break;
		case "Services":
			moveMe = -600;
			break;
		case "Portfolio":
			moveMe = -1200;
			break;
		case "Contact":
			moveMe = -1800;
			break;
	}
	
	if(jQuery.browser["mozilla"] == true && navigator.appVersion.indexOf("Win") != -1) {
		$(".subContent").removeClass("hide");
		$(".subContent").not("#"+page+"Pane").addClass("hide");
		//$("#content").css({top: moveMe});
	}
	else {
		$("#content").animate({top: moveMe}, navTime, "swing");
	}
}

function navRollin() {
	$("#navigation").animate({height: "14px",width:"300px",left:navPlace});
	$("#navColor").animate({opacity: ".5"});
	$(".navItem").animate({left:rollInX, top:-$(".navItem").height()});
}

function navRollout() {
	$("#navigation").animate({height: $(".navItem").height(),width:navRolloutWidth+"px",left:navPlace-50});
	$("#navColor").animate({opacity: "0"});
	var navWidth = 0;
	var spaceSplit = navItems.length;
	spaceSplit--;
	var lastWidth = 0;
	for(x in navItems) {
		navWidth += lastWidth;
		$("#"+navItems[x]).animate({top: "1px", left: navWidth});
		lastWidth = (emptySpace/spaceSplit) + $("#"+navItems[x]).width();
	}
}

function setCovers() {
	var contWidth = $("#container").width();
	var contPos = $("#container").css("top");
	var docWidth = $(document).width();
	var covWidth = (docWidth - contWidth)/2;
	$(".bgCover").css({top: contPos, left: "0px", width: covWidth}).slice(1).css({left: contWidth+covWidth});
}