jQuery(document).ready(function ($) {
		
	// MAIN NAV DROPDOWNS ------------------------------------------------------- //
	
	$('#nav li a.drop').click(function(e) {
		e.preventDefault();
		var $sub = $(this).siblings('.sub-menu'),
				wasHidden = $sub.hasClass('hidden');
		if($sub.length) {
			hideMenu();
			if(wasHidden) {
				$(this).addClass('expanded');
				$sub.removeClass('hidden');
			}
		}
	});
	
	var menuFadeTimer;
	$('#nav li.menu-item')
		.bind('mouseenter', function() {clearTimeout(menuFadeTimer);})
		.bind('mouseleave', function() {
			menuFadeTimer = setTimeout(hideMenu, 1000);
		});
	
	function hideMenu() {
		$('#nav a.expanded').removeClass('expanded');
		$('#nav .sub-menu').addClass('hidden');
	}
	
	
	// SEARCH REPLACE ----------------------------------------------------------- //
	
	var $searchForm = $('<form action="/site/search" method="get" id="searchForm"><input type="hidden" name="s" value="y"><input type="text" name="q" id="searchBox" class="sans-serif" size="20" maxlength="100"></form>'),
			searchWidth = $('#menu-item-3.search a').width();
	
	$('#menu-item-3.search a').click(function(e) {
		e.preventDefault();
		
		hideMenu();
		
		if($(this).hasClass('searching')) {
			if($searchForm.find('#searchBox').val().length) {$searchForm.submit();} else {$searchForm.find('#searchBox').focus();}
		} else {
			$(this).addClass('searching').width('180px').before($searchForm);
			$searchForm.find('#searchBox').focus()
				.blur(function(e) {
					if(!$(this).val().length) {
						$('#menu-item-3.search a').removeClass('searching').width(searchWidth+'px');
						$searchForm.remove();
					}
				});
		}
	});
	
	
	// SHADOWED DIVS ------------------------------------------------------------ //
	
	$('.shadowed').not('.up').wrap('<div class="shadow">').removeClass('shadowed');
	$('.shadowed.up').wrap('<div class="shadow up">').removeClass('shadowed');	
	
	
	// ACCORDION ---------------------------------------------------------------- //
	
/*
	$('.accordion .trigger').css({'cursor':'pointer'});
	
	var fold = $('.accordion .fold:first'),
		origHeight = fold.show().height(),
		foldHeight;
		fold.addClass('expanded');
		$('.accordion .trigger:first').addClass('open');
	$('.accordion .trigger').click(function(ev) {
		fold = $(this).next();
		
		if(!fold.hasClass('expanded')) {
			origHeight = fold.siblings('.expanded').height();
			
			fold.siblings('.open').removeClass('open');
			fold.siblings('.expanded').animate({'height':0}, 200, function() {$(this).css({'display':'none','height':origHeight+'px'}).removeClass('expanded'); });
			
			foldHeight = fold.show().height();
			fold.hide();
			$(this).addClass('open');
			fold.css({'height':'0','display':'block'}).animate({'height':foldHeight+'px'}, 200).addClass('expanded');
		}
	});
*/
	
	
	// RESIZING CAPTION IMAGE CONTAINER ----------------------------------------- //
	
	$('.caption').each(function(){
		var imgWidth = $(this).find('img').width();
		$(this).width(imgWidth);
	});
	
	
	// LETTERS TO THE EDITOR ---------------------------------------------------- //

	$('#letters-to-the-editor-container').after('<div id="overlay"></div>');
	
	$('a[href=#letters-to-the-editor]').click(function(ev) {
		ev.preventDefault();
		$('#overlay').css({'opacity':'0'}).show().animate({'opacity':'1'}, 200);
		$('#letters-to-the-editor-container').css({'opacity':'0'}).show().animate({'opacity':'1'}, 200);
	});
	
	$('#letters-to-the-editor .cancel').click(function(ev) {
		ev.preventDefault();
		$('#letters-to-the-editor-container').hide();
		$('#overlay').animate({'opacity':'0'}, 200, function() {$(this).hide();});
	});
	
	$('#overlay').click(function() {
		$('#letters-to-the-editor-container').hide();
		$('#overlay').animate({'opacity':'0'}, 200, function() {$(this).hide();});
	});
	
	
	// ARTICLE ACTIONS ----------------------------------------------------------- //
	
	$('body.article #comments:not(.has-comments)').hide();
	
	$('.actions .discuss').click(function(e) {
		e.preventDefault();
		$('#comments').toggle();
	});
	
	$('a[href="#print"]').click(function(ev) {
		ev.preventDefault();
		if(window.print) window.print();
	});
	
	$('a.submit:not(.cancel)').click(function (e) {
		e.preventDefault();
		$(this).parents('form').first().submit();
	});
	
	
	// ARTICLE ORPHANS & WIDOWS ------------------------------------------------- //
	$('body.article #article .entry-content p').each(function(){$(this).html($(this).html().replace(/\s([^\s<]+)\s*$/,'&nbsp;$1'));});
	
	
	// ARTICLE EXPANDABLE FOOTNOTES --------------------------------------------- //
	var $expandable = $('body.article #article .expandable'),
			$expander = $expandable.find('.expander');
	
	if($expander.length > 0) {
		$expander
			.data('oHeight', $expander.outerHeight())
			.css({'height':'0px', 'opacity':'0'});
		$('body.article #article .expandable .trigger').click(function(e) {
			e.preventDefault();
			if($expander.height() == 0) {
				$expander.animate({'height':$expander.data('oHeight')+'px', 'opacity':'1'}, 300);
			} else {
				$expander.animate({'height':'0px', 'opacity':'0'}, 300);
			}
		});
	}
	
	
	// SIDEBAR PUSHER ----------------------------------------------------------- //
	
	if($('body').hasClass('article') && $('.sidebar-pusher').length) {
		
		var pusherPosTop = $('.sidebar-pusher').parent().position().top - 30,
			sidebarPosTop = $('#article-sidebar').position().top - 24;
			
		$(window).scroll(function() {
			if($(window).scrollTop() > pusherPosTop && $(window).scrollTop() < sidebarPosTop) {
				$('.sidebar-pusher').parent().removeClass('absolute').addClass('fixed').css({'top':'30px','left':($('.column.entry-list').position().left + 16)+'px'});
			} else if($(window).scrollTop() >= sidebarPosTop) {
				$('.sidebar-pusher').parent().removeClass('fixed').addClass('absolute').css({'top':(sidebarPosTop + 28)+'px', 'left':($('.column.entry-list').position().left + 16)+'px'});
			} else {
				pusherPosTop = $('.sidebar-pusher').parent().removeClass('fixed').position().top - 30;
				$('.sidebar-pusher').parent().removeClass('fixed').css({'left':'-4px', 'top':'0'});
			}
		});
		
		$(window).resize(function() {
			if($('.sidebar-pusher').parent().hasClass('fixed') || $('.sidebar-pusher').parent().hasClass('absolute')) {
				$('.sidebar-pusher').parent().css({'left':($('.column.entry-list').position().left + 16)+'px'});
			}
		});
	}
	
	
	// -------------------------------------------------------------------------- //
		
	
	
	
	// EFCA BANNER-ASK COOKIE [ BnrHlp ] ---------------------------------------- //
	
	
	// check user interaction with the banner
/*	
	
	if(readCookie('BnrHlpCookie') == null) {
		// if there is no BnrHlp cookie, show banner
		$('#help-banner').show();
	}
	
	// handle "view special message" click
	$('#view-message').click(function(evt) {
		evt.preventDefault();
		
		setBnrHlpCookie('view message');
		window.location = 'http://www.efca.org/about-efca/efca-growing-mindset/';
				
	});
	
	// handle "no thanks" click
	$('#hide-banner').click(function(evt) {
		evt.preventDefault();
		
		setBnrHlpCookie('no thanks');
		$('#help-banner').animate({'margin-top' : '-236px'}, 300);
		
	});
	
	// setCookie
	function setBnrHlpCookie(msg) {
		
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
				
		document.cookie = 'BnrHlpCookie='+msg+'; expires='+date.toGMTString()+'; path=/';
				
	}

	// read cookie	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	*/
	
	
});
