$(function () {
	var $galPrevLink = $('.gallery-previous a');
	var $galNextLink = $('.gallery-next a');
	var $galImageList = $('.gallery-attachment dl.image-list');
	var $calMonthView = $('.table-container td');
	
	function updateImages() {
		$galImageList
			.find('dt:first, dd:first')
			.addClass('active')
		.end()
			.find('dt:not(.active), dd:not(.active)')
			.addClass('none');
	}
	
	function changeButtons() { // Change display state of buttons
		// If not first child or last child: display next and prev
		if(!$galImageList.find('dt:first').hasClass('active') && !$galImageList.find('dt:last').hasClass('active')) {
			$galPrevLink.add($galNextLink).removeClass('disabled');
		} 
		
		// Prevents prev on image #1 or next on last image
		if($galImageList.find('dt:first').is('.active')) {
			$galPrevLink.removeClass('disabled');
			$galNextLink.addClass('disabled');
		} else if($galImageList.find('dt:last').is('.active')) {
			$galPrevLink.removeClass('disabled');
			$galNextLink.addClass('disabled');
		}
		SI_clearFooter();
	}
	
	// Add .alt to every other row on styled tables and image thumbnails in galleries
	$('table.styled tr:odd, .gallery-thumbnails ul li:odd').addClass('alt'); 
	$calMonthView.find('a').closest('td').addClass('event'); // Add .event to days on calendar in month view
	$calMonthView.find('h4').closest('td').addClass('event'); // Add event class to month view 
	/*($('select#select-gallery option').length < 2) ? $('.gallery-teaser form, .gallery select').addClass('none') : ;
	($galImageList.find('dt').length > 1) ? $('.gallery-attachment .gallery-directional').removeClass('none') : ;*/
	updateImages();
	$galNextLink.click(function () {	
		if(!$(this).hasClass('disabled')) {
			var $nextImg = $galImageList.find('dd.active');
			updateImages();
			$nextImg
				.next()
					.addClass('active')
					.removeClass('none')
					.next()
						.removeClass('none')
						.addClass('active');
			changeButtons();
		}
	});
	$galPrevLink.click(function () {		
		if(!$(this).hasClass('disabled')) {
			var $prevImg = $('.gallery-attachment dl dt.active');
			updateImages();
			$prevImg
				.prev()
					.addClass('active')
					.removeClass('none')
					.prev()
						.addClass('active')
						.removeClass('none');
			changeButtons();
		}
	});
	
	// Preload images and set containing element to smallest / largest height
	if($(".module .grid").length > 0 && $(".module .grid ul li").length > 1){
		$(".gallery .grid .images .thumb a").height(0);
		$(window).bind('load', function() { 
			var min_height = 0;
			var max_height = 0;
			// Hide images and then loop through all the images to get the smallest height
			$(".gallery .grid .images img").each(function(i) {
				if(i==0){ min_height = $(this).outerHeight();}
				if($(this).outerHeight() < min_height){ min_height = $(this).outerHeight(); }
			});
			$(".gallery .grid .images .thumb a").css("height",min_height + 'px');
			// Now get the maximum height of every li to make them all the same
			$('.gallery .grid .images li, .calendar .grid dl').each(function(){ 
				if($(this).outerHeight() > max_height){ max_height = $(this).outerHeight(); }
			}).css('height', max_height + 'px');
		});
	}

});
