// global variable
var nMenuLevel       = 0;	
var bInitialized     = 0;
var oMenu            = null;
var oCurrent         = null;
var oCloseTimer      = null;
var nDissapearDelay  = 200;
var bKeepHighLighted = 0;
var bFontItalic      = 0;
var bFontBold        = 0;        
var HighBgColor      = '#E8E8E8';
var HighSubBgColor   = '#E8E8E8';
var LowBgColor       = '#E8E8E8';		
var BorderColor      = '#E8E8E8';
var BorderSubColor   = '#E8E8E8';
var BorderWidth      = 0;
var BorderBtwnElmnts = 0;
var nSubItemSpace    = 8;
var g_bNav           = 0;
var g_bExp           = 0;
var g_oWnd           = null;
var g_strHide, g_strShow, g_strUnit;        

// main call
//g_oWnd.onload = OnSize;

//////////////////////////////////////////////////////////////////////////////////////
// Functions 
function CreateMenu( strMenu, nItems, strIdPos )
{	
	// check browser
	GetBrowserInfo();

	oMenu = CreateMenuStructure( strMenu, nItems );	

	if ( !NavYes )
		posX  = document.getElementById( strIdPos ).offsetTop + 1;	
	else
		posX  = document.getElementById( strIdPos ).offsetTop + 11;	
	
	posY  = document.getElementById( strIdPos ).offsetLeft + document.getElementById("main_area").offsetLeft;
		
	PosMenu( oMenu, posX, posY );

	bInitialized = 1;
	Initiate( oMenu );
	
	g_oWnd.onresize = OnSize;

	return oMenu;
}

function OnSize()
{
	posX  = document.getElementById( "dropDownUnderMenu" ).offsetTop + 1;	
	posY  = document.getElementById( "dropDownUnderMenu" ).offsetLeft + document.getElementById("main_area").offsetLeft;
	
	PosMenu( oHomeMenu, posX, posY )
	
	if ( document.getElementById( "more_soft_pos" ) != null )
	{		
		posX  = document.getElementById( "more_soft_pos" ).offsetTop + 1;	
		posY  = document.getElementById( "more_soft_pos" ).offsetLeft + document.getElementById("main_area").offsetLeft;
		
		PosMenu( oHomeSoft, posX, posY )
	}
	
	if ( document.getElementById( "more_sys_pos" ) != null )
	{
		posX  = document.getElementById( "more_sys_pos" ).offsetTop + 1;	
		posY  = document.getElementById( "more_sys_pos" ).offsetLeft + document.getElementById("main_area").offsetLeft;
	
		PosMenu( oHomeSys, posX, posY )		
	}
}

