$(function() {
	$(window).bind("hashchange", loadPage);
	if(location.hash == ""){
//		location.hash = "#!work0";
	}else{
//		loadPage();
	}

	
	$("#contact").dialog({
		autoOpen: false,
		modal: true,
		width: 400,
		height: 300,
		resizable: false,
		draggable: false
	});

	if(location.href.match("work.html")){
		$("#container > header > nav > a.active").removeClass("active");
		$($("#container > header > nav > a")[0]).addClass("active");
	}else if(location.href.match("about.html")){
		$("#container > header > nav > a.active").removeClass("active");
		$($("#container > header > nav > a")[1]).addClass("active");
	}else if(location.href.match("team.html")){
		$("#container > header > nav > a.active").removeClass("active");
		$($("#container > header > nav > a")[2]).addClass("active");
	}else if(location.href.match("jobs")){
		$("#container > header > nav > a.active").removeClass("active");
		$($("#container > header > nav > a")[3]).addClass("active");
	}
	
});

function loadPage(e){

	if(location.hash.search("work") > 0){
		console.log("work");
		$("#container > header > nav > a.active").removeClass("active");
		$($("#container > header > nav > a")[0]).addClass("active");

		if($("#display").length > 0){
			loadDisplay((location.hash).match(/\d+/)[0]);
		}else{
			console.log("Loading from non work page");
			_gaq.push(['_trackPageview', '/work.html']);
			var page = (location.hash).match(/\d+/)[0];
			$("#content").load("work.html", function(){window.workCb();loadDisplay(page);});
		}
	}else if(location.hash.search("about") > 0){
		$("body,#controls,header > nav").removeClass("invert");
		$("#container > header > nav > a.active").removeClass("active");
		console.log("about");
		$($("#container > header > nav > a")[1]).addClass("active");
		_gaq.push(['_trackPageview', '/about.html']);
		$("#content").load("about.html");
	}else if(location.hash.search("team") > 0){
		$("body,#controls,header > nav").removeClass("invert");
		$("#container > header > nav > a.active").removeClass("active");
		console.log("team");
		$($("#container > header > nav > a")[2]).addClass("active");
		_gaq.push(['_trackPageview', '/team.html']);
		$("#content").load("team.html");
	}else if(location.hash.search("jobs") > 0){
		$("body,#controls,header > nav").removeClass("invert");
		$("#container > header > nav > a.active").removeClass("active");
		console.log("jobs");
		$($("#container > header > nav > a")[3]).addClass("active");
		_gaq.push(['_trackPageview', '/jobs.html']);
		$("#content").load("jobs.html", function(){window.jobsCb();});
	}else if(location.hash.search("labs") > 0){
		$("body,#controls,header > nav").removeClass("invert");
		$("#container > header > nav > a.active").removeClass("active");
		console.log("labs");
		$($("#container > header > nav > a")[2]).addClass("active");
		_gaq.push(['_trackPageview', '/labs.html']);
		$("#content").load("labs.html");
	}
	
// case "#contact":
// console.log("contact");
// $($("#container > header > nav > a")[3]).addClass("active");
// _gaq.push(['_trackPageview', '/contact.html']);
// $("#content").load("contact.html");
// break;
//	default:
//		location.hash = "#work0";
//	}
}
