var m_JSNavi_aButtons = new Object();
var m_JSNavi_aLines = new Object();
var m_JSNavi_nMaxLineLevel = 0;

// Line

function JSNavi_AddLine(oButton)
{
	if(!oButton) return;
	var nId = oButton.nId;
	var oElLine = JS_General_GetNode("NL" + nId);
	if(!oElLine) return;
	var oLine = new JSNavi_Line(nId, oElLine, oButton);
	m_JSNavi_aLines[nId] = oLine;
	oButton.oLine = oLine;
	if(oLine.nLevel > m_JSNavi_nMaxLineLevel) m_JSNavi_nMaxLineLevel = oLine.nLevel;
}

function JSNavi_Line(nId, oElLine, oButton)
{
	this.nId = nId;
	this.oElLine = oElLine;
	this.oButton = oButton;
	this.nWidth = 0;
	this.bVisible = false;
	this.nLevel = oButton.nLevel + 1;
	this.oElSep = JS_General_GetNode("NS" + oButton.nLevel);
	this.oFrom = oButton.aChildren[0].oElUp;
	this.oTo = oButton.aChildren[oButton.aChildren.length - 1].oElUp;
	this.Position = JSNavi_LinePosition;
	this.Show = JSNavi_LineShow;
	this.Hide = JSNavi_LineHide;
	
	if(this.oElSep) this.oElSep.style.backgroundColor = "transparent";
}

function JSNavi_LineShow()
{
	if(this.bVisible) return;
	this.bVisible = true;
	if(this.oButton.oElDown) this.oButton.oElDown.style.backgroundColor = "";	
	this.oElLine.style.display = "block";
	if(!this.nWidth) this.nWidth = JSNavi_GetLineWidth(this);
	JSNavi_AdjustBox();
	if(!this.oElSep) return;
	this.oElSep.style.backgroundColor = "";
	this.Position();
}

function JSNavi_GetLineWidth(oLine)
{
	var aChildren = oLine.oButton.aChildren;
	var nWidth = 0;
	for(var nIndex in aChildren)
	{
		var oDivButton = aChildren[nIndex].oElDiv;
		nWidth += oDivButton.offsetWidth;
	}
	
	if(oLine.nLevel == 1)
	{
		var oExtra = JS_General_GetNode("NBExtra");
		if(oExtra) nWidth += oExtra.offsetWidth;
	}
	
	return nWidth;
}

function JSNavi_AdjustBox()
{
	var nWidth = 0;
	for(var nId in m_JSNavi_aLines)
	{
		var oLine = m_JSNavi_aLines[nId];
		if(oLine.bVisible && oLine.nWidth > nWidth) nWidth = oLine.nWidth;
	}
	nWidth += 1;
	JSNavi_SetBoxWidth(nWidth);
}

function JSNavi_SetBoxWidth(nWidth)
{
	var nBorderWidth =  m_oDivNaviBorderLeft.offsetWidth;
	m_oDivNavi.style.width = (nWidth + 2 * nBorderWidth) + "px";
	m_oDivNaviInner.style.width = nWidth + "px";
	var nHeight = m_oDivNaviInner.offsetHeight;		
	m_oDivNaviBorderLeft.style.height = nHeight + "px";
	m_oDivNaviBorderRight.style.height = nHeight + "px";
}

function JSNavi_LinePosition()
{
	var oPos = this.oElSep;
	if(!oPos) return;
	var from = JSNavi_GetOffset(this.oFrom);
	var to = JSNavi_GetOffset(this.oTo);
	var down = JSNavi_GetOffset(this.oButton.oElDown);
	from = from < down ? from : down;
	to = to > down ? to : down;
	var width = to - from + this.oTo.offsetWidth;
	oPos.style.left = from + "px";	
	oPos.style.width = width + "px";	
}

function JSNavi_GetOffset(oObj)
{
	var nOffset = oObj.offsetLeft;
	if(m_JS_General_r8IE) nOffset += oObj.offsetParent.offsetLeft;
	return nOffset;
}

