/*
 * topNavi_menu.js
 * Functionality for the top menu on dan pearlman website
 *
 * Part of the dan pearlman theme
 *
 * @author Jack Weinert (lieblinx/CodeMonkz)
 */
var currentSubUnitIndex = 0;
var unitContainerWidth = 0;
var unitWidths = [];
var hideTimer = null;

// DP_CalculateWidths ----------------------------------------------------------
// Measure/calculates the width of the menu entries to enable dynamic
// re-positioning of hierarchical deeper menu entries.
// ---------------------------------------------------------- DP_CalculateWidths
function DP_CalculateWidths()
{
	if (unitWidths.length == 0)
	{
		// calculate unit width
		var unitContainer = HTML.GetElementByID('unitContainer');
		var unitCell = HTML.FirstChildTag(unitContainer);
		var unitIndex = 0;
		unitContainerWidth = 0;
		while (unitCell)
		{
			if (HTML.HasElementClass(unitCell,'unit') == true)
			{
				var cellWidth = unitCell.clientWidth;
				unitWidths.push(cellWidth);
				
				unitContainerWidth += cellWidth;
				unitIndex++;
			}
			unitCell = HTML.NextSiblingTag(unitCell);
		}
	}
}

// DP_HideSubUnit --------------------------------------------------------------
// Hide the sub unit menu entries given by its unit index.
//
// @param
// index - index of the unit, which sub unit entries should be hidden
// -------------------------------------------------------------- DP_HideSubUnit
function DP_HideSubUnit(index)
{
	var subUnitElement = HTML.GetElementByID('subUnitContainer_' + index);
	HTML.AppendElementClass(subUnitElement,'hidden');
	
	if (hideTimer)
	{
		window.clearTimeout(hideTimer);
		hideTimer = null;
	}
	
	if (currentSubUnitIndex == index)
	{
		currentSubUnitIndex = 0;
	}
}

// DP_ShowSubUnit --------------------------------------------------------------
// Show the sub unit menu entries given by its unit index.
//
// @param
// index - index of the unit, which sub unit entries should be shown
// -------------------------------------------------------------- DP_ShowSubUnit
function DP_ShowSubUnit(index)
{
	var subUnitElement = HTML.GetElementByID('subUnitContainer_' + index);
	HTML.RemoveElementClass(subUnitElement,'hidden');
	
	var leftOffset = 0;
	for (var i=0;i < (index - 1);i++)
	{
		leftOffset += unitWidths[i];
	}
	
	if ((leftOffset + subUnitElement.offsetWidth) > 1000)
	{
		leftOffset = 1000 - subUnitElement.offsetWidth;
	}
	
	subUnitElement.style.marginLeft = leftOffset + 'px';
}

// DP_ToggleSubUnit ------------------------------------------------------------
// Toggles the state of the sub unit menu entries given by its unit index.
//
// @param
// index - index of the unit, which sub unit entries should be toggled between
//	showing and hiding
// ------------------------------------------------------------ DP_ToggleSubUnit
function DP_ToggleSubUnit(index)
{
	DP_CalculateWidths();
	if (currentSubUnitIndex != index)
	{
		if (currentSubUnitIndex != 0)
		{
			DP_HideSubUnit(currentSubUnitIndex);
		}
		DP_ShowSubUnit(index);
		currentSubUnitIndex = index;
	}
	else
	{
		DP_HideSubUnit(index);
		currentSubUnitIndex = 0;
	}
}

// DP_DisplaySubUnit -----------------------------------------------------------
// Display the sub unit menu entries given by its unit index.
//
// @param
// index - index of the unit, which sub unit entries should be displayed
// ----------------------------------------------------------- DP_DisplaySubUnit
function DP_DisplaySubUnit(index)
{
	DP_CalculateWidths();
	if (currentSubUnitIndex != index)
	{
		if (currentSubUnitIndex != 0)
		{
			DP_HideSubUnit(currentSubUnitIndex);
		}
		
		DP_ShowSubUnit(index);
		currentSubUnitIndex = index;
	}
	else
	{
		if (hideTimer)
		{
			window.clearTimeout(hideTimer);
			hideTimer = null;
		}
	}
}

// DP_CanHideSubUnit -----------------------------------------------------------
// Signal that the sub unit menu entries of a given unit can be hidden.
//
// @param
// index - index of the unit, which sub unit entries can be hidden
// ----------------------------------------------------------- DP_CanHideSubUnit
function DP_CanHideSubUnit(index)
{
	if (currentSubUnitIndex == index)
	{
		hideTimer = window.setTimeout('DP_HideSubUnit('+index+');',5000);
	}
}

// DP_StopHideSubUnit ----------------------------------------------------------
// Signal that the sub unit menu entries of a given unit should not hidden
// anymore. This is the opposite behaviour that DP_CanHideSubUnit().
//
// @param
// index - index of the unit, which sub unit entries should not be hidden
//	anymore
// ---------------------------------------------------------- DP_StopHideSubUnit
function DP_StopHideSubUnit(index)
{
	if (currentSubUnitIndex == index)
	{
		if (hideTimer)
		{
			window.clearTimeout(hideTimer);
			hideTimer = null;
		}
	}
}