function PosMenu( CntnrPntr, Tp, Lt )
{
	nMenuLevel++;

	var Topi,Lefti,Hori;
	var Cntnr      = CntnrPntr;
	var Mmbr       = Cntnr.FrstMbr;
	var CntnrStyle = Cntnr.style;
	var MmbrStyle  = Mmbr.style;
	var MmbrWt     = parseInt(MmbrStyle.width);
	var MmbrHt     = parseInt(MmbrStyle.height);
	var CntnrWt    = parseInt(CntnrStyle.width);
	var CntnrHt    = parseInt(CntnrStyle.height);

	CntnrStyle.top  = nMenuLevel == 1 ? Tp + g_strUnit : 0;
	CntnrStyle.left = nMenuLevel == 1 ? Lt + g_strUnit : 0;
	//Cntnr.OrgTop    = nMenuLevel == 1 ? Tp : Tp - 5; 	
	Cntnr.OrgTop    = Tp; 	
	Cntnr.OrgLeft   = Lt;

	if ( nMenuLevel == 1 )
	{
		Hori  = 1;
		Lefti = CntnrWt - MmbrWt - 2 * BorderWidth;
		Topi  = 0
	}
	else
	{	
	  if ( nMenuLevel == 2 )
	  {
	  	Hori  = 0;
	  	Lefti = 0;
		Topi  = CntnrHt - MmbrHt - 2 * BorderWidth
		}
		else
		{
			Hori  = 0;
			Lefti = 0;
			Cntnr.OrgLeft = MmbrWt;
			Cntnr.OrgTop  = MmbrHt - 2 * BorderWidth;
			Topi          = CntnrHt - MmbrHt - 2 * BorderWidth;
		}
	}
	
	while ( Mmbr != null)
	{	  
		Cntnr.OrgTop   = nMenuLevel == 1 ? Tp : Cntnr.OrgTop; 
		MmbrStyle.left = Lefti + (nMenuLevel == 1 ? 0 : BorderWidth) + g_strUnit;
		MmbrStyle.top  = Topi  + (nMenuLevel == 1 ? 0 : BorderWidth) + g_strUnit;
				
		if( Mmbr.ChildCntnr )
		{						
			PosMenu( Mmbr.ChildCntnr, Topi + MmbrHt + (nMenuLevel == 1 ? 0 : 2 * BorderWidth), Lefti )
		}
		Mmbr = Mmbr.PrvMbr;
		
		if ( Mmbr )
		{	
		    MmbrStyle = Mmbr.style;
			MmbrWt    = parseInt(Mmbr.style.width);
			MmbrHt    = parseInt(Mmbr.style.height);
			
			Hori ? Lefti -= BorderBtwnElmnts ? (MmbrWt+BorderWidth) : (MmbrWt) : Topi -= BorderBtwnElmnts ? (MmbrHt+BorderWidth) : (MmbrHt)
		}
	}
	nMenuLevel--
}

function Initiate( oMenuToInit )
{	
	if ( bInitialized )
	{			
		Init( oMenuToInit );
		bInitialized = 0;
	}
}

function Init( CntnrPntr )
{
	var Mmbr    = CntnrPntr.FrstMbr;
	var MCStyle = CntnrPntr.style;	
	
	nMenuLevel++;
	
	MCStyle.visibility = nMenuLevel == 1 ? 'visible' : 'hidden';

	while ( Mmbr != null )
	{
		if( Mmbr.Hilite )
		{
		  Mmbr.Hilite = 0;

			if ( bKeepHighLighted )
			  LowItem( Mmbr )
		}
		if ( Mmbr.ChildCntnr ) 
		{
		  Init(Mmbr.ChildCntnr);		  
		 }
		Mmbr = Mmbr.PrvMbr
	}
	nMenuLevel--
}

function ClearAllChilds( Pntr )
{
	while ( Pntr )
	{
		if( Pntr.Hilite )
		{
			Pntr.Hilite = 0;

			if ( bKeepHighLighted )
			  LowItem( Pntr );

			if( Pntr.ChildCntnr )
			{
				Pntr.ChildCntnr.style.visibility = 'hidden';
				
				ClearAllChilds( Pntr.ChildCntnr.FrstMbr )
			}
			break
		}
		Pntr = Pntr.PrvMbr
	}
}

function GoTo()
{
	if( this.LinkTxt )
	{
		LowItem( this );
	
		this.LinkTxt.indexOf('javascript:') != -1 ? eval(this.LinkTxt) : window.location.href = this.LinkTxt
	}
}

function HiliteItem( P )
{
	if (P.ro)
	{	
		window.document.images[P.rid].src=P.ri2
	}
	else
	{	
		if (P.HiBck)
			P.style.backgroundColor=P.HiBck;

		if (P.HiFntClr)
		  P.style.color = P.HiFntClr
	}
	P.Hilite=1
}

function LowItem(P)
{
	if (P.ro)
	{	
		window.document.images[P.rid].src = P.ri1
	}
	else
	{	
		if (P.LoBck)
			P.style.backgroundColor =P.LoBck;
		if (P.LwFntClr)
			P.style.color = P.LwFntClr 
	}
}