function JSNavi_LineHide()
{
	if(!this.bVisible) return;
	this.bVisible = false;
	this.oElLine.style.display = "none";
	this.oButton.oElDown.style.backgroundColor = "transparent";	
	if(!this.oElSep) return;
	var bVisible = false;
	for(var nId in m_JSNavi_aLines)
	{
		var oLine = m_JSNavi_aLines[nId];
		if(oLine.nLevel == this.nLevel) bVisible |= oLine.bVisible;
	}
	if(!bVisible) 
	{
		this.oElSep.style.backgroundColor = "transparent";
		this.oElSep.style.width = "1px";
		JSNavi_AdjustBox();
	}
}

// Button

function JSNavi_AddButton(sName, nId, nLevel, sParent)
{
	if(nId > 0 && !JS_General_GetNode("NB" + nId)) return; 
	var oButton = new JSNavi_Button(sName, nId, nLevel, sParent)
	m_JSNavi_aButtons[oButton.sName] = oButton;
}

function JSNavi_Button(sName, nId, nLevel, sParent)
{
	this.sName = sName;
	this.nId = nId;
	this.nLevel = nLevel;
	this.sParent = sParent;
	this.bActive = false;
	this.bHilite = false;
	this.aChildren = new Array();
	this.Init = JSNavi_ButtonInit;
	this.InitLine = JSNavi_ButtonInitLine;
	this.Show = JSNavi_ButtonShow;
	this.Hide = JSNavi_ButtonHide;
	this.Activate = JSNavi_ButtonActivate;
	this.Deactivate = JSNavi_ButtonDeactivate;
	this.ShowAndActivate = JSNavi_ButtonShowAndActivate;
	this.Hilite = JSNavi_ButtonHilite;
	this.UnHilite = JSNavi_ButtonUnHilite;
}

function JSNavi_ButtonInit()
{
	this.oParent = m_JSNavi_aButtons[this.sParent];
	if(this.oParent) this.oParent.aChildren[this.oParent.aChildren.length] = this;
	this.oElDiv = JS_General_GetNode("NB" + this.nId); 
	this.oElUp = JS_General_GetNode("NB" + this.nId + "Up"); 
	this.oElButton = JS_General_GetNode("NB" + this.nId + "Button"); 
	this.oElLink = JS_General_GetNode("NB" + this.nId + "Link"); 
	this.oElDown = JS_General_GetNode("NB" + this.nId + "Down");

	if(this.oElDown) this.oElDown.style.backgroundColor = "transparent";
	if(this.oElDiv) this.oElDiv.onmouseover = Function("JSNavi_ShowAndHiliteButton('" + this.sName + "')");
}

function JSNavi_ButtonInitLine()
{
	JSNavi_AddLine(this);
}

function JSNavi_ButtonShow()
{	
	if(this.oParent) 
	{
		for(var nIndex in this.oParent.aChildren)
		{
			var oButton = this.oParent.aChildren[nIndex];
			if(oButton.nId != this.nId) oButton.Hide();
		}

		this.oParent.Show();
	}
	
	if(this.oLine) this.oLine.Show();
}

function JSNavi_ButtonHilite()
{
	if(this.bHilite) return;
	this.bHilite = true;
	if(this.oElLink) this.oElLink.className = "NaviHover";
	if(this.oParent) this.oParent.Hilite();
}

function JSNavi_ButtonUnHilite()
{
	if(!this.bHilite || !this.oElLink) return;
	this.bHilite = false;
	if(this.oElLink) this.oElLink.className = this.bActive ? "NaviActive" : "NaviInactive";
}

function JSNavi_ButtonHide()
{
	if(!this.oLine) return;
	this.oLine.Hide();
	
	var nLevel = this.nLevel + 2;

	if(nLevel < m_JSNavi_nMaxLineLevel) return;
	for(var nId in m_JSNavi_aLines)
	{
		var oLine = m_JSNavi_aLines[nId];
		if(oLine.nLevel >= nLevel) oLine.Hide();
	}
}

function JSNavi_ButtonActivate()
{
	if(this.bActive) return;
	this.bActive = true;
	
	if(this.oElButton) this.oElButton.className = "NaviButton NaviButtonActive";
	if(this.oElLink) this.oElLink.className = "NaviActive";
	
	if(this.oParent) 
	{
		for(var nIndex in this.oParent.aChildren)
		{
			var oButton = this.oParent.aChildren[nIndex];
			if(oButton.nId != this.nId) oButton.Deactivate();
		}

		this.oParent.Activate();
	}
}

