var expandFirstItemAutomatically = false;	// Expand first menu item automatically ?
var initMenuIdToExpand = false;	// Id of menu item that should be initially expanded. the id is defined in the <li> tag.
var expandMenuItemByUrl = true;	// Menu will automatically expand by url - i.e. if the href of the menu item is in the current location, it will expand


var initialMenuItemAlwaysExpanded = true;	// NOT IMPLEMENTED YET

var dhtmlgoodies_slmenuObj;
var divToScroll = false;
var ulToScroll = false;	
var divCounter = 1;
var otherDivsToScroll = new Array();
var divToHide = false;
var parentDivToHide = new Array();
var ulToHide = false;
var offsetOpera = 0;
if(navigator.userAgent.indexOf('Opera')>=0)offsetOpera=1;	
var slideMenuHeightOfCurrentBox = 0;
var objectsToExpand = new Array();
var initExpandIndex = 0;
var alwaysExpanedItems = new Array();
var closeDiv = false;
	
function popMenusToShow()
{
	var obj = divToScroll;
	var endArray = new Array();
	while(obj && obj.tagName!='BODY'){
		if(obj.tagName=='DIV' && obj.id.indexOf('slideDiv')>=0){
			var objFound = -1;
			for(var no=0;no<otherDivsToScroll.length;no++){
				if(otherDivsToScroll[no]==obj){
					objFound = no;		
				}					
			}	
			if(objFound>=0){
				otherDivsToScroll.splice(objFound,1);	
			}		
		}	
		obj = obj.parentNode;
	}	
}

function showSubMenu(inputObj)
{

//	if(this && this.tagName)inputObj = this.parentNode;
	if (CurrentDiv != null && CurrentDiv != "")
	{
		otherDivsToScroll.push(document.getElementById(CurrentDiv));	
		CurrentDiv = "";
	}

	var MainLITbl;
	if(inputObj && inputObj.tagName=='LI'){
		divToScroll = inputObj.getElementsByTagName('DIV')[0];
		MainLITbl = inputObj.getElementsByTagName('TABLE')[0];
		for(var no=0;no<otherDivsToScroll.length;no++){
			if(otherDivsToScroll[no]==divToScroll)
			{
				closeDiv = true;
				autoHideMenus();
				return;
			}
		}
		
	}
	hidingInProcess = false;
	if(otherDivsToScroll.length>0){
		if(divToScroll){				
			if(otherDivsToScroll.length>0){
				popMenusToShow();
			}
			if(otherDivsToScroll.length>0){	
				autoHideMenus();
				hidingInProcess = true;
			}

		}	
	}		
	if(divToScroll && !hidingInProcess){
		if (MainLITbl)
		{
			MainLITbl.className = "MainMenuSelectedBG";
			var MainLITblHref = MainLITbl.getElementsByTagName('A')[0];
			if (MainLITblHref)
				MainLITblHref.className = "MainMenuSelectedLink";
			var MainLITblImg = MainLITbl.getElementsByTagName('IMG')[0];
			if (MainLITblImg)
				MainLITblImg.src = MainLITblImg.src.replace("MenuArrowExpand.gif","MenuArrowCollaps.gif");
		}
		divToScroll.style.display='';
		otherDivsToScroll.length = 0;
		otherDivToScroll = divToScroll.parentNode;
		otherDivsToScroll.push(divToScroll);	
		while(otherDivToScroll && otherDivToScroll.tagName!='BODY'){
			if(otherDivToScroll.tagName=='DIV' && otherDivToScroll.id.indexOf('slideDiv')>=0){
				otherDivsToScroll.push(otherDivToScroll);
									
			}
			otherDivToScroll = otherDivToScroll.parentNode;
		}			
		ulToScroll = divToScroll.getElementsByTagName('UL')[0];
		if(divToScroll.style.height.replace('px','')/1<=1)scrollDownSub();
	}
	
}

