//*****************************************************************************
// Common script code, should be included on all pages.
//*****************************************************************************

//=============================================================================
// Module: easternTimeDisplay
//
// Maintains the current Eastern time zone date and time display shown on the
// page header.
//=============================================================================
var easternTimeDisplay = function () {

	var easternTimeEl  = null;
	var easternTime    = null;
	var localTime      = new Date();
	var timeDifference = 0;

	var dayNames   = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
	var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

	//-------------------------------------------------------------------------
	// Initializes the display.
	//-------------------------------------------------------------------------
	function init() {

		// Get the Eastern time display element, exit if not found.
		easternTimeEl = document.getElementById("easternTime");
		if (easternTimeEl == null)
			return;

		// Get the current Eastern time from the server. Once retrieved, start
		// updating the display.
		var xhr = new XHR();
		xhr.url = "getEasternTime.asp?random=" + Math.floor(Math.random() * 10000000);
		xhr.successCallback = function (reqObj) {

			// Create a Date object from that string and calculate the
			// difference between it and the client's current time.
			easternTime = new Date(reqObj.responseText);
			timeDifference = easternTime.valueOf() - localTime.valueOf();

			// Update the display now and set an interval timer to keep it
			// updated.
			update();
			setInterval(update, 500);
		}
		xhr.get();
	}

	//-------------------------------------------------------------------------
	// Updates the display.
	//-------------------------------------------------------------------------
	function update() {

		// Determine Eastern time based on the client's current time.
		var currentTime = new Date();
		easternTime = new Date(currentTime.valueOf() + timeDifference);

		// Format the date and time.
		var hrs = easternTime.getHours();
		var mins = easternTime.getMinutes();
		var secs = easternTime.getSeconds();
		var meridiem = "am";
		if (hrs >= 12 && hrs <= 23)
			meridiem = "pm";
		if (hrs == 0)
			hrs = 12;
		if (hrs > 12)
			hrs -= 12;
		if (mins < 10)
			mins = "0" + mins;
		if (secs < 10)
			secs = "0" + secs;
		var s = dayNames[easternTime.getDay()] + ", "
			+ monthNames[easternTime.getMonth()] + " " + easternTime.getDate() + ", "
			+ easternTime.getFullYear()
			+ " " + hrs + ":" + mins + ":" + secs + " " + meridiem;

		// Clear any current content.
		while (easternTimeEl.firstChild != null)
			easternTimeEl.removeChild(easternTimeEl.firstChild);

		// Update the display.
		easternTimeEl.appendChild(document.createTextNode(s));
	}

	//=========================================================================
	// Initialization code.
	//=========================================================================

	// Initialize on page load.
	domUtils.onready(init);
}();

