// JavaScript Document

var button_image;
var sidebar_left;
var center;
var sidebar_right;
var sidebar_right_last;

function show_sub(element) {
	$(element).addClass("hover").children("ul.sub").show();
}

function hide_sub(element) {
	$(element).removeClass("hover").children("ul.sub").hide();
}

function adjust_heights() {
	
	sidebar_left_height = sidebar_left.height();
	sidebar_right_height = sidebar_right.height();
	target_height = Math.max(sidebar_left_height, center.height(), sidebar_right_height) + 20;
	if (sidebar_left_height < target_height) {
		sidebar_left.height(target_height);
	}
	/*
	if (sidebar_right_height < target_height) {
		sidebar_right_last.height(target_height - sidebar_right_height + sidebar_right_last.height() - 1);
	}*/
}

$(document).ready(function() {
	
	// initialize variables
	sidebar_left = $("#sidebar_left");
	center = $("#center");
	initial_center_height = center.height();
	sidebar_right = $("#sidebar_right");
	sidebar_right_last = $("#sidebar_right div.sidebar_item:last");
	// adjust heights of left and right sidebars
	adjust_heights();
	
	// show/hide of sub-links of Primary Navigation
	$("#primary_links li.has_sub").hover(
		function() { show_sub(this); },
		function() { hide_sub(this); }
	);
	
	// hover button images
	$(".button_image").hover(
		function() {
			button_image = $(this).attr("src");
			$(this).attr("src", button_image.replace(".","_hover."));
		},
		function() {
			$(this).attr("src", button_image);
		}
	);
	// highlight query text
	$("#search_query").focus(function() {
		$(this).val("");								  
	});
	
	// re-check center height for variable image heights
	$(window).load(function () {
		if ($("#center").height() != initial_center_height) {
      		adjust_heights();
		}
    });
});
