(function(){
	// if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
	var d = document, e = d.documentElement, s = d.createElement('style');
	if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
		s.textContent = 'body{visibility:hidden}';
		e.firstChild.appendChild(s);
		function f(){ s.parentNode && s.parentNode.removeChild(s); }
		addEventListener('load',f,false);
		setTimeout(f,3000); 
	}
})();

// DOM READY JS
$(document).ready(function() {
	
	// superfish navigation menu
    $('ul.sf-menu').superfish({ 
	    delay:         500, // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
	    speed:         'fast', // speed of the animation. Equivalent to second parameter of jQuery .animate() method 
	    autoArrows:    false, // if true, arrow mark-up generated automatically 
	    dropShadows:   true // completely disable drop shadows by setting this to false 
    });

	
	// add for-nav class to corresponding main nav items
	$('#m-nav li').not('#m-nav li li').slice(4).addClass('for-nav');

	// hide extra top level nav items
	$('#m-nav :nth-child(8n)').next('li').hide();

	// search box hover behavior dependent on focus status
	$('#global-search-form').hoverIntent(function() { // only fire if deliberate
		$(this).find('input').stop().animate({opacity: 1}, 300);
	}, function() {
		if ($(this).hasClass('inactive')) { // only if not currently in focus
			$(this).find('input').stop().animate({opacity: .2}, 300);
		}
	});
	
	// search box focus behavior and status marker
	$('#search-box').focus(function() {
		$("#global-search-form input").stop().animate({opacity: 1}, 300);
		$(this).closest('form').removeClass('inactive');
	}).blur(function() {
		$("#global-search-form input").stop().animate({opacity: 0.2}, 300);
		$(this).closest('form').addClass('inactive'); // 
	});
	
	
	// add correct border to li element above an active li element in subnav
	$('.sf-menu li li.here').prev('li').addClass('here-neighbor');
	
	// provide an element for the shadow on submenus
	$('.sf-menu ul li:first-child a').wrap('<div />');	
	
	// iterate through all people blocks
	$('.people-list').each(function(index) {
		var peopleCount = $(this).children('li').size();
		//alert(peopleCount);
		if(peopleCount > 5) {
			$(this).children('li').each(function(index) {
				if (index > 4) {
					$(this).addClass('extra').hide();
				}
			});
		} else {
			// hide toggle link if <= 5
			$(this).next('p').hide();
		}
	});

	// toggle additional entries
	$('.people-toggler').toggle(function() {
		$(this).parent().prev('ul.curtailed').children('.extra').show();
		$(this).find('span').html('-');
		return false;
	}, function() {
		$(this).parent().prev('ul').children('.extra').hide();
		$(this).find('span').html('+');
		return false;
	});
	
	
	
	
	// shorten long research people lists if more than 5
/*	var peopleCount = $('.people-list.curtailed li').size();
	if(peopleCount > 5) {
		$('.people-list.curtailed li').each(function(index) {
			if (index > 4) {
				$(this).addClass('extra').hide();
			}
		});
		
		// toggle additional entries
		$('.people-toggler').toggle(function() {
			$('.people-list.curtailed li.extra').show();
			$(this).find('span').html('-');
			return false;
		}, function() {
			$('.people-list.curtailed li.extra').hide();
			$(this).find('span').html('+');
			return false;
		});
	} else {
		// hide toggle link if < 5
		$('.people-toggler').hide();
	}
	
*/	
	// bigger targets for links
	$('.biggerlink').css('cursor','pointer').click(function(){
	     window.location=$(this).find('a').attr('href');
	     return false;
	});
	
	// bigger targets for nav links
	$('.sf-menu li').css('cursor','pointer').click(function(){
	     window.location=$(this).find('a').attr('href');
	     return false;
	});

	// select images with alt attribute and automatically add a caption
	$('img.add-caption').jcaption();

	// hide last span in category navs
	$('.catlist span:last-child').hide();
});