$(function () {	
	// Adjust tab bar behavior on click
	$('#tab-bar li a').click(function () {
		$('#tab-bar li').removeClass('active');
		$(this)
			.closest('li')
			.addClass('active');
		$('.tab-content').hide()
		$('.tab-content[id=' + $(this).attr('class') + ']').show();
		
		return false;
	});
	
	// Set Tab behavior for when you load the page (comment one out)
	$('.in_the_news').click();
	//$('.calendar_of_events').click();
	
	// News >> Accordion
	$('#in_the_news ul.news li p').each(function () {
		if($(this).text().length > 170) {
			$(this).text($(this).text().substring(0, 170) + '... ');
		}
		$(this).append($(this).siblings('a'));
		$(this).add($(this).siblings('h6')).wrapAll('<div />');
	});
	
	if($('#in_the_news ul.news li p').length === $('#in_the_news ul.news li p').length) {
		$('#in_the_news ul.news li')
			.unwrap()
			.find('div')
				.unwrap();
		$('#in_the_news div.news-list').accordion();
	}
	
	
	// adjusts the calendar list at the page bottom
	//$('.calendar-list dl dd').each(function () {
	//		if($(this).prev().get(0).nodeName != 'DT') {
	//			$(this).before($(this).closest('dl').find('dt:first').clone());
	//		}
	//	});
	//	
	//	$('dl.calendar-day dt')
	//		.unwrap()
	//		.each(function () {
	//			$(this)
	//				.add($(this).next())
	//				.wrapAll('<dl class="calendar-day"></dl>');
	//		});
	
	$('dl.calendar-day').wrapAll('<div id="calendar-list"></div>').group({
		'size': '3',
		'wrapElem': '<div class="calendar-group"></div>'
	});
	
	$('dl.calendar-day:first-child').addClass('first-child');
	$('div.calendar-group').each(function () {
		$(this)
			.find('dl.calendar-day')
			.eq($(this).find('dl.calendar-day').length-1)
				.addClass('last-child');
	});
	$('dl.calendar-day.first-child.last-child')
		.removeClass('first-child last-child')
		.addClass('only-child');
		
	$('dl.calendar-day dd').attr('class','');
	
	//slideshow modifications
	$('#slideshow ul.news').cycle({
		fx: 'fade',
		timeout: 9001,
		speed: 1575,
		pager: $('#cycle_pager'),
		pause: 0,
		pauseOnPagerHover: 1,
		before: setCaption,
		random: 1
	});
	
	$('#caption_control a').click(function () {
		if(!$('#cycle_bar:animated').length) {
			if($(this).hasClass('show')) {
				$(this).text('Hide');
				$('#cycle_bar').animate({
				    bottom: 0
				}, 1000);
			} else {
				$(this).text('Show');
				$('#cycle_bar').animate({
				    bottom: -55
				}, 1000);
			}
			$(this).toggleClass('show hide');
			return false;
		}
	});
	
	// sidebar button classing:
	$('#buttons ul li > h5').each(function () {
		$(this)
			.closest('li')
			.addClass($(this).html())
			.addClass('icon');
		$(this).closest('li').prepend('<span></span>');
		$(this).remove();
	});
});

function setCaption(currSlideElement, nextSlideElement, options, forwardFlag) {
	$('#slide_caption').html($(nextSlideElement).find('p').html());
}