function OpenMenu()
{	
	oCurrent    = this;
	bInitialized = 0;

	ClearAllChilds( this.Container.FrstMbr );
	HiliteItem( this );

	if ( this.ChildCntnr != null)
	{		
		this.ChildCntnr.style.top        = 2 + this.ChildCntnr.OrgTop  + parseInt(this.Container.style.top)  + ((NavYes) ? nOffsetTop : 0) + g_strUnit;
		this.ChildCntnr.style.left       = this.ChildCntnr.OrgLeft + parseInt(this.Container.style.left) + g_strUnit;
		this.ChildCntnr.style.visibility = 'visible'
	}
	status = this.LinkTxt
}

function CloseMenu()
{
	if(!bKeepHighLighted)
	{
		LowItem( this )
	}
	
	if( this == oCurrent )
	{
		bInitialized = 1;
		if( oCloseTimer )
		  clearTimeout(oCloseTimer);
		
		if ( oCurrent.ChildCntnr )
		{
			nMenuLevel = 0;
			oCloseMenu = oCurrent.Container;			
		}
		else
		{
			nMenuLevel = 1;
			oCloseMenu = oCurrent.Container;			
			//oCurrent.style.visibility = 'hidden';
		}
		oCloseTimer = setTimeout( 'Initiate(oCloseMenu)', nDissapearDelay )				
  }
}

function CntnrSetUp( nWidth, nHeight )
{
	this.FrstMbr          = null;
	this.OrgLeft          = 0;
	this.OrgTop           = 0;	
	this.bgColor          = nMenuLevel == 1 ? BorderColor : BorderSubColor;
	this.style.width      = nWidth  + g_strUnit;
	this.style.height     = nHeight + g_strUnit;
	this.style.fontFamily = FontFamily;
	this.style.fontWeight = bFontBold   ? 'bold'  : 'normal';
	this.style.fontStyle  = bFontItalic ? 'italic': 'normal';
	this.style.fontSize   = FontSize + 'px';
	this.style.zIndex     = nMenuLevel + 100	
	this.style.backgroundColor = nMenuLevel == 1 ? BorderColor : BorderSubColor;		
	
	if ( NavYes && nMenuLevel == 1 )
	{
		this.style.paddingBottom = 5  + "px";
		this.style.paddingRight  = 23 + "px";
	}
}

