/*
 *  This was, umm, borrowed from jquery.js.  The width() function doesn't
 *  seem to be calculating properly for the hidden drop down menus.
 *  I think it has something to do with setting position: absolute, 
 *  visibility: hidden and display:block.
 */

function localGetWH(elem, name) {
	var which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
	var val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
	var padding = 0, border = 0;
	$.each( which, function() {
		padding += parseFloat($.curCSS( elem, "padding" + this, true)) || 0;
		border += parseFloat($.curCSS( elem, "border" + this + "Width", true)) || 0;
	});
	val -= Math.round(padding + border);
	return val;
}


$(document).ready(function(){
 	$('div#footerPanel div#additionalrelatedsitelinks').css('display', 'none');

	/*
	 * page.js - Positions the panels making up tke page.
	 */

	var margin = 8;
	var units = 'px';

	/*
	 * Grab the wrappers for the divs
	 */

	var pagePanel = $('div#pagePanel');
	var leftPanel = $('div#leftPanel');
	var rightPanel = $('div#rightPanel');
	var contentPanel = $('div#contentPanel');
	var contentHeadingPanel = $('div#contentHeadingPanel');
	var backgroundPanel = $('div#backgroundPanel');
	var bottomPanel = $('div#bottomPanel');
	var footerPanel = $('div#footerPanel');

	/*
	 * Center the content
	 */

	if($('html').width() > pagePanel.width()) {
		var left = ($('html').width() - pagePanel.width()) / 2;
		pagePanel.css('left', left + units);
	}
	
	/*
	 *  Adjust the height of the leftPanel by the height of
	 *	leftAccreditations.
	 */

	if(leftPanel.size()) {
		var leftAccreditations = $('div#leftAccreditations');
		if(leftAccreditations.size())
			leftPanel.height(leftPanel.height() + leftAccreditations.height());
	}

	/*
	 * Make the left panel and content panel the same height
	 */

	if(leftPanel.size()) {
		if(leftPanel.height() > contentPanel.height())
			contentPanel.height(leftPanel.height());
		else
			leftPanel.height(contentPanel.height());
	}

	/*
	 * If the contentHeadingPanel is present, center it's heading
	 */

	if(contentHeadingPanel.size()) {
		var chps = $('div#contentHeadingPanel > div');
		chps.css('left', contentPanel.position().left + ((contentPanel.width() - chps.width()) / 2) + units);
	}

	/*
	 * pageBottom is the running position of the page's bottom edge
	 */

	var pageBottom = contentPanel.position().top + contentPanel.height();

	/*
	 * If the backgroundPanel is present add a margin between
	 * it and the footerPanel (or bottomPanel).
	 */

	if(backgroundPanel.size()) {
		var backgroundPanelHeight = pageBottom - backgroundPanel.position().top + margin;
		backgroundPanel.height(backgroundPanelHeight + units);
		pageBottom += margin;
	}

	/*
	 * Setup the optional bottom panel.  This panel is positioned
	 * under the left and content panels only.
	 */

	if(bottomPanel.size()) {
		bottomPanel.css('top', (pageBottom + margin) + units);
		pageBottom += margin + bottomPanel.height();
	}

	/*
	 * Position the footerPanel under the left, content and right panels.
	 * If the rightPanel extends bolow the leftPanel or contentPanel,
	 * extend thoes panel's height to match the rightPanel.
	 */

	if(rightPanel.size()) {
		if(rightPanel.position().top + rightPanel.height() > pageBottom)
			pageBottom = rightPanel.position().top + rightPanel.height();
		if(bottomPanel.size() == 0) {
			if(leftPanel.size() && pageBottom > leftPanel.height())
				leftPanel.height(pageBottom - leftPanel.position().top);
			if(pageBottom > contentPanel.height())
				contentPanel.height(pageBottom - contentPanel.position().top);
		}
	}

	if(footerPanel.size()) {
		footerPanel.css('top', (pageBottom + margin) + units);
		pageBottom += margin + footerPanel.height();
	}

	pagePanel.height(pageBottom);

	/*
	 * Set up an event handler to toggle the hidden links section
	 */

 	$('div#footerPanel div#additionalrelatedsitelinks').hide();
 	var additionalrelatedsitelinksVisable = false;
	$('div#footerPanel').click(function(event) {
		if(event.target.tagName != 'A') {
 			if(additionalrelatedsitelinksVisable) {
				$('div#footerPanel div#additionalrelatedsitelinks').slideUp();
				additionalrelatedsitelinksVisable = false;
			}
			else {
				$('div#footerPanel div#additionalrelatedsitelinks').slideDown();
				additionalrelatedsitelinksVisable = true;
			}
		}
	});

	/*
	 * navButton support
	 */

	$('div.navButton').hover(function(){
		$('*', this).addClass('hover');
	},function(){
		$('*', this).removeClass('hover');
	});

	/*
	 * navMenu support
	 */

	$('.navMenu').hover(function(){
		$('ul', this).css('visibility', 'visible');
	},function(){
		$('ul', this).css('visibility', 'hidden');
	});

	/*
	 * CSS is set here because IE6's hover support is just so broken
	 */

	$('.navMenu ul li').hover(function(){
		$('a', this).css('color', '#000');
		$(this).css('cursor', 'pointer');
	},function(){
		$('a', this).css('color', '#fff');
		$(this).css('cursor', 'default');
	});
});
