
var activeTopNavPanel;

function hideTopNavPanel(callback)
{
	if (activeTopNavPanel != null)
		activeTopNavPanel.slideUp('fast', callback);
}

function showTopNavPanel(panel, callback)
{
	if (activeTopNavPanel != null)
		hideTopNavPanel(function() { panel.slideDown('fast', callback); });
	else
		panel.slideDown('fast', callback);
}

function initTopNav()
{
    var shadowOpts = {
        left: 0,
        top: 2,
        blur: 4,
        opacity: 0.7,
        color: 'black',
        swap: false
    }
	
	var panelGroup = {
		activePanelIndex: -1,
		completeHide: false,
		activeTimeout: null,
		showPanel: function(panel)
		{
			if (panel.id != this.activePanelIndex && this.activePanelIndex != -1)
				this.hidePanel(panels[activePanelIndex]);
			
			var theGroup = this;

			panel.panel.show();
			panel.onOpen();

			panel.panel.hover(
				function() { theGroup.completeHide = false; clearTimeout(theGroup.activeTimeout); },
				function() { theGroup.hidePanel(panel) });
			
			panel.trigger.mouseout(function() {
				theGroup.completeHide = true;
				theGroup.activeTimeout = setTimeout(function() {
					if (theGroup.completeHide)
					{
						panel.panel.hide();
						panel.onClose();
						theGroup.activePanelIndex = -1;
					}
				}, 500);
			});
			
			this.activePanelIndex = panel.id;
		},
		hidePanel: function(panel)
		{
			panel.onClose();
			panel.panel.hide();
			
			this.activePanelIndex = -1;
		},
		panels: new Array()
	};

	var i = 0;
	$('#hhnTopNav ul li a').each(function() {
		var itemId = '#' + $(this).attr('rel');
		if (itemId != '#')
		{
			var dlg = $(itemId);
			var elem = $(this);
			var panelDef = 
		    {
				trigger: elem,
				panel: dlg,
				id: i,
				onOpen: function() 
				{
					this.panel.css('border', 'solid 1px #000');
					this.panel.dropShadow(shadowOpts);
					this.trigger.addClass('open');
				},
				onClose: function()
				{
					this.panel.removeShadow(shadowOpts);
					this.trigger.removeClass('open');
				}
		    };
			
			panelGroup.panels.push(panelDef);
			
			elem.click(function($e) {
				$e.preventDefault();
				
				panelGroup.showPanel(panelDef);
			});
		}
	});
}