/* Create a name space for RADIANT JavaScript so that we don't step on any third
 * party libraries. */

var RADIANT = {
	navigation : {},
	utils : {},
	sIFR : {}
};

RADIANT.navigation.activeMenu = null;

RADIANT.navigation.showMenu = function (menuID)
{
	
	var menu = RADIANT.utils.getObject(menuID);
	
	if (menu == null)
	{
		RADIANT.utils.debug("The menu  is missing for " + menuID);
	}
	
	if (menu == RADIANT.navigation.activeMenu)
	{
		return;
	}
	
	if (RADIANT.navigation.activeMenu == null)
	{
		RADIANT.utils.toggleDisplay(menu);
	}
	else
	{
		RADIANT.utils.toggleDisplay(menu, RADIANT.navigation.activeMenu);
	}
	
	RADIANT.navigation.activeMenu = menu;
	
};

// 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.

RADIANT.utils.getActiveStyle = function (obj)
{
  	
	// Ensure that we have an object and not just the ID of an HTML element.
	
	obj = RADIANT.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;
	}
  	
};

RADIANT.utils.getActiveStyleProperty = function (obj, property)
{
	
	// Ensure that we have an object and not just the ID of an HTML element.
	
	obj = RADIANT.utils.getObject(obj);
	
	// Get the computed style for the object.
	
	var objStyle = RADIANT.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 = RADIANT.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.

RADIANT.utils.getObject = function (param)
{
	if (typeof(param) == "object")
	{
		return param;
	}
	else
	{
		return document.getElementById(param);
	}
};

RADIANT.utils.toggleDisplay = function ()
{
	for ( var i = 0; i < arguments.length; i++ )
	{
		
		var obj = RADIANT.utils.getObject(arguments[i]);
		
		if (obj == null)
		{
			RADIANT.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 = RADIANT.utils.getActiveStyleProperty(obj, "display");
		
		if (display == "none")
		{
			RADIANT.utils.show(obj);
		}
		else
		{
			RADIANT.utils.hide(obj);
		}
	
	}
};

RADIANT.utils.show = function (obj)
{
	
	obj.style.display = RADIANT.utils.getDefaultDisplay(obj);

	obj.style.visibility = "visible";
	
};


RADIANT.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.

RADIANT.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 = RADIANT.utils.getActiveStyle(objCopy).display;
	
	document.body.removeChild(objCopy);
	
	return value;
	
};

RADIANT.utils.debug = function (message)
{
	// alert(message);
};


RADIANT.sIFR.writeHeader = function (swfPath)
{
	
	var params = {
		sFlashSrc: swfPath,
		sColor: "#740E1B",
		sWmode: "transparent",
		sFlashVars: "textalign=right"
	};
	
	sIFR.replaceElement("h1.sIFR", named(params));
	
};