$(document).ready(function() {
	
	/** nav icon hover fade
	*/
	var navspeed = 200;
	var initNavOp = 0.5
	$('#navigation li a').hover(function() {
		if (!($(this).hasClass('active')))
			$(this).stop().fadeTo(navspeed, 1.0);
	}, function() {
		if (!($(this).hasClass('active')))
			$(this).stop().fadeTo(navspeed, initNavOp);
	});
	
	/** page + nav transfers
		page1 is outgoing, page2 is incoming
	*/
	var $currentPage = $('#welcome');
	var $currentNav;
	function switchPages(page1, page2) {
		if (nothingIsAnimated()) {
			//alert ("Switching from " + $(page1).attr('id') + " to " + $(page2).attr('id'));
			if ($(page1).attr('id') != $(page2).attr('id')) {
				$currentPage = $(page2);
				$('#designWork').hide();
				$(page1).addClass('out', 500, "swing", function() {
					$(page2).removeClass('out', 500, "swing", function() {
						if ($(page2).attr('id') == "design")
							$('#designWork').fadeIn(500);
					});
				});
			}
		}
	}
	function switchNav(currentNav, nextNav) {
		//alert ("Switching nav to " + $(nextNav).parent().attr('class'));
		if ($currentNav != null) {
			$(currentNav).removeClass('active');
			$(currentNav).removeAttr('style');
		}
		$(nextNav).addClass('active');
		$(nextNav).removeAttr('style');
		$currentNav = $(nextNav);
	}
	function nothingIsAnimated() {
		if (
			!$('#welcome').is(':animated') &&
			!$('#aboutMe').is(':animated') &&
			!$('#design').is(':animated') &&
			!$('#code').is(':animated') &&
			!$('#contact').is(':animated')
		)
			return true;
		else
			return false;
	}
	$('#navigation li a').click(function() {
		if (nothingIsAnimated()) {
			var $nextPage = $(this).attr('href');
			if ($currentPage != $nextPage) {
				switchNav($currentNav, $(this));
				switchPages($currentPage, $nextPage);
			}
		}
	});
	
	/** logo click handler
	*/
	$("#logo a").click(function() {
		switchNav($currentNav, null);
		switchPages($currentPage, $(this).attr("href"));
	});
	
	
	/** btn shtuff
	*/
	function handleBtn(btn) {
		var knownavspeed = 200;
		$(btn).fadeTo(navspeed, initNavOp);
		$(btn).hover(function() {
			$(this).fadeTo(knownavspeed, 1.0);
		}, function() {
			$(this).fadeTo(knownavspeed, initNavOp);
		});
	}
	handleBtn($('#getToKnowMeBtn'));
	handleBtn($('#resumeBtn'));
	$('#getToKnowMeBtn').click(function() {
		switchNav(null, $('#navigation li.about_me_nav a'));
		switchPages($currentPage, $(this).attr('href'));
	});
	
	/**	design work box hovers n stuff
	*/
	$('.boxgrid.captionfull').hover(function() {
		$(".cover", this).stop().animate({top: '0px'}, {queue:false, duration:150});
	}, function() {
		$(".cover", this).stop().animate({top: '145px'}, {queue:false, duration:150});
	});
	
	/** fancy box!
	*/
	$("a.fancy_box_gallery").fancybox({
		titleFormat: formatTitle,
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		orig: $(this).parent()
	});
	
	$("a.fancy_box").fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		orig: $(this).parent()
	});
	
	/** custom title for FancyBox
	*/
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    	return '<div><span>' + 'Image ' + (currentIndex + 1) + ' of ' + currentArray.length + ' (Use arrow keys to scroll through images!)</div>';
	}
	
});



