/*
 * Copyright (c) 1997-2009, Ron Lussier. All rights reserved.
 *
 * This software is an unpublished work subject to a confidentiality agreement
 * and protected by copyright and trade secret law. Unauthorized copying,
 * redistribution or other use of this work is prohibited. All copies must
 * retain this copyright notice. Any use or exploitation of this work without
 * authorization could subject the perpetrator to criminal and civil liability.
 *
 * The above copyright notice does not indicate actual or intended publication
 * of this source code.
 */

/*---------------------------------------------------------------------------------------------------------
 * This file contains code that augments HTML at load time to enhance functionality.
 *---------------------------------------------------------------------------------------------------------*/


if (typeof EB === "undefined") {
	/**
	 * The EARTHENBERM global namespace object.
	 */
	var EB = {};
}

if (typeof EB.augments === "undefined") {
	/**
	 * The EARTHENBERM augmentations global namespace object.
	 */
	EB.augments = {};
	}

/**
 * Find context-sensitive help links and augment them to work properly.
 *
 * These links are in the format:
 *     <... class='contextHelpLink'>[<a href='PageContextHelpTest'>?</a>]</...>
 * 
 * The href attribute specifies the ID of a block of HTML that should be put into the help
 * popup.  It may also specify a help box width using the following format:
 *     href='PageContextHelpTest;300'
 *
 * Normally the default width should be used.
 */
EB.augments.augmentContextHelp = function() {
	var		helpNodes = EB.dom.getElementsByClass('contextHelpLink');
	
	if (helpNodes && helpNodes.length) {
		for (var loop = 0; loop < helpNodes.length; loop++) {
			var		thisNode = helpNodes[loop];

			/*
			 * Find anchors and augment them.
			 */
			var		anchors = thisNode.getElementsByTagName('a');
			
			if (anchors && anchors.length) {
				for (var aLoop = 0; aLoop < anchors.length; aLoop++) {
					var		anchor = anchors[aLoop];
					var		href = anchor.href;
					var		lastSlashIndex = href.lastIndexOf('/');
					var		lastHashIndex = href.lastIndexOf('#');
					var		lastIndex = Math.max(lastSlashIndex, lastHashIndex);

					if (lastIndex >= 0) {
						href = href.substr(lastIndex + 1);
					}

					var		parts = href.split(';');

					if (parts && (parts.length > 0)) {
						var		helpContentsId = parts[0];
						var		helpPanelWidth = 250;

						if (parts.length > 1) {
							helpPanelWidth = parts[1];
						}

						/*
						 * Bundle together the help parameters with the function that puts up the help.
						 */
						anchor.href='javascript:none()';
						anchor.onclick = EB.augments.makeContextHelpFunction(thisNode, helpContentsId,
																				helpPanelWidth);
					}
				}
			}
		}
	}
}

EB.augments.makeContextHelpFunction = function(node, contentsId, width) {
	return function()
				{
					EB.dialogManager.showHelp(node, contentsId, width);
				};
};

