var visible = "";
var lcn = "";
var pc = "";
var rs = "";
var pvSet = {};
var span = true;
var time = 750/2;

function sitepart(tgt) {
	if (visible && (visible != tgt)) {
		$(visible).fadeOut(time);
	}
	if (tgt && (visible != tgt)) {
		$(tgt).delay(time+25).fadeIn(time);
	}
	visible = tgt;
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function checklocation() {
	if (window.location.hash) {
		current = window.location.hash;
		if (lcn != current && current != "portrait") {
		    $("#portrait").fadeOut(450);
			sitepart(current);
			lcn = current;
		} else if (lcn != current && lcn != "" && current == "portrait") {
		    visible = "portrait";
		    $("#portrait").fadeIn(750);
		    lcn = current;
		}
	}
}

function resumesections() {
	var projects = [];
	var as = $("#menu-content a");
	for (i = 0; i < as.size(); i++) {
		projects.push(as.slice(i).attr("href").split("#")[1]);
	}
	for (i = 0; i < projects.length; i++) {
		pid = "#"+projects[i];
		pclass = "."+projects[i];
		$(pid).append('<div class="resume"></div>');
		sections = $(".resume-section").has(pclass).clone();
		for (j = 0; j < sections.size(); j++) {
			$(pid + " .resume").append($(sections.get(j)).children("h3").get(0));
			$(pid + " .resume").append("<ul></ul>");
			$(pid + " .resume ul").slice(-1).append($(sections.get(j)).find(pclass));
		}
	}
}

function menurotate() {
	$.each($("#menu-content a"), function(){
		if (span) {
			$(this).find('span.in').delay(Math.floor(Math.random()*501)).animate({opacity: 0}, 1000);
		} else {
			$(this).find('span.in').delay(Math.floor(Math.random()*501)).animate({opacity: 1}, 1000);
		}
	});
	span = !span;
}

$(document).ready(function() {
	$("#menu-content a").css("padding", "0").css("height", "80px");
	$("#person .info").css("display", "none");
	$("#content .content").css("display", "none").css("padding-top", "0");
	$("#content").css("margin", "0");
	resumesections();
	$.each($(".photos").parent(), function(){
		$(this).find(".photos a").attr("rel", $(this).attr("id"));
	});
	$.each($(".photos"), function(){
		pvSet[$(this).parent().attr("id")] = [];
		var set = pvSet[$(this).parent().attr("id")];
		$.each($(this).find("a"), function(){
			var pic = $(this).attr("href").replace("fullscreen-", "preview-");
			set.push(pic);
			$("body").append('<img src="' + pic + '" style="display: none;" />');
		});
	});
	$("#menu-content a").wrapInner('<span class="in" />');
	$("#menu-content span").wrap('<span class="out" />');
	$("#menu-content li").hover(function(){
		var name = $(this).find('a').attr('href').split('#')[1];
		var set = pvSet[name];
		$(this).find('a').css("background", 'url("' + set[Math.floor(Math.random()*set.length)] + '")');
		$(this).find('span').stop();
		$(this).find('span').animate({opacity: 0}, 200);
		$("body").append('<div class="menu-hover" id="menu-hover-' + name + '"></div>');
		$("#menu-hover-" + name).text($(this).text());
		$("#menu-hover-" + name).css("top", $(this).offset()['top']+90);
		$("#menu-hover-" + name).css("left", $(this).offset()['left']);
		$("#menu-hover-" + name).stop();
		$("#menu-hover-" + name).animate({opacity: 1}, 200);
	}, function(){
		var name = $(this).find('a').attr('href').split('#')[1];
		$(this).find('span').stop();
		$(this).find('span.out').animate({opacity: 1}, 200);
		if (span) {
			$(this).find('span.in').animate({opacity: 1}, 200);
		}
		$("#menu-hover-" + name).stop();
		$("#menu-hover-" + name).animate({opacity: 0}, 200, function(){
			$(this).remove();
		});
	});
	$(".photos a").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'speedIn'		: 250,
		'speedOut'	 	: 250
	});
	// checklocation(); // we're not doing this because we'd get shitty scrolling we don't like!
	window.setInterval('checklocation()',185);
	menurotate();
	window.setInterval('menurotate()', 4000);
});

