
var activeMenu, activeLink;

/* Create a name space for oldCape JavaScript so that we don't step on any third
 * party libraries. */

var OLDCAPE = {
	utils : {}
};

OLDCAPE.utils.showMenu = function(menuID)
{
	
	var menuToShow = OLDCAPE.utils.getObject(menuID + "Menu");
	var linkToSelect = OLDCAPE.utils.getObject(menuID + "Link");
	
	if (menuToShow == null || linkToSelect == null)
	{
		OLDCAPE.utils.debug("Either the menu or link is missing for " + menuID);
	}
	
	if (menuToShow == activeMenu)
	{
		return;
	}
	
	if (activeMenu == null)
	{
		OLDCAPE.utils.toggleDisplay(menuToShow);
		OLDCAPE.utils.toggleLink(linkToSelect);
	}
	else
	{
		OLDCAPE.utils.toggleDisplay(menuToShow, activeMenu);
		OLDCAPE.utils.toggleLink(linkToSelect, activeLink);
	}
	
	activeMenu = menuToShow;
	activeLink = linkToSelect;
	
};

OLDCAPE.utils.toggleLink = function()
{
	for (var i = 0; i < arguments.length; i++)
	{
    	
		var obj = OLDCAPE.utils.getObject(arguments[i]);
		
		if (obj == null)
		{
			OLDCAPE.utils.debug("Could not find object with ID " + arguments[i] + " in the page.");
		}
		
		if (obj.className == "selected")
		{
			obj.className = "";
		}
		else
		{
			obj.className = "selected";
		}
		
	}
};


// Returns the computed style for the element.

// NOTE: Safari contains a bug which causes it to return null if the element (or
// any parent element) is hidden. We do not attempt to normalize this because
// the caller may be checking for the display property.

OLDCAPE.utils.getActiveStyle = function(obj)
{
  	
	// Ensure that we have an object and not just the ID of an HTML element.
	
	obj = OLDCAPE.utils.getObject(obj);
	
	if (obj.currentStyle)
	{
		// Internet Explorer
		return obj.currentStyle;
	}
	else if (document.defaultView && document.defaultView.getComputedStyle)
	{
		// Mozilla/FireFox/Safari
		return document.defaultView.getComputedStyle(obj, null);	
	}
	else
	{
		// Fall back to non-computed style.	
		return obj.style;
	}
  	
};

OLDCAPE.utils.getActiveStyleProperty = function(obj, property)
{
	
	// Ensure that we have an object and not just the ID of an HTML element.
	
	obj = OLDCAPE.utils.getObject(obj);
	
	// Get the computed style for the object.
	
	var objStyle = OLDCAPE.utils.getActiveStyle(obj);
	
	// Safari bug. See above.
	
	if (objStyle == null)
	{
		
		// If the user is looking for the display property, we'll assume that
		// the element is hidden, though technically it could be a parent
		// element that's hidden.
		
		if (property == "display")
		{
			return "none";
		}
		
		// Try to display the element. This won't work if the element is hidden
		// by a parent element.
		
		var oldDisplay = obj.style.display;
		
		obj.style.display = "block";
		
		objStyle = OLDCAPE.utils.getActiveStyle(obj);
		
		obj.style.display = oldDisplay;
		
		// If it's still null, use the non-computed style.
		
		if (objStyle == null)
		{
			return obj.style[property];	
		}
		
		return objStyle[property];
		
	}
	else
	{
		
		return objStyle[property];
		
	}
		
};

// Takes 1 argument which is either the ID or object reference of elements whose
// style display mode will be toggled.

OLDCAPE.utils.getObject = function(param)
{
	if (typeof(param) == "object")
	{
		return param;
	}
	else
	{
		return document.getElementById(param);
	}
};

OLDCAPE.utils.toggleDisplay = function()
{
	for ( var i = 0; i < arguments.length; i++ )
	{
		
		var obj = OLDCAPE.utils.getObject(arguments[i]);
		
		if (obj == null)
		{
			OLDCAPE.utils.debug("Could not find object with ID " + arguments[i] + " in the page.");
		}
		
		// Since all elements are hidden with a value of "none", whereas each
		// element may have its own display value, we'll check for "none".
		
		var display = OLDCAPE.utils.getActiveStyleProperty(obj, "display");
		
		if (display == "none")
		{
			OLDCAPE.utils.show(obj);
		}
		else
		{
			OLDCAPE.utils.hide(obj);
		}
	
	}
};

OLDCAPE.utils.show = function(obj)
{
	
	obj.style.display = OLDCAPE.utils.getDefaultDisplay(obj);

	obj.style.visibility = "visible";
	
};


OLDCAPE.utils.hide = function(obj)
{
	
	obj.style.display = "none";
	
	obj.style.visibility = "hidden";
	
};

// Determines the default display value for the object's HTML element type. For
// instance, a DIV would have a default display of "block". This method should
// ensure that we use the correct display value for each browser.

OLDCAPE.utils.getDefaultDisplay = function(obj)
{
	
	var value = "";
	
	// Create a new element with a tag name that corresponds to the object.
	
	var objCopy = document.createElement(obj.tagName);
	
	// Unless we add the element to the document, we won't know the actual
	// display value. IE will return an empty string; Firefox, "block".
	
	// By setting the visibility to hidden, we ensure that the user will not
	// see any such changes.
	
	objCopy.visibility = "hidden";
	
	document.body.appendChild(objCopy);
	
	value = OLDCAPE.utils.getActiveStyle(objCopy).display;
	
	document.body.removeChild(objCopy);
	
	return value;
	
};

OLDCAPE.utils.debug = function(message)
{
	// alert(message);
};