var resizeFooter = function(resized) {
	var windowHeight = $(window).height();
	var topHeight = 290;
	var contactHeight = $("#contact").outerHeight();
	var newHeight = windowHeight - topHeight - contactHeight;
	
	if (resized) {
		newHeight -= 45;
	}
	
	if (newHeight > 0) {
		$("#footerWrap").css("height", newHeight + "px");
	}
};

var initScroller = function(name) {
	$("#scroller-" + name + "-window").scrollable({
		easing: 'easeOutQuad',
		size: '1',
		items: '#scroller-' + name + '-wrap',
		loop: 'true',
		prev: '#button-prev-project-' + name,
		next: '#button-next-project-' + name,
		navi: '#project-dots-' + name,
		naviItem: 'a',
		keyboard: false,
		speed: 'slow'
	});
};

$(window).ready(function() {
	// Attach click event to top menu items
	$("#menu a").click(function() {
	
		// Scroll to content
        var top = $($(this).attr("href")).offset().top - 300;
        $('html, body').animate({ scrollTop: top }, 'slow');
		
		return false;
	});
	
	$("#submenu a").click(function() {
		$(".workcontent").hide();
		$("#submenu a").removeClass("active");
		
		$($(this).attr("href")).show();
		$(this).addClass("active");
		
		return false;
	});
	
	// When content item is displayed, set corresponding menu tab to active
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop() + 325;
		var activeTab = "";
		
		$("div.pagecontent div.content").each(function() {
			if (scrollTop >= $(this).offset().top) {
				activeTab = $(this).attr("id");
			}
		});
		
		$("#menu a").removeClass("active");
		if (activeTab) {
			$("#menu li." + activeTab + " a").addClass("active");
		}
		else {
			$("#menu li:first a").addClass("active");
		}
	});
		
	// Resize footer when window is resized
	$(window).resize(function() {
		resizeFooter(true);
	});
	
	// Set initial footer height
	resizeFooter(false);

});

$(function(){
	var spt = $('span.mailme');
	var at = / at /;
	var dot = / dot /g;
	var addr = $(spt).text().replace(at,"@").replace(dot,".");
	$(spt).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
	.hover(function(){window.status="Send a letter!";}, function(){window.status="";});
	$(spt).remove();
});