//=============================================================================
// Module: siteMenu
//
// Handles the site menu.
//=============================================================================
var siteMenu = function () {

	// The site menu element.
	var siteMenuEl = null;

	// The currently active sub menu.
	var activeSubMenu = null;

	//-------------------------------------------------------------------------
	// Initialize the site menu.
	//-------------------------------------------------------------------------
	function init() {

		// Get the site menu element, exit if not found.
		siteMenuEl = document.getElementById("siteMenu");
		if (siteMenuEl == null)
			return;

		// Process each item within the site menu.
		var rowEl = siteMenuEl.rows[0];
		var linkEl, subMenuEl;
		for (var i = 0; i < rowEl.cells.length; i++) {

			// Scan the table cell for a link and a sub menu. Note that not all
			// links will have a sub menu.
			var cellEl = rowEl.cells[i];
			linkEl = null;
			subMenuEl = null;
			for (var j = 0; j < cellEl.childNodes.length; j++) {
				var node = cellEl.childNodes[j];
				if (linkEl == null && node.tagName == "A")
					linkEl = node;
				if (subMenuEl == null && node.tagName == "DIV")
					subMenuEl = node;
			}

			// If a link was found, set it up.
			if (linkEl != null) {

				// Set the link up according to whether it has a sub menu or
				// not.
				if (subMenuEl == null) {

					// Set up event handling for the link.
					linkEl.onfocus = closeSubMenu;
				}
				else {

					// Add a style class to the link.
					domUtils.addClass(linkEl, "hasSubMenu");

					// Initialize the sub menu.
					initSubMenu(linkEl, subMenuEl);

					// Set up event handling for the link.
					linkEl.onfocus     = openSubMenu;
					linkEl.onmouseover = openSubMenu;
				}
			}
		}
	}

	//-------------------------------------------------------------------------
	// Event handler for the page.
	//-------------------------------------------------------------------------
	function pageMousedown(e) {

		// If there is no currently active sub menu, exit.
		if (activeSubMenu == null)
			return;

		// Find the node that triggered the event.
		var node = (domUtils.isIE ? window.event.srcElement : e.target);

		// If the triggering node is part of the site menu, exit.
		if (domUtils.contains(siteMenuEl, node))
			return;

		// Close the currently active menu.
		closeSubMenu();
	}

	//-------------------------------------------------------------------------
	// Event handler for top-level menu links.
	//-------------------------------------------------------------------------
	function menuLinkMouseout(e) {

		var current, related;

		try {
			if (window.event) {
				current = this;
				related = window.event.toElement;
			}
			else {
				current = e.currentTarget;
				related = e.relatedTarget;
			}

			// If the mouse has moved off the link but not onto the sub menu,
			// close it.
			if (!domUtils.contains(current, related) && !domUtils.contains(activeSubMenu, related))
				closeSubMenu();
		}
		catch (ex) {}
	}

	//-------------------------------------------------------------------------
	// Event handlers for sub menus.
	//-------------------------------------------------------------------------
	function subMenuMouseout(e) {

		var current, related;

		try {
			if (window.event) {
				current = this;
				related = window.event.toElement;
			}
			else {
				current = e.currentTarget;
				related = e.relatedTarget;
			}

			// If the mouse has moved off the sub menu, close it.
			if (current != related && !domUtils.contains(current, related))
				closeSubMenu();
		}
		catch (ex) {}
	}

	function subMenuClick(e) {

		// Find the element that triggered the event.
		var el = (domUtils.isIE ? window.event.srcElement : (e.target.tagName ? e.target : e.target.parentNode));

		// If it is not a link, exit.
		if (el.tagName != "A")
			return;

		// Close the sub menu.
		closeSubMenu();
	}

	//-------------------------------------------------------------------------
	// Initializes a sub menu.
	//-------------------------------------------------------------------------
	function initSubMenu(linkEl, subMenuEl) {

		// Set object references.
		linkEl.subMenuEl = subMenuEl;
		subMenuEl.linkEl = linkEl;

		// Make sure the sub menu is at least as wide as its parent link
		// (plus 10%).
		var lw = linkEl.offsetWidth;
		var mw = subMenuEl.offsetWidth;
		if (mw < lw)
			subMenuEl.style.width = Math.round(1.1 * lw) + "px";

		// Set event handlers for the sub menu and it's opener link.
		subMenuEl.onmouseout = subMenuMouseout;
		subMenuEl.onclick    = subMenuClick;
		linkEl.onmouseout    = menuLinkMouseout;
	}

	//-------------------------------------------------------------------------
	// Opens a sub menu.
	//-------------------------------------------------------------------------
	function openSubMenu(e) {

		// Get the sub menu associated with this link.
		var subMenuEl = this.subMenuEl;

		// If that sub menu is the currently active one, exit.
		if (activeSubMenu != null && activeSubMenu == subMenuEl)
			return false;

		// Highlight the link.	
		domUtils.addClass(this, "isActive");

		// Close any currently active sub menu.
		closeSubMenu();

		// Position the sub menu just below the link and make it visible.
		var pt = domUtils.offset(this);
		subMenuEl.style.left = pt.x + "px";
		subMenuEl.style.top  = (pt.y + this.parentNode.offsetHeight) + "px";
		subMenuEl.style.visibility = "visible";

		// Mark this sub menu as the currently active one.
		activeSubMenu = subMenuEl;

		return false;
	}

	//-------------------------------------------------------------------------
	// Closes the currently active sub menu.
	//-------------------------------------------------------------------------
	function closeSubMenu() {

		// Exit if there is no active sub menu.
		if (activeSubMenu == null)
			return;

		// Unhighlight the owner link.	
		domUtils.removeClass(activeSubMenu.linkEl, "isActive");

		// Make the active sub menu invisible.
		activeSubMenu.style.visibility = "";

		// Clear the active sub menu.
		activeSubMenu = null;
	}

	//=========================================================================
	// Initialization code.
	//=========================================================================

	// Set event capturing on the page.
	if (document.documentElement.addEventListener)
		document.documentElement.addEventListener("mousedown", pageMousedown, true);
	else if (document.documentElement.attachEvent)
		document.documentElement.attachEvent("onmousedown", pageMousedown);

	// Initialize on page load.
	domUtils.onready(init);
}();

//=============================================================================
// Code to fix browser-specific issues.
//=============================================================================

if (domUtils.isIE && document.documentMode == null) {
	domUtils.onready(function () {

		//---------------------------------------------------------------------
		// For IE versions prior to IE 8, fix the problem with tables that have
		// an explicit style width of "100%" being cut off (but only for tables
		// with class "main").
		//---------------------------------------------------------------------
		var elList = document.getElementsByTagName("TABLE");
		for (var i = 0; i < elList.length; i++) {
			if (domUtils.hasClass(elList[i], "main") && elList[i].style.width == "100%")
				elList[i].style.width = "99%";
		}
	});
}

if (domUtils.isSafari) {
	domUtils.onready(function () {

		//---------------------------------------------------------------------
		// For Safari, change text inputs with a size of "2" to "4".
		//---------------------------------------------------------------------
		var elList = document.getElementsByTagName("INPUT");
		for (var i = 0; i < elList.length; i++) {
			if (elList[i].type == "text" && elList[i].size == "2")
				elList[i].size = "4";
		}
	});
}

//=============================================================================
// Code to adjust the width of specified DIVs to fit the current display.
//=============================================================================
domUtils.onready(function () {

	// Find DIVs with the special class name and set to zero width.
	var elList = document.getElementsByTagName("DIV");
	for (i = 0; i < elList.length; i++)
		if (domUtils.hasClass(elList[i], "adjustWidth"))
			elList[i].style.width = "0px";

	// Find the width of the widest element in the main wrapper.
	var el = document.getElementById("mainWrapper");
	el = el.rows[0].cells[0];
	var maxWidth = 0;
	for (var i = 0; i < el.childNodes.length; i++) {
		var width = el.childNodes[i].offsetWidth;
		if (width != null && width > maxWidth)
			maxWidth = width;
	}

	// Resize those DIVs.
	var elList = document.getElementsByTagName("DIV");
	for (i = 0; i < elList.length; i++)
		if (domUtils.hasClass(elList[i], "adjustWidth")) {

			// Make the DIV the same width as the widest element.
			elList[i].style.width = maxWidth + "px";
			var dw = elList[i].offsetWidth - maxWidth;
			elList[i].style.width = (maxWidth - dw) + "px";
		}
});