function autoHideMenus()
{
	if(otherDivsToScroll.length>0){
		divToHide = otherDivsToScroll[otherDivsToScroll.length-1];
		parentDivToHide.length=0;
		var obj = divToHide.parentNode.parentNode.parentNode;
		while(obj && obj.tagName=='DIV'){			
			if(obj.id.indexOf('slideDiv')>=0)parentDivToHide.push(obj);
			obj = obj.parentNode.parentNode.parentNode;
		}
		var tmpHeight = (divToHide.style.height.replace('px','')/1 - slideMenuHeightOfCurrentBox);
		if(tmpHeight<0)tmpHeight=0;
		if(slideMenuHeightOfCurrentBox)divToHide.style.height = tmpHeight  + 'px';
		ulToHide = divToHide.getElementsByTagName('UL')[0];
		slideMenuHeightOfCurrentBox = ulToHide.offsetHeight;
		scrollUpMenu();		
	}else{
		slideMenuHeightOfCurrentBox = 0;
		if (!closeDiv)
			showSubMenu();	
		else
		{
			var MainLI = document.getElementById('mainMenuItem' + parseInt(divToScroll.id.substr(8)));
			var MainLITbl = MainLI.getElementsByTagName('TABLE')[0];
			if (MainLITbl)
			{
				MainLITbl.className = "MenuBG";
				var MainLITblHref = MainLITbl.getElementsByTagName('A')[0];
				if (MainLITblHref)
					MainLITblHref.className = "MenuLink";
				var MainLITblImg = MainLITbl.getElementsByTagName('IMG')[0];
				if (MainLITblImg)
					MainLITblImg.src = MainLITblImg.src.replace("MenuArrowCollaps.gif","MenuArrowExpand.gif");
			}
			closeDiv = false;

		}		
	}
}


function scrollUpMenu()
{

	var height = divToHide.offsetHeight;
	height-=25;
	if(height<0)height=0;
	divToHide.style.height = height + 'px';

	for(var no=0;no<parentDivToHide.length;no++){	
		parentDivToHide[no].style.height = parentDivToHide[no].getElementsByTagName('UL')[0].offsetHeight + 'px';
	}
	if(height>0){
		setTimeout('scrollUpMenu()',5);
	}else{
		var MainLI = document.getElementById('mainMenuItem' + parseInt(otherDivsToScroll[0].id.substr(8)));
		var MainLITbl = MainLI.getElementsByTagName('TABLE')[0];
		if (MainLITbl)
		{
			MainLITbl.className = "MenuBG";
			var MainLITblHref = MainLITbl.getElementsByTagName('A')[0];
			if (MainLITblHref)
				MainLITblHref.className = "MenuLink";
			var MainLITblImg = MainLITbl.getElementsByTagName('IMG')[0];
			if (MainLITblImg)
				MainLITblImg.src = MainLITblImg.src.replace("MenuArrowCollaps.gif","MenuArrowExpand.gif");
		}
		MainLI = document.getElementById('mainMenuItem' + parseInt(divToScroll.id.substr(8)));
		MainLITbl = MainLI.getElementsByTagName('TABLE')[0];
		if (MainLITbl)
		{
			MainLITbl.className = "MainMenuSelectedBG";
			var MainLITblHref = MainLITbl.getElementsByTagName('A')[0];
			if (MainLITblHref)
				MainLITblHref.className = "MainMenuSelectedLink";
			var MainLITblImg = MainLITbl.getElementsByTagName('IMG')[0];
			if (MainLITblImg)
				MainLITblImg.src = MainLITblImg.src.replace("MenuArrowExpand.gif","MenuArrowCollaps.gif");
		}
		divToHide.style.display='none';
		otherDivsToScroll.length = otherDivsToScroll.length-1;
		autoHideMenus();			
	}
}	

function scrollDownSub()
{
	if(divToScroll){			
		var height = divToScroll.offsetHeight/1;
		var offsetMove =Math.min(25,(ulToScroll.offsetHeight - height));
		height = height +offsetMove ;
		divToScroll.style.height = height + 'px';
		
		for(var no=1;no<otherDivsToScroll.length;no++){
			var tmpHeight = otherDivsToScroll[no].offsetHeight/1 + offsetMove;
			otherDivsToScroll[no].style.height = tmpHeight + 'px';
		}			
		if(height<ulToScroll.offsetHeight)setTimeout('scrollDownSub()',5); else {
			divToScroll = false;
			ulToScroll = false;
			if(objectsToExpand.length>0 && initExpandIndex<(objectsToExpand.length-1)){
				initExpandIndex++;
				
				showSubMenu(false,objectsToExpand[initExpandIndex]);
			}
		}

	}
}
