$(document).ready(function(){
$("table.styled tr:nth-child(odd)").addClass("alt");  // Add alt class to alternating rows on styled tables
$(".table-container td a").parent().addClass("event"); // Add event class to month view 
$(".table-container td h4").parent().parent().parent().addClass("event"); // Add event class to month view
if($("#gallery-form #select-gallery").children().length <= 2) { $(".gallery-teaser form").addClass("none"); $(".gallery select").addClass("none"); }
if($(".gallery-attachment dl.image-list dt").length > 1){$(".gallery-attachment .gallery-directional").removeClass("none");}

$('<div class="module-divider">&nbsp;</div>').insertAfter(".sc .module h2"); // add module divider to sc

$(".gallery-attachment dl.image-list dt:first").addClass("active");
$(".gallery-attachment dl.image-list dd:first").addClass("active");
$(".gallery-attachment dl.image-list dt:not(.active)").addClass("none");
$(".gallery-attachment dl.image-list dd:not(.active)").addClass("none");
	
	var changeButtons = function(){
		// Change display state of buttons
		// If not first child or last child: display next and prev
		if( !$(".gallery-attachment dl.image-list dt:first").hasClass("active") && !$(".gallery-attachment dl.image-list dt:last").hasClass("active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").removeClass("disabled");
		} 
		// If first child: display next, disable prev
		if( $(".gallery-attachment dl.image-list dt:first").is(".active") ){
			$(".gallery-next a").removeClass("disabled");
			$(".gallery-previous a").addClass("disabled");
		} 
		// If last child: display prev, disable next
		if( $(".gallery-attachment dl.image-list dt:last").is(".active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").addClass("disabled");
		}
		
		SI_clearFooter();
	}
	
	var updateImages = function(){
		$(".gallery-attachment dl.image-list dt.active").addClass("none");
		$(".gallery-attachment dl.image-list dd.active").addClass("none");
		$(".gallery-attachment dl.image-list dt.active").removeClass("active");
		$(".gallery-attachment dl.image-list dd.active").removeClass("active");
	}
	
	$(".gallery-next a").click(function(){	
		if(!$(this).hasClass("disabled")){
			var nextImg = $(".gallery-attachment dl.image-list dd.active");
			updateImages();
			nextImg.next().removeClass("none");
			nextImg.next().next().removeClass("none");
			nextImg.next().addClass("active");
			nextImg.next().next().addClass("active");
			changeButtons();
		}
	});
	$(".gallery-previous a").click(function(){		
		if(!$(this).hasClass("disabled")){
			var prevImg = $(".gallery-attachment dl dt.active");
			updateImages();
			prevImg.prev().addClass("active");
			prevImg.prev().prev().addClass("active");
			prevImg.prev().removeClass("none");
			prevImg.prev().prev().removeClass("none");
			changeButtons();
		}
	});
	
	/* Add alt class to gallery thumbnails - Not being used yet */
	$(".gallery-thumbnails ul li:odd").addClass("alt");
	
	/* 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').each(function(){ 
				if($(this).outerHeight() > max_height){ max_height = $(this).outerHeight(); }
			}).css('height', max_height + 'px');
		});
	}*/
	
	// 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:has(img)").height(0);
        
        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).height() < min_height) {
                min_height = $(this).height();
            }
        });

        $(".gallery .grid .images .thumb a").css("height", min_height + 'px');
		
		// Take care of the files in a grid view and set to same width as images
		var img_width = $(".gallery .grid .images .thumb img:eq(0)").width();
		$(".gallery .grid .images .thumb a.file span").css("width", img_width + '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).height() > max_height) {
                max_height = $(this).height();
            }
        }).css('height', max_height + 'px');
    }
	
	// Take care of the files in a grid view and set to same width as images
	// This should apply to both list and grid
	var img_width = $(".gallery .images .thumb img:eq(0)").width();
	$(".gallery .images .thumb a.file span").css("width", img_width + 'px');


	
});