function JSNavi_ButtonDeactivate()
{
	if(!this.bActive) return;
	this.bActive = false;
	if(this.oElButton) this.oElButton.className = "NaviButton NaviButtonInactive";
	if(this.oElLink) this.oElLink.className = "NaviInactive";
}

function JSNavi_ButtonShowAndActivate()
{
	this.Show();
	this.Activate();
}

// General

function JSNavi_InitNavi()
{	
	m_oDivNavi = JS_General_GetNode("Navi");
	m_oDivNaviBorderTop = JS_General_GetNode("NaviBorderTop");
	m_oDivNaviBorderLeft = JS_General_GetNode("NaviBorderLeft");
	m_oDivNaviInner = JS_General_GetNode("NaviInner");
	m_oDivNaviBorderRight = JS_General_GetNode("NaviBorderRight");
	m_oDivNaviBorderBottom = JS_General_GetNode("NaviBorderBottom");

	for(var sName in m_JSNavi_aButtons)
	{
		var oButton = m_JSNavi_aButtons[sName];
		oButton.Init();
	}
	
	for(var sName in m_JSNavi_aButtons)
	{
		var oButton = m_JSNavi_aButtons[sName];
		oButton.InitLine();
	}
	
	m_oDivNaviBorderTop.onmouseover = JSNavi_RestoreButtons;
	m_oDivNaviBorderLeft.onmouseover = JSNavi_RestoreButtons;
	m_oDivNaviBorderRight.onmouseover = JSNavi_RestoreButtons;
	m_oDivNaviBorderBottom.onmouseover = JSNavi_RestoreButtons;
	
	JSNavi_DetectActiveItem();
	JSNavi_RestoreButtons();
}

function JSNavi_ShowAndActivateButton(sButton)
{
	var oButton = m_JSNavi_aButtons[sButton];
	if(oButton) oButton.ShowAndActivate();
}

function JSNavi_ShowAndHiliteButton(sButton)
{
	var oButton = m_JSNavi_aButtons[sButton];
	if(oButton)
	{ 
		oButton.Show();
		JSNavi_UnHiliteAllButtons();
		oButton.Hilite(true);
	}
}

function JSNavi_UnHiliteAllButtons()
{
	for(var sName in m_JSNavi_aButtons)
	{
		var oButton = m_JSNavi_aButtons[sName];
		oButton.UnHilite();
	}
}

function JSNavi_ShowButton(sButton)
{
	var oButton = m_JSNavi_aButtons[sButton];
	if(oButton) oButton.Show();
}

function JSNavi_HideButton(sButton)
{
	var oButton = m_JSNavi_aButtons[sButton];
	if(oButton) oButton.Hide();
}

function JSNavi_RestoreButtons()
{
	JSNavi_UnHiliteAllButtons();
	JSNavi_ShowAndActivateButton(m_JSNavi_sActiveItem);
}

function JSNavi_DetectActiveItem()
{
	if(m_JSNavi_sActiveItem && m_JSNavi_aButtons[m_JSNavi_sActiveItem]) return;
	var sPage = JSNavi_GetPageName(window.location.pathname);
	var oButton = JSNavi_GetButtonByPage(sPage);
	m_JSNavi_sActiveItem = oButton ? oButton.sName : "start";
}

function JSNavi_GetButtonByPage(sPage)
{
	for(var sName in m_JSNavi_aButtons)
	{
		var oButton = m_JSNavi_aButtons[sName];

		if(oButton.oElLink && oButton.oElLink.href)
		{
			var sButtonPage = JSNavi_GetPageName(oButton.oElLink.pathname);
			if(sButtonPage == sPage) return oButton;		
		 }
	}
	
	return null;
} 

function JSNavi_GetPageName(sPage)
{
	if(!sPage) return "";
	var nLast = sPage.lastIndexOf("/");
	if(nLast < 0) return sPage;
	if(nLast >= sPage.length - 1) return "";
	return sPage.substr(nLast + 1);
}