function MbrSetUp( MmbrCntnr, PrMmbr, WhatMenu, Wdth, Hght )
{
	var MemVal    = eval(WhatMenu + '[0]');
	var strParams = eval(WhatMenu + '[6]');

	if ( nMenuLevel >= 2 )
	  Hght = Hght - nSubItemSpace;
	  	
	this.PrvMbr            = PrMmbr;
	this.Level             = nMenuLevel;
	
	strLink = eval( WhatMenu + '[1]' );
	
	if ( strParams.length > 0 ) 
	{
		strLink = "javascript:window.open('" + strLink + "'," + strParams + ")";	
	}
	
	this.LinkTxt           = strLink;	
	this.Container         = MmbrCntnr;
	this.ChildCntnr        = null;
	this.Hilite            = 0;
	this.style.overflow    = 'hidden';
	this.style.cursor      = g_bExp && this.LinkTxt ? 'hand' : 'hand';
	this.ro                = 0;	
	this.value             = MemVal;
	this.LoBck             = nMenuLevel == 1 ? LowBgColor    : LowSubBgColor;
	this.LwFntClr          = nMenuLevel == 1 ? FontLowColor  : FontSubLowColor;
	this.HiBck             = nMenuLevel == 1 ? HighBgColor   : HighSubBgColor;
	this.HiFntClr          = nMenuLevel == 1 ? FontHighColor : FontSubHighColor;
	this.style.color       = this.LwFntClr;
	this.style.textAlign   = MenuTextCentered;
	this.style.width       = Wdth      + g_strUnit;
	this.style.height      = Hght      + g_strUnit;
	this.style.paddingLeft = nOffsetLeft + 0 + "px";
	this.style.paddingTop  = nOffsetTop  + g_strUnit;

	// set backgroundcolor
	if ( this.LoBck )
	  this.style.backgroundColor = this.LoBck;

	// generate background image
	if ( nMenuLevel == 1 && eval( WhatMenu + '[2]' ) )
	  this.style.backgroundImage = "url(\'" + eval(WhatMenu+'[2]')+"\')";
			
	// show text
	if ( document.getElementById )
	{		
		t = window.document.createTextNode( MemVal );    
		this.appendChild( t )
	}
	else 
	  this.innerHTML = MemVal;
		
	// Show icon
/*	if ( iconName )
	{
		t = window.document.createElement('img');
		this.appendChild(t);
		t.style.position = 'absolute';
		t.src            = iconName;
		t.style.width    = eval(WhatMenu + '[7]') + g_strUnit;
		t.style.height   = eval(WhatMenu + '[4]') + g_strUnit;
		t.style.top      = 0  + g_strUnit;
		t.style.left     = nOffsetLeft + g_strUnit
	}
*/	
	// show submenu icon	
	if ( nMenuLevel == 2 && eval(WhatMenu + '[3]') > 0 )
	{
	  t = window.document.createElement('img');
		this.appendChild( t );
		t.style.position = 'absolute';
		t.src            = '../common/images/tri.gif';
		t.style.width    = 5;
		t.style.height   = 10;
		t.style.top      = 5  + g_strUnit;
		t.style.left     = Wdth - 20 + g_strUnit;
	}
		
	if( navigator.userAgent.toLowerCase().indexOf('msie') != -1 )
	{		
		if ( nMenuLevel == 2 )
			this.onmouseover = OpenMenu;
			
		this.onmouseout  = CloseMenu;
		
		if ( nMenuLevel == 2 )
			this.onclick     = GoTo;
		else
		{
			this.onclick     = OpenMenu;
		}
	}
	else
	{
		if ( nMenuLevel == 2 )
			this.addEventListener('mouseover', OpenMenu,  false);
			
		this.addEventListener('mouseout',  CloseMenu, false);
		
		if ( nMenuLevel == 2 )
			this.addEventListener('click',     GoTo,      false);
		else
		{
			this.addEventListener('click',     OpenMenu,      false);
		}		
		
	}	
}

