$(document).ready(function() {
		
		
	var position=0; //down
	
	var headerHeight = 125;
	var footerHeight = 100;
	
	var whatActive = 0;
	var whenActive = 0;
	var whyActive = 0;
	var howActive = 0;
	
	
	
	
	
		
	var resizeTimer;
	$(window).resize(function() {
		if (position==1) {
		    clearTimeout(resizeTimer);
		    resizeTimer = setTimeout(correctHeight, 100);
		}
	});
	
	
	function correctHeight () {
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
	
		$("#nav").animate({height: upPosition}, "fast");
	
	};
	
	
	
	
	
	
	
	
	var loadPathName = $(location).attr('href');
	if (loadPathName.indexOf('what') > -1) {
		setTimeout(function() { 
			var windowHeight = $(window).height();
			var upPosition = (windowHeight - headerHeight);
			var downPosition = footerHeight;
			$("#nav").animate({height: upPosition}, "slow", function () { $("#what").fadeIn(1000, function () { $(".moveWhat").addClass("selectedState"); } ); } );
			whatActive = 1;	
			position = 1; }, 1500);
	}
	if (loadPathName.indexOf('when') > -1) {
		setTimeout(function() { 
			var windowHeight = $(window).height();
			var upPosition = (windowHeight - headerHeight);
			var downPosition = footerHeight;
			$("#nav").animate({height: upPosition}, "slow", function () { $("#when").fadeIn(1000, function () { $(".moveWhen").addClass("selectedState"); } ); } );
			whenActive = 1;	
			position = 1; }, 1500);
	}
	if (loadPathName.indexOf('why') > -1) {
		setTimeout(function() { 
			var windowHeight = $(window).height();
			var upPosition = (windowHeight - headerHeight);
			var downPosition = footerHeight;
			$("#nav").animate({height: upPosition}, "slow", function () { $("#why").fadeIn(1000, function () { $(".moveWhy").addClass("selectedState"); } ); } );
			whyActive = 1;	
			position = 1; }, 1500);
	}
	if (loadPathName.indexOf('how') > -1) {
		setTimeout(function() { 
			var windowHeight = $(window).height();
			var upPosition = (windowHeight - headerHeight);
			var downPosition = footerHeight;
			$("#nav").animate({height: upPosition}, "slow", function () { $("#how").fadeIn(1000, function () { $(".moveHow").addClass("selectedState"); } ); } );
			howActive = 1;	
			position = 1; }, 1500);
	}
	if (loadPathName.indexOf('who') > -1) {
		setTimeout(function() { 
			var windowHeight = $(window).height();
			var upPosition = (windowHeight - headerHeight);
			var downPosition = footerHeight;
			$("#nav").animate({height: upPosition}, "slow", function () { $("#who").fadeIn(1000, function () { $(".moveWho").addClass("selectedState"); } ); } );
			whoActive = 1;	
			position = 1; }, 1500);
	}
	
	


	
	function fadeOutAll () {
		$("#what").fadeOut("slow");
		$("#when").fadeOut("slow");
		$("#why").fadeOut("slow");
		$("#how").fadeOut("slow");
		
		$(".moveWhat").removeClass("selectedState");
		$(".moveWhen").removeClass("selectedState");
		$(".moveWhy").removeClass("selectedState");
		$(".moveHow").removeClass("selectedState");
		
		whatActive = 0;
		whenActive = 0;
		whyActive = 0;
		howActive = 0;
	};






	
	$(".clickMe").click(fadeOutAll);
	
	
	
	function blankSelected () {
		$("#nav:first").removeClass("selected");
	}
	
	function moveNavUp () {
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		$("#nav").animate({height: upPosition}, "slow");	
	}
	
	

	
	$(".moveWhat").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
		
		if (position == 0 && whatActive == 0) {
			$("#nav").animate({height: upPosition}, "slow", function () { $("#what").fadeIn(1000, function () { $(".moveWhat").addClass("selectedState"); } ); } );
			whatActive = 1;	
			position = 1;
			return;
		}
		
		if (position == 1 && whatActive == 0) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow", function () { $("#nav").animate({height: upPosition}, "slow", function () { $("#what").fadeIn(1000, function () { $(".moveWhat").addClass("selectedState"); } ); }); } );
			whatActive = 1;	
			return;
		}
		
		if (position == 1 && whatActive == 1) {
			$("#what").fadeOut(50);
			$("#nav").animate({height: downPosition}, "slow", function () { $(".moveWhat").removeClass("selectedState"); } );
			whatActive = 0;
			position = 0;
			return;
		}
	
	});
	
	$(".moveWhen").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
		
		if (position == 0 && whenActive == 0) {
			$("#nav").animate({height: upPosition}, "slow", function () { $("#when").fadeIn(1000, function () { $(".moveWhen").addClass("selectedState"); } ); } );
			whenActive = 1;
			position = 1;
			return;
		}
		
		if (position == 1 && whenActive == 0) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow", function () { $("#nav").animate({height: upPosition}, "slow", function () { $("#when").fadeIn(1000, function () { $(".moveWhen").addClass("selectedState"); } ); }); } );
			whenActive = 1;	
			return;
		}
		
		if (position == 1 && whenActive == 1) {
			$("#when").fadeOut(50);
			$("#nav").animate({height: downPosition}, "slow", function () { $(".moveWhen").removeClass("selectedState"); } );
			whenActive = 0;
			position = 0;
			return;
		}
	
	});
	
	$(".moveWhy").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
		
		if (position == 0 && whyActive == 0) {
			$("#nav").animate({height: upPosition}, "slow", function () { $("#why").fadeIn(1000, function () { $(".moveWhy").addClass("selectedState"); } ); } );
			whyActive = 1;	
			position = 1;
			return;
		}
		
		if (position == 1 && whyActive == 0) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow", function () { $("#nav").animate({height: upPosition}, "slow", function () { $("#why").fadeIn(1000, function () { $(".moveWhy").addClass("selectedState"); } ); }); } );
			whyActive = 1;	
			return;
		}
		
		if (position == 1 && whyActive == 1) {
			$("#why").fadeOut(50);
			$("#nav").animate({height: downPosition}, "slow", function () { $(".moveWhy").removeClass("selectedState"); } );
			whyActive = 0;
			position = 0;
			return;
		}
	
	
	});
	
	$(".moveHow").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
		
		if (position == 0 && howActive == 0) {
			$("#nav").animate({height: upPosition}, "slow", function () { $("#how").fadeIn(1000, function () { $(".moveHow").addClass("selectedState"); } ); } );
			howActive = 1;	
			position = 1;
			return;
		}
		
		if (position == 1 && howActive == 0) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow", function () { $("#nav").animate({height: upPosition}, "slow", function () { $("#how").fadeIn(1000, function () { $(".moveHow").addClass("selectedState"); } ); }); } );
			howActive = 1;	
			return;
		}
		
		if (position == 1 && howActive == 1) {
			$("#how").fadeOut(50);
			$("#nav").animate({height: downPosition}, "slow", function () { $(".moveHow").removeClass("selectedState"); } );
			howActive = 0;
			position = 0;
			return;
		}
	
	});
	
	$(".moveWho").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
		
		if (position == 0 && whoActive == 0) {
			$("#nav").animate({height: upPosition}, "slow", function () { $("#who").fadeIn(1000, function () { $(".moveWho").addClass("selectedState"); } ); } );
			whoActive = 1;	
			position = 1;
			return;
		}
		
		if (position == 1 && whoActive == 0) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow", function () { $("#nav").animate({height: upPosition}, "slow", function () { $("#who").fadeIn(1000, function () { $(".moveWho").addClass("selectedState"); } ); }); } );
			whoActive = 1;	
			return;
		}
		
		if (position == 1 && whoActive == 1) {
			$("#who").fadeOut(50);
			$("#nav").animate({height: downPosition}, "slow", function () { $(".moveWho").removeClass("selectedState"); } );
			whoActive = 0;
			position = 0;
			return;
		}
	
	
	});
	
	$(".closeNav").click(function(){
		var windowHeight = $(window).height();
		var upPosition = (windowHeight - headerHeight);
		var downPosition = footerHeight;
	
		if (position == 1) {
			fadeOutAll();
			$("#nav").animate({height: downPosition}, "slow");
			position = 0;
		}	
	})

});
