/* Create a name space for oldCape JavaScript so that we don't step on any third
 * party libraries. */

var OLDCAPE = {
	menu : { activeMenu : null, activeLink : null },
	quickSearch : {}
};

OLDCAPE.init = function(menuID)
{
	menuID = menuID || OLDCAPE.menu.getDefault();
	
	OLDCAPE.menu.show(menuID);
	
	/* Shows a sub-menu when the mouse hovers over a menu. */
	$("#menu a").mouseover(function(e) {
		var linkID = e.target.id;
		var menuID = OLDCAPE.menu.getMenuFromLinkID(linkID)
		OLDCAPE.menu.show(menuID);
	});
	
	/* Expands and collapses the quick property search when the activator is
	 * clicked. */
	$("span.quick-property-search div.activator").click(function(e) {
		var $activator = $(this);
		
		if ($activator.hasClass("expanded"))
		{
			OLDCAPE.quickSearch.hide();
		}
		else
		{
			OLDCAPE.quickSearch.show();
		}
		
		e.preventDefault();
		return false;
	});
	
	/* Collapses the quick property search form if the user clicks elsewhere on
	 * the page. */
	$("body").click(function(e) {
		var $clicked = $(e.target);
		if ($clicked.closest("span.quick-property-search").length === 0)
		{
			OLDCAPE.quickSearch.hide();
		}
	});
};

/* Quick Search ***************************************************************/

OLDCAPE.quickSearch.show = function()
{
	var $activator = $("span.quick-property-search div.activator"); 
	$activator.addClass("expanded");
	
	var $panel = $("span.quick-property-search div.panel"); 
	$panel.show();
};

OLDCAPE.quickSearch.hide = function()
{
	var $activator = $("span.quick-property-search div.activator"); 
	$activator.removeClass("expanded");
	
	var $panel = $("span.quick-property-search div.panel"); 
	$panel.hide();
};

/* Menu ***********************************************************************/

/* Returns the default menu ID. The default menu is designated with a CSS class
 * named "default". */
OLDCAPE.menu.getDefault = function(menuID)
{
	var $default = $("#menu a.default");
	
	if ($default.length === 0)
	{
		return;
	}
	
	var linkID = $default.get(0).id;
	
	return  OLDCAPE.menu.getMenuFromLinkID(linkID);
};

/* Gets the menu corresponding to the specified link ID. This is highly
 * technical. We strip the word "Link" off the link ID. This will give us the
 * menu name. */
OLDCAPE.menu.getMenuFromLinkID = function(linkID)
{
	return linkID.substr(0, linkID.length - 4);
};

OLDCAPE.menu.show = function(menuID)
{
	var $menu = $("#" + menuID + "Menu");
	var $link = $("#" + menuID + "Link");
	
	// If we didn't find the menu, chances are we were passed an empty string.
	
	if ($menu.length == 0 || $link.length == 0)
	{
		return;
	}
	
	// See if the selected menu is the one we're already showing.
	
	if ($menu.get(0) == OLDCAPE.menu.activeMenu)
	{
		return;
	}
	
	// If we have an active menu, hide/deselect it.
	
	if (OLDCAPE.menu.activeMenu != null) 
	{
		$(OLDCAPE.menu.activeMenu).hide();
		$(OLDCAPE.menu.activeLink).removeClass("selected");
	}
	
	$menu.show("fast");
	$link.addClass("selected");
	
	OLDCAPE.menu.activeMenu = $menu.get(0);
	OLDCAPE.menu.activeLink = $link.get(0);
};