function CreateMenuStructure( MName,NumberOf )
{
	nMenuLevel++;
	
	var i,NoOffSubs,Mbr,Wdth=0,Hght=0;
	var PrvMmbr    = null;
	var WMnu       = MName+'1';
	var MenuWidth  = eval(WMnu+'[5]');
	var MenuHeight = eval(WMnu+'[4]');
	
	if ( NavYes && nMenuLevel == 1 )
		MenuHeight -= nOffsetTop;

	if ( nMenuLevel == 1 )
	{
		for ( i = 1; i < NumberOf + 1; i++ )
		{
			WMnu = MName + eval(i);
			Wdth = eval( WMnu + '[5]' ) ? Wdth + eval(WMnu+'[5]') : Wdth + MenuWidth;

			if ( NavYes && nMenuLevel == 1 )
			  Wdth -= nOffsetLeft;

			if ( NavYes & i == 1 )
			  Wdth -= 3
		}
		  	
		Wdth = BorderBtwnElmnts ? Wdth + (NumberOf + 1) * BorderWidth : Wdth + 2 * BorderWidth;
		Hght = MenuHeight + 2 * BorderWidth;		
	}
	else
	{	
	  for ( i = 1; i < NumberOf + 1; i++ )
		{
			if ( i == NumberOf )
				Hght = Hght + 3;				
			WMnu = MName + eval(i);
			Hght = eval(WMnu+'[4]') ? Hght + eval(WMnu+'[4]') : Hght + MenuHeight;
			Hght = Hght - nSubItemSpace;
		}
		  		
		Hght = BorderBtwnElmnts ? Hght + (NumberOf + 1) * BorderWidth : Hght + 2 * BorderWidth;
		Wdth = MenuWidth + 2 * BorderWidth;		
	}

	if ( NavYes )
	{
		var MmbrCntnr = window.document.createElement("div");

		MmbrCntnr.style.position   = 'absolute';
		MmbrCntnr.style.visibility = 'hidden';
	
		window.document.body.appendChild(MmbrCntnr) 		
	}
	else
	{
		if (document.getElementById)
		{	
			WMnu += 'c';
			window.document.body.insertAdjacentHTML( "AfterBegin", "<div id='"+WMnu+"' style='visibility:hidden; position:absolute;'><\/div>" );
			var MmbrCntnr = window.document.all[WMnu]		
		}
		else
		{	
			WMnu += 'c';
			window.document.body.insertAdjacentHTML( "AfterBegin", "<div id='"+WMnu+"' style='visibility:hidden; position:absolute;'><\/div>" );
			var MmbrCntnr = window.document.all[WMnu]
		}
	}

	MmbrCntnr.SetUp = CntnrSetUp;
	MmbrCntnr.SetUp( Wdth, Hght, MmbrCntnr );
	
	for ( i = 1; i < NumberOf + 1; i++ )
	{
		WMnu      = MName + eval(i);
		NoOffSubs = eval(WMnu+'[3]');
		Wdth      = nMenuLevel == 1 ? eval(WMnu+'[5]') ? eval(WMnu+'[5]') : MenuWidth : MenuWidth;
		Hght      = nMenuLevel == 1 ? MenuHeight : eval(WMnu+'[4]') ? eval(WMnu+'[4]') : MenuHeight;
		
		if ( NavYes && nMenuLevel == 1 )
			Wdth -= nOffsetLeft;
						
		if(document.getElementById)
		{
			Mbr = window.document.createElement("div");

			Mbr.style.position   = 'absolute';
			Mbr.style.visibility = 'inherit';
		
			MmbrCntnr.appendChild(Mbr)
		}
		else 
		  Mbr = window.document.all[WMnu];

		if ( NavYes && i == 1 && nMenuLevel == 1 )
		  Wdth -= 3
		  		
		if ( nMenuLevel >= 2 && i == NumberOf )
			Hght = Hght + 3;			
		
		Mbr.SetUp = MbrSetUp;
		Mbr.SetUp(MmbrCntnr,PrvMmbr,WMnu,Wdth,Hght);
		
		if ( NoOffSubs ) 
		  Mbr.ChildCntnr = CreateMenuStructure( WMnu+'_', NoOffSubs );
		
		PrvMmbr = Mbr
	}
	MmbrCntnr.FrstMbr=Mbr;
	nMenuLevel--;

	return (MmbrCntnr)
}

function GetBrowserInfo()
{
	AgntUsr = navigator.userAgent.toLowerCase();
	AppVer  = navigator.appVersion.toLowerCase();	
	NavYes  = AgntUsr.indexOf('mozilla') != -1 && AgntUsr.indexOf('compatible') == -1 ? 1 : 0;
	ExpYes  = AgntUsr.indexOf('msie')    != -1 ? 1 : 0;
	DomYes  = document.getElementById ? 1 : 0;
	DomNav  = NavYes ? 1 : 0;
 	DomExp  = ExpYes ? 1 : 0;
	
	g_strUnit = DomYes ? "px" : "px"
	g_bNav    = NavYes && document.layers ? 1 : 0;
	g_bExp    = ExpYes && document.all ? 1 : 0;
	g_oWnd    = NavYes ? window : window.document.body;				
	g_strHide = g_bNav ? 'hide' : 'hidden';
	g_strShow = g_bNav ? 'show' : 'visible';
}
