/*
   Milonic DHTML Menu - Treemenu module version 1.21 - January 3 2006
   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 isOdd(Ovalue)
{
Ovalue=Ovalue/2
if(indexOf(Ovalue+" ","."))return true; else return false
}

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)
	{
		_TI=_mi[_Oi]
		
		for (var _ai=_m[_TI[0]][0][0];_ai<=_m[_TI[0]][0][_m[_TI[0]][0].length-1];_ai++)
		{	
			if(_mi[_ai].childN+" "!=$u&&_ai!=_Oi)
			{
				//alert($6)
				if(_mi[_ai].child.style.visibility==$6)resetOI(_ai)
			}
		}
	}
}


function setTreeClass(_gm)
{
	_ti=_itemRef
	_mnu=getMenuByName(_mi[_ti][3])
	_gm=gmobj("menu"+_mnu)
	if(_W.treeOffset)_gm.style.paddingLeft=treeOffset+"px"

	for(_a=0;_a<_m[_mnu][0].length;_a++)
	{
		_cItem=_m[_mnu][0][_a]
		_titemObj=gmobj("el"+_cItem)
		
		//gmobj("andy").innerHTML=_cItem + " - " + _titemObj + " - " + _mnu + " - " + _a + " - " + _ti + " - " + _itemRef
		if(_mi[_cItem][3])
		{
			_titemObj.className="treeItemCollapsed";
			_mi[_cItem][87]="treeItemExpanded";
			_mi[_cItem][54]="treeItemCollapsed";
		}
		else
		{
			if(_m[_mnu][6].clickimage==_mi[_cItem][82])_mi[_cItem][82]=""
			_titemObj.className="treeItemBranch"	
			imgO=gmobj("_imgO"+_cItem)
			if(_W.treeItemImagePadding)if(imgO)imgO.style.paddingLeft=treeItemImagePadding+"px"
		}
	}

	_tmnu=getMenuByItem(_ti)
	if(_ti!=_m[_tmnu][0][_m[_tmnu][0].length-1])_gm.className="treeBranch"
	if(_mi[_cItem][3])
	{
		_titemObj.className="treeEndItemCollapsed";
		_mi[_cItem][87]="treeEndItemExpanded";
		_mi[_cItem][54]="treeEndItemCollapsed";
	}
	else
	{
		_titemObj.className="treeEndItem"	
	}
		
		
		
		

}

function _otA(_ti)
{
	_dB=_d.body;
	_dBT=_dB.offsetTop
	_dBL=_dB.offsetLeft	

	_TI=_mi[_ti]                                             // set menu item shorthand to _TI
	
	if(!_TI[27])_TI[27]=1
	
	if(!_TI[3]||!_TI[34])return                              // If no showmenu has been specified or not a treemenu go back
	if(!_TI.child)                                          // Set the menu that this item will open.
	{		
		_TI.childN=getMenuByName(_TI[3])
		if(!_TI.childN)return
		_TI.child=gmobj("menu"+_TI.childN)
	}
	
	hmL(_TI.childN)
	
	//if(mac){if(!_TI.obj)_TI.obj=gmobj("pTR"+_ti)              // IE on the The Apple Mac needs to use <TR> for positioning
	//}else{if(!_TI.obj)_TI.obj=gmobj("OtI"+_ti)}                // All other browsers can use <TD>
	
	
	if(!_TI.obj)
	{
		setTreeClass(_TI[0])
		//setTreeClass(getMenuByName(_mi[_ti][3]))
		
	}
	
	
	_TI.obj=gmobj("OtI"+_ti)
	_mnuO=_m[_TI.childN]                                    // This is the object reference to the menu we are about to open
	_tio=_TI.obj                                             // Set _tio as shorthand object reference to this menu item 
	_Pmenu=getMenuByItem(_ti)
	if(_mnuO[7])                                           // If the DISPLAY attribute is on the menu is shown and must now be hidden
	{
		if(ie&&!mac)_tio.style.display="none"
		_tAct=0
		treeMenuDisplay(_TI.childN,0)                       // Hide this menu
		_CH=_m[_TI.childN].HGT-_m[_TI.childN].OHGT           // Set _CH as the variable to store this menus height, used for adjusting child and siblings
		closeTMenu(_ti)
	}
	else                                                   // Display this child menu
	{
		if(ie&&!mac)_tio.style.display="block"
		_tAct=1
		treeMenuDisplay(_TI.childN,1)
		_PiGP=gpos(_tio)                                    // get the Dimensions of parent menu item
		_cD=gpos(_TI.child)   
		

		_cor=1
		if(ie)_cor=0

		
		                              // Get the Dimensions of menu we are about to open
		_TI.ttop=_PiGP[0]+_PiGP[2]-_TI[27]+_cor // set temporary top property for the 
		_m[_TI.childN].ttop=_TI.ttop		
		_TI.tleft=_PiGP[1]+treeOffset
		_TI.tleft=_TI.tleft-treeOffset
		//if(_TI.childN==1)gmobj("menu"+_TI.childN).style.backgroundImage=""
		if(mac)_TI.tleft-=_TI[27]
		if(sfri)
		{
			if(_m[_Pmenu][14]=="relative")
			{
				_TI.tleft=(_TI.tleft)+_dBL
				_TI.ttop=(_TI.ttop)+_dBT
				
			}
		else
			_TI.tleft-=_TI[27]
		}
		
		
		_m[_TI.childN].tleft=_TI.tleft
		spos(_TI.child,_TI.ttop,_TI.tleft)         // set the position of the menu to open
		

		if(!_m[_TI.childN].parentItem)
		{
			_m[_TI.childN].parentItem=_tio
			_m[_TI.childN].OHGT=_PiGP[2]
			_m[_TI.childN].parentItemN=_ti
		}			
		
		
		
		//_cor=0
		//if(ie)_cor=0
		//_m[_TI.childN].HGT=_cD[2]+_PiGP[2]-_cor // height correction
		
		
		
		_m[_TI.childN].HGT=_cD[2]+_PiGP[2] // height correction
		
		
		//alert(_m[_TI.childN].HGT)

		_OH=_m[_mi[_ti].childN].HGT-_m[_mi[_ti].childN].OHGT

		if(_TI.children)
		{
			for(_tm=0;_tm<_TI.children.length;_tm++)
			{
				_mnu=_TI.children[_tm] // this items menu
				_GP=gpos(_m[_mnu].parentItem) // get position of OtI
				_m[_mnu].ttop=_GP[0]+_m[_mnu].OHGT-_TI[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(_ti)
}

function resetChildren(_ti)
{	
	_ar=getChildrenByItem(_ti)
	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(_ti)
{
	var _TI=_mi[_ti]
	var _ar=_m[_TI[0]][0]
	for (var _ai=0; _ai<_ar.length; _ai++)
	{
		_AI=_ar[_ai]
		if(_ti<_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(_ti)
{
	_TI=_mi[_ti]                                             // menu item
	_mnu=_TI.childN
	_th=0
	while(_m[_mnu].parentItem)
	{
		if(_tAct)
		{
			_m[_mnu].HGT=_m[_mnu].HGT+_th	
			if(_th==0)_th=_m[_mnu].HGT-_m[_TI.childN].OHGT;			
		}
		else
		{
			_m[_mnu].HGT=_m[_mnu].HGT-_CH
		}
		//alert(_m[_mnu].HGT)
		
		_cor=0
		if(ie)_cor=1

		
		spos(_m[_mnu].parentItem,_n,_n,_m[_mnu].HGT-_cor)
		_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(_ti)
{	
	_ar=getChildrenByItem(_ti)
	_TI=_mi[_ti]
	_TI.children=_ar	
	for(_tm=0;_tm<_ar.length;_tm++)
	{		
		treeMenuDisplay(_ar[_tm],0)
	}	
	spos(_m[_TI.childN].parentItem,_n,_n,_m[_TI.childN].OHGT)
}



gChildren=new Array();
function crawlChildren(_ti) // returns menus that are children of declared menu item
{	
	_cHm=_mi[_ti].childN
	if(!_cHm)return
	var _ar=_m[_cHm][0]
	if(_mi[_ti].childN && _m[_mi[_ti].childN][7])gChildren[gChildren.length]=_mi[_ti].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(_ti)
{
	gChildren=new Array();
	crawlChildren(_ti)
	return gChildren;
}

function clickBranch(_titem){
	_mnu=getMenuByName(_mi[_titem][3])
	_M=_m[_mnu]
	if(_M&&!_M[23])BDMenu(_mnu)
	_mnu=-1; 
	_itemRef=_titem
	itemOff(_titem,1)
	_caA(_titem)
	_oTree()
}


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)
{
	//start=new Date().getTime()
	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)
				}
			}
		}
	}
	//end=new Date().getTime()
	//alert(end-start)
}


function openAllBranches(){
	if(_startM)return _StO('openAllBranches()',50);
	if(!_W.singleMasterMenu)allBranches(1)
}

function closeAllBranches(){
	if(_startM)return _StO('closeAllBranches()',50);
	allBranches(0)
}

