function navUpdate(el, ev) {

	// Show and hide the appropriate nav items on hover
	if (ev.type == 'mouseenter') {
		// Hide any other open subnavs
		$(el).siblings().removeClass('subnav-open');
		$(el).siblings().children('ul').addClass('hidden');
		
		// If this item has a subnav, show it
		if ($(el).hasClass('has-subnav')) {
			$(el).addClass('subnav-open');
			$(el).children('ul').removeClass('hidden');
		}
	}
	else {
		// Hide this subnav
		$(el).removeClass('subnav-open');
		$(el).children('ul').addClass('hidden');
	}
}



function navShowSelected(el) {

	// If el wasn't passed in, assume we're talking about #nav
	(!el) ? el = $('#nav') : el = el;
	
	// Make sure the selected item is visible
	$(el).find('.selected:first').children('ul').removeClass('hidden');	
	$(el).find('.selected:first').parents('ul').removeClass('hidden');
	$(el).find('.selected:first').parents('li').addClass('subnav-open');
	if ($(el).find('.selected:first').hasClass('has-subnav')) {
		$(el).find('.selected:first').addClass('subnav-open');
	}
}



$(document).ready(function(){

	$('body').addClass('js-enabled');

	// If there's a selected nav item, make sure it's visible on page load
	if ($('#nav').find('.selected').length) {
		navShowSelected($('#nav'));
	}
	
	// Update the nav on hover
	$('#nav li').hover(function(ev) {
		navUpdate(this, ev);
	});

	// Show the selected nav item when mousing off of the nav
	$('#nav').mouseleave(function() {
		navShowSelected(this);
	});

	// Clear/reset the search input
	$("#q").blur(function() {
		if ($(this).val() == '') {
			$(this).val(this.defaultValue);
		}
	}).focus(function() {
		if ($(this).val() == this.defaultValue) {
			$(this).val('');
		}
	});

	// Add class names to handle hover states in IE6
	$('#secondary .featured').hover(
		function() {
			$(this).toggleClass('hover');
		}
	);
	$('#tertiary li').hover(
		function() {
			$(this).toggleClass('hover');
		}
	);

	// Tool Tips
	$('.tool-tip').parent().hover(
		function() {
			$(this).children('.tool-tip').removeClass('hidden');
		},
		function() {
			$(this).children('.tool-tip').addClass('hidden');
		}
	);

	// Demonstrate form error states
	$('#request-speaker').submit(function(ev) {
		ev.preventDefault();
		$(document).scrollTop(0);
		$('p.error').show();
		$('.location').addClass('error');
		$('#location').focus();
	});

	// Open the lightbox
	if ($('a.open-lightbox').length) {
		$('a.open-lightbox').fancybox({
			'padding': 0,
			'overlayColor': '#000',
			'overlayOpacity': 0.6,
			onComplete: function() {
				$('#fancybox-inner a:first').focus();
			}
		});
	}

});