/*
   Milonic DHTML Menu - Treemenu module version 1.20 - October 26 2005
   This module is only compatible with the Milonic DHTML Menu version 5.62 or higher

   Copyright 2005 (c) Milonic Solutions Limited. All Rights Reserved.
   This is a commercial software product, please visit http://www.milonic.com/ for more information.
   
   SYNTAX: <script type="text/javascript" src="/treemenu.js"></script>
*/

var  _tAct

function treeMenuDisplay(_mnu,_show){                      // This function hides or shows the menu
	
	_m[_mnu][7]=_show                                      // Set the menus visibility property to Show or Hide 
	_m[_mnu].treemenu=1;
	menuDisplay(_mnu,_show); 
	}

_lastItem=-1
_Oi=-1

function resetOI(_oi)
{
	_otA(_oi)
	_caA(_oi)
	_gmi=gmobj("el"+_oi)
	_gmi.itemOn=1
	itemOff(_oi)
}



function _oTree()
{	
	if(_Oi>-1&&_mi[_Oi][34]!="tree")resetOI(_Oi)
	
	
	_Oi=_itemRef
	_otA(_Oi)
	
	if(_W.singleMasterMenu)
	{
		_I=_mi[_Oi]
		
		for (var _ai=_m[_I[0]][0][0];_ai<=_m[_I[0]][0][_m[_I[0]][0].length-1];_ai++)
		{	
			if(_mi[_ai].childN+" "!=$u&&_ai!=_Oi)
			{
				//alert($6)
				if(_mi[_ai].child.style.visibility==$6)resetOI(_ai)
			}
		}
	}
}

function _otA(_i)
{
	_dB=_d.body;
	_dBT=_dB.offsetTop
	_dBL=_dB.offsetLeft	

	_I=_mi[_i]                                             // set menu item shorthand to _I
	
	if(!_I[27])_I[27]=1
	
	if(!_I[3]||!_I[34])return                              // If no showmenu has been specified or not a treemenu go back
	if(!_I.child)                                          // Set the menu that this item will open.
	{		
		_I.childN=getMenuByName(_I[3])
		if(!_I.childN)return
		_I.child=gmobj("menu"+_I.childN)
	}
	
	hmL(_I.childN)
	
	//if(mac){if(!_I.obj)_I.obj=gmobj("pTR"+_i)              // IE on the The Apple Mac needs to use <TR> for positioning
	//}else{if(!_I.obj)_I.obj=gmobj("OtI"+_i)}                // All other browsers can use <TD>
	_I.obj=gmobj("OtI"+_i)
	_mnuO=_m[_I.childN]                                    // This is the object reference to the menu we are about to open
	_io=_I.obj                                             // Set _io as shorthand object reference to this menu item 
	_Pmenu=getMenuByItem(_i)
	if(_mnuO[7])                                           // If the DISPLAY attribute is on the menu is shown and must now be hidden
	{
		if(ie&&!mac)_io.style.display="none"
		_tAct=0
		treeMenuDisplay(_I.childN,0)                       // Hide this menu
		_CH=_m[_I.childN].HGT-_m[_I.childN].OHGT           // Set _CH as the variable to store this menus height, used for adjusting child and siblings
		closeTMenu(_i)
	}
	else                                                   // Display this child menu
	{
		if(ie&&!mac)_io.style.display="block"
		_tAct=1
		treeMenuDisplay(_I.childN,1)
		_PiGP=gpos(_io)                                    // get the Dimensions of parent menu item
		_cD=gpos(_I.child)                                 // Get the Dimensions of menu we are about to open
		_I.ttop=_PiGP[0]+_PiGP[2]-_I[27] // set temporary top property for the 
		_m[_I.childN].ttop=_I.ttop		
		_I.tleft=_PiGP[1]+treeOffset
		if(_I.childN==1)gmobj("menu"+_I.childN).style.backgroundImage=""
		if(mac)_I.tleft-=_I[27]
		if(sfri)
		{
			if(_m[_Pmenu][14]=="relative")
			{
				_I.tleft=(_I.tleft)+_dBL
				_I.ttop=(_I.ttop)+_dBT
				
			}
		else
			_I.tleft-=_I[27]
		}
		
		
		_m[_I.childN].tleft=_I.tleft
		spos(_I.child,_I.ttop,_I.tleft)         // set the position of the menu to open
		

		if(!_m[_I.childN].parentItem)
		{
			_m[_I.childN].parentItem=_io
			_m[_I.childN].OHGT=_PiGP[2]
			_m[_I.childN].parentItemN=_i
		}			
		_m[_I.childN].HGT=_cD[2]+_PiGP[2]-2

		_OH=_m[_mi[_i].childN].HGT-_m[_mi[_i].childN].OHGT

		if(_I.children)
		{
			for(_tm=0;_tm<_I.children.length;_tm++)
			{
				_mnu=_I.children[_tm] // this items menu
				_GP=gpos(_m[_mnu].parentItem) // get position of OtI
				_m[_mnu].ttop=_GP[0]+_m[_mnu].OHGT-_I[27] 
				spos(gmobj("menu"+_mnu),_m[_mnu].ttop,_m[_mnu].tleft) // set the position of this items menu
				treeMenuDisplay(_mnu,1) // show the menu
			}
		}
	}
	resetParents(_i)
}

