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

function maxHeight() {
	// account for the height of the absolutely positioned sidebar footers
	if($('.column.right .container:first-child').height()+ $('.column.right .container.sidebar-footer').height() > $('.column.left .container:first-child').height()+ $('.column.left .container.sidebar-footer').height()){
		// set footer promos position to right side height
		$('.column.right .container:first-child').height($('.column.right .container:first-child').height()+ $('.column.right .container.sidebar-footer').height());
		$('.column.right .container.sidebar-footer').css('margin-top', '-'+$('.column.right .container.sidebar-footer').height()+'px');
		$('.column.left .container:first-child').height($('.column.right .container:first-child').height());
		$('.column.left .container.sidebar-footer').css('margin-top', '-'+$('.column.left .container.sidebar-footer').height()+'px');
	} else {
		// set footer promos position to left side height
		$('.column.left .container:first-child').height($('.column.left .container:first-child').height()+ $('.column.left .container.sidebar-footer').height());
		$('.column.left .container.sidebar-footer').css('margin-top', '-'+$('.column.left .container.sidebar-footer').height()+'px');
		$('.column.right .container:first-child').height($('.column.left .container:first-child').height());
		$('.column.right .container.sidebar-footer').css('margin-top', '-'+$('.column.right .container.sidebar-footer').height()+'px');
	}
	var heights = new Array();
	$(".twocolumn .column .box .container, .threecolumn .column .box .container").each(function(i) {
		heights[i] = $(this).height();
	});
	return heights.max();
}
function setHeights() {
	var h = maxHeight() + 20; 
	$(".twocolumn .column .box, .threecolumn .column .box").each(function(i) {$(this).css("height",h+"px");});
}
function adjustHeight() {
	var a = $("#advanced").height()/1; 
	$(".twocolumn .middle .box, .threecolumn .middle .box").each(function(i) {
		var h = $(this).height(); 
		var b = h-(-a); 
		$(this).css("height",b+"px"); 
		setHeights();
	});
}

$(document).ready(function(){
	// drop shadows
	$(".box").append('<div class="corner tl"></div><div class="corner tr"></div><div class="btm"></div><div class="corner br"></div><div class="corner bl"></div>');
	
	// add class for IE6
	$('input[type="text"],input[type="password"]').addClass("text");
	$(":last-child").addClass("last-child");
	
	// external links
	$('a[rel="external"]').attr("target","_blank");
	$('a[href^="http://"], a[href^="https://"]').each(function() {
		var href = $(this).attr("href");
		var images = $(this).find("img").length;
		if(href.indexOf("http://www.igsb.org/")==-1 && images==0) {
			$(this).addClass("external");
			$(this).attr("target","_blank");
		}
		if(images>0) {$(this).attr("target","_blank");}
	});

	// header search form
	$("#query").focus(function() {if($(this).val()==this.defaultValue) {$(this).val(""); $(this).addClass("focus");}});
	$("#query").blur(function() {if($(this).val()=="") {$(this).val("Search..."); $(this).removeClass("focus");}});
	$("#header form").submit(function() {if($("#query").val()=="" || $("#query").val()=="Search...") {alert("You must enter a search term."); $("#query").focus(); return false;} else {return true;}});
	
	// advanced search form
	$("#searchform").submit(function() {if($("#keywords").val()=="" || $("#keywords").val()=="Search...") {alert("You must enter a search term."); $("#keywords").focus(); return false;} else {return true;}});
	
	// search results
	$("div.result").hover(function() {$(this).addClass("hover");},function() {$(this).removeClass("hover");});
	$("div.result").click(function() {var url = jQuery(".url a",this).attr("href"); window.location=url;});
	$("#refine").click(function() {$("#advanced").slideDown(500); setTimeout("adjustHeight();", 500); $("#refine").unbind("click"); return false;});
});

$(window).load(function(){
	// fix "stubbies"
	setHeights();
});
