$(document).ready(function(){

/* Switch out image used in Google Search input */
$("#header .search input#q").css("background-position","left top");
$("#header .search input#q").focus(function(){
	$(this).css("background-position","left bottom");
});
$("#header .search input#q").blur(function(){
	if($(this).val() == ""){
		$(this).css("background-position","left top");
	}
});

if($(".pc h1 span").length == 0) {	$('.pc h1').wrapInner("<span></span>"); } // Apply needed container to h1 if not there already
if($(".sc div.content div.inner, .sc div.module div.inner").length == 0) { $('.sc div.content, .sc div.module').wrapInner("<div class=\"inner\"></div>"); }

$(".sc div.module:even div.inner").prepend("<span class=\"paperclip\"></span>");

var randomheader = ['blue','green','pink'];
$(".sc .content").randomClass(randomheader, {randomness: 'successive'});


$(".tab-row ul li a").wrapInner("<span></span>");  // puts a span tag in anchors in the tabs

	$("table.styled tr: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($("#select-gallery").children().length <= 2){ $(".gallery-teaser form").addClass("none"); $(".gallery select").addClass("none"); }
	if($(".attachment dl.image dt").length > 1){$(".attachment .gallery-directional").removeClass("none");}
	
	
	
	$(".attachment dl.image dt:first").addClass("active");
	$(".attachment dl.image dd:first").addClass("active");
	$(".attachment dl.image dt:not(.active)").addClass("none");
	$(".attachment dl.image dd:not(.active)").addClass("none");
	
	$(".attachment dl.image dd").each(function(){ if($.trim($(this).text()) == "") $(this).hide();});
	
	var changeButtons = function(){
		// Change display state of buttons
		// If not first child or last child: display next and prev
		if( !$(".attachment dl.image dt:first").hasClass("active") && !$(".attachment dl.image dt:last").hasClass("active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").removeClass("disabled");
		} 
		// If first child: display next, disable prev
		if( $(".attachment dl.image dt:first").is(".active") ){
			$(".gallery-next a").removeClass("disabled");
			$(".gallery-previous a").addClass("disabled");
		} 
		// If last child: display prev, disable next
		if( $(".attachment dl.image dt:last").is(".active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").addClass("disabled");
		}
		
		SI_clearFooter();
	}
	
	var updateImages = function(){
		$(".attachment dl.image dt.active").addClass("none");
		$(".attachment dl.image dd.active").addClass("none");
		$(".attachment dl.image dt.active").removeClass("active");
		$(".attachment dl.image dd.active").removeClass("active");
	}
	
	$(".gallery-next a").click(function(){	
		if(!$(this).hasClass("disabled")){
			var nextImg = $(".attachment dl.image 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 = $(".attachment dl dt.active");
			updateImages();
			prevImg.prev().addClass("active");
			prevImg.prev().prev().addClass("active");
			prevImg.prev().removeClass("none");
			prevImg.prev().prev().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').each(function(){ 
				if($(this).outerHeight() > max_height){ max_height = $(this).outerHeight(); }
			}).css('height', max_height + 'px');
		});
	}
	
});