function resetChildren(_i)
{	
	_ar=getChildrenByItem(_i)
	for (var _ai=1; _ai<_ar.length; _ai++)
	{
		_AI=_ar[_ai]
		if(_tAct)_m[_AI].ttop+=_OH; else _m[_AI].ttop-=_CH;
		spos(gmobj("menu"+_AI),_m[_AI].ttop)
	}
}


function resetSiblings(_i)
{
	var _I=_mi[_i]
	var _ar=_m[_I[0]][0]
	for (var _ai=0; _ai<_ar.length; _ai++)
	{
		_AI=_ar[_ai]
		if(_i<_AI)
		{
			if(_mi[_AI].childN)
			{
				_mnu=_mi[_AI].childN
				if(_tAct)_m[_mnu].ttop+=_OH; else _m[_mnu].ttop-=_CH;
				spos(gmobj("menu"+_mnu),_m[_mnu].ttop)
				resetChildren(_AI)
			}
		}
	}
}

function resetParents(_i)
{
	_I=_mi[_i]                                             // menu item
	_mnu=_I.childN
	_th=0
	while(_m[_mnu].parentItem)
	{
		if(_tAct)
		{
			_m[_mnu].HGT=_m[_mnu].HGT+_th	
			if(_th==0)_th=_m[_mnu].HGT-_m[_I.childN].OHGT;			
		}
		else
		{
			_m[_mnu].HGT=_m[_mnu].HGT-_CH
		}
		spos(_m[_mnu].parentItem,_n,_n,_m[_mnu].HGT)
		_fixMenu(_mnu)
		resetSiblings(_m[_mnu].parentItemN)
		_mnu=_mi[_m[_mnu].parentItemN][0]
	}
	
	if(mac)
	{	
		_macP=gpos(gmobj("tbl"+_mnu))
		spos(gmobj("menu"+_mnu),_n,_n,_macP[2])
	}
}



function closeTMenu(_i)
{	
	_ar=getChildrenByItem(_i)
	_I=_mi[_i]
	_I.children=_ar	
	for(_tm=0;_tm<_ar.length;_tm++)
	{		
		treeMenuDisplay(_ar[_tm],0)
	}	
	spos(_m[_I.childN].parentItem,_n,_n,_m[_I.childN].OHGT)
}



gChildren=new Array();
function crawlChildren(_i) // returns menus that are children of declared menu item
{	
	_cHm=_mi[_i].childN
	if(!_cHm)return
	var _ar=_m[_cHm][0]
	if(_mi[_i].childN && _m[_mi[_i].childN][7])gChildren[gChildren.length]=_mi[_i].childN;
	for (var _ai=_ar[0]; _ai<_ar[_ar.length-1]+1; _ai++)
	{		
		if(_mi[_ai].childN && _m[_mi[_ai].childN][7])crawlChildren(_ai)
	}
}


function getChildrenByItem(_i)
{
	gChildren=new Array();
	crawlChildren(_i)
	return gChildren;
}

function clickBranch(_titem){
	_popi(_titem);
	clickAction(_titem)
}


function closeBranchByName(_mnuN){
	if(_startM)return _StO('closeBranchByName("'+_mnuN+'")',50);
	_mnuN=$tL(_mnuN)
	for(var _ga=0;_ga<_mi.length;_ga++){
		if(_mi[_ga][3]){
			if(_mnuN==$tL(_mi[_ga][3])){
				_gpm=gmobj("menu"+getMenuByName(_mi[_ga][3]))
				if(_gpm.style.visibility==$6)clickBranch(_ga)
			}
		}
	}
}

function openBranchByName(_mnuN) // declare the name of a sub menu and open it as though the user had clicked on a menu item
{
	if(_startM)return _StO('openBranchByName("'+_mnuN+'")',50);
	_mnuN=$tL(_mnuN)
	for(var _ga=0;_ga<_mi.length;_ga++){
		if(_mi[_ga][3]){
			if(_mnuN==$tL(_mi[_ga][3])){
				_gpm=gmobj("menu"+_mi[_ga][0])
				if(_gpm.style.visibility==$5)openBranchByName(_m[_mi[_ga][0]][1])
				_gpm=gmobj("menu"+getMenuByName(_mi[_ga][3]))
				if(_gpm.style.visibility==$5)clickBranch(_ga)
			}
		}
	}
}


function allBranches(action)
{
	for(_ga=0;_ga<_mi.length;_ga++){
		if(_mi[_ga][3]&&_mi[_ga][34]=="tree"){
			_gpm=gmobj("menu"+getMenuByName(_mi[_ga][3]))
			if(_gpm){
				if(action)
				{
					if(_gpm.style.visibility==$5)clickBranch(_ga)
				}
				else{	
					if(_gpm.style.visibility==$6)clickBranch(_ga)
				}
			}
		}
	}
}


function openAllBranches(){
	if(_startM)return _StO('openAllBranches()',50);
	if(!_W.singleMasterMenu)allBranches(1)
}

function closeAllBranches(){
	if(_startM)return _StO('closeAllBranches()',50);
	allBranches(0)
}

