/*
 * Drop down sections
 */

function sectionHeight(sectionObj) {
	var windowHeight = $(window).height();
	var sectionHeight = sectionObj.next().outerHeight() + 40;

	// calculate whether section should grow to window height or taller if there is not enough room
	if (sectionHeight < windowHeight) {
		return windowHeight;
	}
	else {
		return sectionHeight;
	}
}

$(document).ready(function() {
		
	$('#menu-main > li > a').click(function() {
		
		// if clicking on currently open section then don't attempt to close any others
		if ($(this).hasClass('down')) {
			$(this).removeClass('down').addClass('up').parent().animate({
				height: '34px'
			}, 700);
		}
		else {
		
			// move up any sections that are down
			$('#menu-main > li > a.down').removeClass('down').addClass('up').parent().animate({
				height: '34px'
			}, 700);
			
			$(this).removeClass('up').addClass('down');
			
			$(this).parent().animate({
				height: sectionHeight($(this))
			}, 700);
		}

	});
	
	$(window).resize(function() {
		$('a.down').parent().height(sectionHeight($('a.down')));
	});
	
});
