var default_primary_nav_height = null;

Array.prototype.max = function(){
    return Math.max.apply( Math, this );
};

$('#primary-nav').ready(function(){
	default_primary_nav_height = $('#primary-nav').height();
	
	$('#primary-nav').live('mouseenter',function(){
		$('#primary-nav').stop(true);
		$('#primary-nav').animate({height: $('#primary-nav ul').height()});
	}).live('mouseleave',function(){
		$('#primary-nav').stop(true);
		$('#primary-nav').animate({height: default_primary_nav_height});		
	});
});

if($('.news-article').length > 0) {
	var article_heights = [];

	article_heights.push($('.news-article:eq(0)').height());
	article_heights.push($('.news-article:eq(1)').height());
	article_heights.push($('.news-article:eq(2)').height());
	
	$('.news-article:lt(3)').css('height',article_heights.max()+'px');
	
	var article_heights = [];

	article_heights.push($('.news-article:eq(3)').height());
	article_heights.push($('.news-article:eq(4)').height());
	
	$('.news-article:gt(3)').css('height',article_heights.max()+'px');
	
}

if($('.print a').length > 0) {
	$('.print a').click(function(){
		window.print();
		return false;
	})
}

if($('.feature').length > 0) {
	var num;
	var current;
	var next;
	var timeout;

	$('.feature-container').ready(function(){
		$('.feature-first').css('display','block');
		
		/*num = $('.feature').length;
		current = 0;

		$('.feature').css('display','none');
		$('.feature:eq('+current+')').fadeIn();
	
		timeout = setTimeout('nextFeature()',10000);*/
	});

	function nextFeature() {

		next = current + 1;
	
		if(next > (num-1)) {
			next = 0;
		}
	
		$('.feature:eq('+current+')').fadeOut();
		$('.feature:eq('+next+')').fadeIn();
	
		current = next;
		clearTimeout(timeout)
		timeout = setTimeout('nextFeature()',10000);
	}
	
	function backFeature() {

		next = current - 1;
		
		if(next < 0) {
			next = (num-1);
		}
	
		$('.feature:eq('+current+')').fadeOut();
		$('.feature:eq('+next+')').fadeIn();
	
		current = next;
		clearTimeout(timeout)
		timeout = setTimeout('nextFeature()',10000);
	}
	
	$('#feature-nav-back').click(backFeature);
	$('#feature-nav-next').click(nextFeature);
}

$(document).ready(function(){
	if($(".attachment a").length > 0){
		$('.attachment a').lightBox();
	}
	
	if($('.calendar-listing .day ul').length > 0) {
		var max_height = $('.calendar-listing .day ul:first').height();

		$('.calendar-listing .day ul li:first').addClass('first');
		$('.calendar-listing .day ul li:even').addClass('even');
		$('.calendar-listing .day ul li:odd').addClass('odd');

		$('.calendar-listing .day ul li').each(function(){
			var top = $(this).position().top;
			if(top > max_height) {
				$(this).parent().siblings('.morelink').show();
			}
		});
		
		$('.calendar-listing .day .morelink').live('click',function(){
			$(this).text('less')
			$(this).siblings('ul').find('.event_date').show();
			$(this).parent().parent().addClass('fullsize');
		});
		
		$('.calendar-listing .fullsize .morelink').live('click',function(){
			$(this).text('more')
			$(this).parent().parent().removeClass('fullsize');
		});
		
	}
	
})
