///////////////////////////////////////////////////////////////
// add properties
//		className
//		cursor
//		onclick
//		onmouseover
//		onmouseout
//		label(image or text)
// to menus objects (for ubar_maxDepth levels) before HTML building
function addDinamicPropertiesToMenuObjects(obj){
	//init:
	var imObj;
	try{
		if(obj.showImg==1) imObj=eval("imUpM"+obj.id+"Off");
	}catch(e){}
	obj.cursor="pointer"
	obj.onmouseover="showUpperMenuDiv('"+obj.id+"');onClassName('"+obj.id+"')"
	obj.onmouseout="hideUpperMenuDiv('"+obj.id+"');offClassName('"+obj.id+"')"
	if(obj.id==homeFI)
		obj.onclick='go2address("'+homeURL+'")';
	else
		obj.onclick=obj.fnc
	
	var is_ssl = obj.is_ssl;
	
	obj.className=menuClasses[obj.dpt]
	obj.ImgArrowSubMenuClassName=ImgArrowSubMenuMenuClasses[obj.dpt]
	//case 1 : not active:
	if(obj.act!=1){
		obj.cursor="default"
		obj.onmouseover="onClassName('"+obj.id+"')"
		obj.onmouseout="offClassName('"+obj.id+"')"
		obj.onclick=""
	}
	//case 2 : is current:
	if(ifCurrent(obj)){
		if(obj.act==1){
			obj.cursor=(_existDoc)?"pointer":"default";
			obj.onclick=(_existDoc)?obj.onclick:"";
		}
		obj.className=menuClasses[obj.dpt]+"_s"
		obj.ImgArrowSubMenuClassName=ImgArrowSubMenuMenuClasses[obj.dpt]+"_s"
		obj.onmouseover=(obj.act==1)?"showUpperMenuDiv('"+obj.id+"')":""
		obj.onmouseout=(obj.act==1)?"hideUpperMenuDiv('"+obj.id+"')":""
		try{
			if(obj.showImg==1) imObj=eval("imUpM"+obj.id+"On");
		}catch(e){}
	}
	else{
	//case 3 : is parent:
		if(ifParentOfCurrent(obj)){
			obj.className=menuClasses[obj.dpt]+"_s"
			obj.ImgArrowSubMenuClassName=ImgArrowSubMenuMenuClasses[obj.dpt]+"_h"
			obj.onmouseover=(obj.act==1)?"showUpperMenuDiv('"+obj.id+"')":""
			obj.onmouseout=(obj.act==1)?"hideUpperMenuDiv('"+obj.id+"')":""
			try{
				if(obj.showImg==1) imObj=eval("imUpM"+obj.id+"On");
			}catch(e){}
		}
	}
	if(imObj && obj.showImg==1){
		obj.label="<img id='contImg"+obj.id+"' "
		obj.label+="src='"+imObj.src+"' "
		obj.label+="width='"+imObj.width+"' "
		obj.label+="height='"+imObj.height+"' border='0' />"
	}
	else{
		obj.label=obj.txt.replace(/\&amp;\#34;/g,"\"").replace(/\&amp;\#39;/g,"\'");

		/*It's in the css now
		if(!isnull(UpperMenuBullet)){
			if(obj.dpt == 0){
			    var imgSrc = UpperMenuBullet.src.toLowerCase();
				if(ifCurrent(obj) || ifParentOfCurrent(obj)){
		            try{
	                    imgSrc= imgSrc.replace(/_off\./g,"_on.");
		            }catch(e){}
				}
				obj.label="<img id='UpperMenuBullet" +obj.id+ "' src='"+imgSrc+"' border='0' width='"+UpperMenuBullet.width+"' height='"+UpperMenuBullet.height+"' class='uppermenubullet' />"
				+" " + obj.label //+"<img class='UpperMenuBullet' src='"+imgPath+"trans.gif' border='0' width='"+UpperMenuBullet.width+"' height='"+UpperMenuBullet.height+"'>"
				//alert(obj.label)
			}
			else
				obj.label= obj.label;
		}
		*/
	}
	obj.href=getHrefFromOnclick(obj.onclick,is_ssl)
	obj.target=getTargetFromOnclick(obj.onclick)
}
///////////////////////////////////////////////////////////////
//main function for build upper menus HTML:
function createUpperMenus(){
	//build div that contain sub-menus:
	function _createMenusDiv(menu){
		if((Number(menu.dpt)+1)>=ubar_maxDepth) return "";

		var subMenus=getSubUppMenus(menu)
		//dbgL(menu.id + ", " + subMenus.length)
		if(isnull(subMenus) || subMenus.length==0) return "";
		var s="<div id='contDiv"+menu.id+"' style='z-index:" +(100+(subMenus[0].dpt))+ ";position:absolute;visibility:hidden; "
					//s += "width:"+getWidth(subMenus,0)+"; "
					s += "' class='"+containerDivClass+(subMenus[0].dpt>1?"_lev"+subMenus[0].dpt:"")+"' >\
			<table dir='"+dir+"' id='contTable"+menu.id+"' cellpadding='0' cellspacing='0' width='100%' class='uppersubmenu"+(subMenus[0].dpt>1?"_lev"+subMenus[0].dpt:"")+"'>"
			for(var i=0;i<subMenus.length;i++){
				addDinamicPropertiesToMenuObjects(subMenus[i])
				//Reqursive build of menu levels
				var subMenusStr= _createMenusDiv(subMenus[i]);
				s+="<tr class='"+subMenus[i].className+"' \
						onmouseover=\""+subMenus[i].onmouseover+"\" onmouseout=\""+subMenus[i].onmouseout+"\">";
					
					if(putImgArrowSubMenuBeforeText && rtlImgArrowSubMenu != ""){
						s+="<td class='"+subMenus[i].ImgArrowSubMenuClassName+"'>"
							if(subMenusStr != ""){
								var theBullet = eval(dir.toLowerCase() + "ImgArrowSubMenu");
								s += " <img src='"+theBullet.src+"' width='"+theBullet.width+"' height='"+theBullet.height+"' /><br />"
							}
						s += "</td>"
					}
					
					s += "<td  id='subMenuTD"+subMenus[i].id+"' \
							width='100%' class='"+subMenus[i].className+"' \
							title='"+(!isnull(subMenus[i].hnt)?subMenus[i].hnt:"")+"' \
							style='cursor:"+subMenus[i].cursor+";' >\
						<a id='contText"+subMenus[i].id+"' \
							active='"+subMenus[i].act+"' \
							href='"+subMenus[i].href+"' "+subMenus[i].target+" \
							class='"+subMenus[i].className+"' \
							style='cursor:"+subMenus[i].cursor+";'>" 
							+ subMenus[i].label +"\
						</a>\
					</td>"
					
					if(!putImgArrowSubMenuBeforeText && rtlImgArrowSubMenu != ""){
						s+="<td class='"+subMenus[i].ImgArrowSubMenuClassName+"'>"
							if(subMenusStr != ""){
								var theBullet = eval(dir.toLowerCase() + "ImgArrowSubMenu");
								s += " <img src='"+theBullet.src+"' width='"+theBullet.width+"' height='"+theBullet.height+"' /><br />"
							}
						s += "</td>"
					}
					if((Number(subMenus[i].dpt)+1)<ubar_maxDepth){
						s+="<td width='0' style='font-size:0px;width:0px;vertical-align:top;'>"
							s += subMenusStr;
						s += "</td>"
					}
				s+="</tr>"
				if(i!=(subMenus.length-1)){
					s+="<tr class='"+horizDelimClass+(subMenus[0].dpt>1?"_Lev"+subMenus[0].dpt:"")+"'>\
						<td colspan='3' class='"+horizDelimClass+(subMenus[0].dpt>1?"_Lev"+subMenus[0].dpt:"")+"'></td>\
					</tr>";
				}
			}
			s+="</table>\
		</div>"
		return s;
	}
	var menusItems=getSubUppMenus();
	if(isnull(menusItems) || menusItems.length==0){
		dbgL("upper menu : 0 items found")
		return;
	}
	var s=""
	try{
		s+="<table id='upperMenus' name='upperMenus' cellpadding='0' cellspacing='0' width='100%' class='uppermenu'>\
			<tr class='uppermenu'>"
			for(var i=0;i<menusItems.length;i++){
				addDinamicPropertiesToMenuObjects(menusItems[i])
				//dbgL(menusItems[i].txt + ">>>" + menusItems[i].onmouseover + ">>>"   )
				//s+="<td class='"+menusItems[i].className+"' height='100%' \
				s+="<td height='100%' \
					onmouseover=\""+menusItems[i].onmouseover+"\" \
					onmouseout=\""+menusItems[i].onmouseout+"\" \
					title='"+(!isnull(menusItems[i].hnt)?menusItems[i].hnt:"")+"' \
					style='direction:"+dir+";' \
				>\
					<table dir='ltr' cellpadding='0' cellspacing='0' width='100%' height='100%'>\
						<tr>"
							s+="\
							<td id='upperMenu_left"+menusItems[i].id+"' class='"+menusItems[i].className+"_left' ></td>"
							s+="<td dir='" +dir+ "' class='"+menusItems[i].className+"' id='tdCont"+menusItems[i].id+"'> \
								<a id='contText"+menusItems[i].id+"' \
									active='"+menusItems[i].act+"' \
									href='"+menusItems[i].href+"' "+menusItems[i].target+" \
									class='"+menusItems[i].className+"' \
									style='cursor:"+menusItems[i].cursor+";' \
								>"
								s+=menusItems[i].label
								var subMenusStr= _createMenusDiv(menusItems[i])
								if(subMenusStr != ""){
									var theBullet = imgArrowDown;
									var tempBulletsrc = theBullet.src;
									if(ifCurrent(menusItems[i]) || ifParentOfCurrent(menusItems[i])){
										tempBulletsrc = theBullet.src.replace(/\.off\./g,".on.");
									}
									s += " <img id='imgArrowDown"+menusItems[i].id+"' src='"+tempBulletsrc+"' border='0' width='"+theBullet.width+"' height='"+theBullet.height+"' /></a><br />";
								}
								s += "</a>";
							s += "</td>"
							s+= "<td id='upperMenu_right"+menusItems[i].id+"' class='"+menusItems[i].className+"_right'></td>"
						s+="</tr> \
					</table>";
					s+="<table cellpadding='0' cellspacing='0' width='100%'>\
						<tr><td valign='top'>" +subMenusStr+ "</td></tr> \
					</table>\
				</td>"
				if(i!=(menusItems.length-1)){
					s+="<td class='"+vertDelimClass+(menusItems[0].dpt>1?"_Lev"+menusItems[0].dpt:"")+"'></td>";
				}
			}
			s+="</tr>\
		</table>"
		dbgL("upper menu created ...")
		//alert(s)
		document.write(s)
	}
	catch(e){
		dbgE("upper menu creating error : \n"+e.description)
	}
}


//upper menu - utils functions
//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//get menus(objects) array - child of argument menu(object):
function getSubUppMenus(obj){
	var _root=false
	var mnuArr=new Array();
	//var re=eval("/^"+MENU_OBJECT_NAME+"\\d+$/");
	var re=eval("/^"+MENU_OBJECT_NAME+"/");
	if(isnull(obj)){
		_root=true
		obj=eval(UPPER_ROOT_MENU_OBJECT_NAME);
	//alert(obj)
	}
	if(!_root && obj.act!=1) return mnuArr;
	if(!_root && obj.shwChld!=1) return mnuArr;
	for(var x in obj){
		if(re.test(x) && obj[x].ubar==1)
			mnuArr[mnuArr.length]=obj[x];
	}
	return mnuArr;
}

//////////////////////////////////////////////////////////////////
// Upper menus operation functions:
function showUpperMenuDiv(id){
	var obj=getReference('contDiv'+id)
	if(isnull(obj)) return
	
	//alert(obj.innerHTML)
	obj.style.visibility='visible';
	//if(appver<"5.5"){
		setTimeout('reverseDir("'+id+'")','1')
		setTimeout('hideSelects("contDiv'+id+'")','1')
	/*
	}
	else{
		reverseDir(id)
		hideSelects('contDiv'+id)
	}
	*/
}
function hideUpperMenuDiv(id){
	var obj=getReference('contDiv'+id)
	if(isnull(obj)) return
	obj.style.visibility='hidden';
	showSelects() 
}

//////////////////////////////////////////////////////////////////
function onClassName(id){
	var obj=getReference('contText'+id)
	var objTD=getReference('tdCont'+id)
	var td_subMenu=getReference('subMenuTD'+id)
	var objLeftTD=getReference('upperMenu_left'+id)
	var objRightTD=getReference('upperMenu_right'+id)
	var imgObj=getReference('contImg'+id)

	if(!isnull(obj)){
		if (obj.active=="0"){
			obj.className = obj.className+ ((obj.className.indexOf(" not_active")>-1 )? "" : " not_active")
			if(!isnull(imgObj)){
				//alert(imgObj.className + "\n*" +imgObj.className+ ((imgObj.className.indexOf(" not_active")>-1 )? "" : " not_active")+ "*")
				imgObj.className = imgObj.className+ ((imgObj.className.indexOf(" not_active")>-1 )? "" : " not_active")
			}
		}
		else{
			try{
				//dbgL(mnu_isIE+ ">>>" + td_subMenu.className +">>>"+ obj.className)
				if(mnu_isIE)				
					td_subMenu.parentElement.className=td_subMenu.className+"_h";
				else
					td_subMenu.parentNode.className=td_subMenu.className+"_h";
				td_subMenu.className=td_subMenu.className+"_h";

				//dbgL("IN>>> " + td_subMenu.className +">>>"+ obj.className)
			}
			catch(e){}
			
			obj.className=obj.className+"_h";
			
			try{
    			//alert(objLeftTD.className + "\n" + obj.className)
				objLeftTD.className=obj.className+"_left";
				objRightTD.className=obj.className+"_right";
			}catch(e){}
			try{
				objTD.className=objTD.className+"_h";
			}catch(e){}
		}
	}
	if(!isnull(imgObj)) imgObj.src=eval("imUpM"+id+"On").src;
	
	/*It's in the css now
	if(!isnull(UpperMenuBullet)){
		try{
            var imgSrc=UpperMenuBullet.src.toLowerCase();
    	    var bull=getReference('UpperMenuBullet' +id)
	        bull.src= imgSrc.replace(/_off\./g,"_on.");
		}catch(e){}
	}
	*/
	//var imgArrowDown = eval(dir.toLowerCase()+ "imgArrowDown")
	//alert(imgArrowDown)
	if(!isnull(imgArrowDown)){
		try{
            var imgArrowSrc=imgArrowDown.src.toLowerCase();
    	    var arrow=getReference("imgArrowDown" +id)
	        arrow.src= imgArrowSrc.replace(/\.off\./g,".on.");
		}catch(e){}
	}
}
function offClassName(id){
	var obj=getReference('contText'+id)
	var objTD=getReference('tdCont'+id)
	var td_subMenu=getReference('subMenuTD'+id)
	var objLeftTD=getReference('upperMenu_left'+id)
	var objRightTD=getReference('upperMenu_right'+id)
	var imgObj=getReference('contImg'+id)

	if(!isnull(obj)){
		try{
			if(mnu_isIE)				
				td_subMenu.parentElement.className=td_subMenu.parentElement.className.replace(/_h$/,"").replace(/ not_active/gi,"");
			else
				td_subMenu.parentNode.className=td_subMenu.parentNode.className.replace(/_h$/,"").replace(/ not_active/gi,"");
			td_subMenu.className=td_subMenu.className.replace(/_h$/,"").replace(/ not_active/gi,"");
		}
		catch(e){}
		
		obj.className=obj.className.replace(/_h$/,"").replace(/ not_active/gi,"");
		try{
			//alert(objLeftTD.className)
			objLeftTD.className=objLeftTD.className.replace(/_h_/,"_").replace(/ not_active/gi,"");
			objRightTD.className=objRightTD.className.replace(/_h_/,"_").replace(/ not_active/gi,"");
		}catch(e){}
		try{
			objTD.className=objTD.className.replace(/_h$/,"").replace(/ not_active/,"");
		}catch(e){}
	}
	if(!isnull(imgObj)){
		imgObj.className = imgObj.className.replace(/ not_active/gi,"");
		imgObj.src=eval("imUpM"+id+"Off").src;
	}
	
	/*It's in the css now
	if(!isnull(UpperMenuBullet)){
		try{
            var imgSrc=UpperMenuBullet.src.toLowerCase();
    	    var bull=getReference('UpperMenuBullet' +id)
	        bull.src= imgSrc.replace(/_on\./g,"_off.");
		}catch(e){}
	}
	*/	
	//var imgArrowDown = eval(dir.toLowerCase()+ "imgArrowDown")
	if(!isnull(imgArrowDown)){
		try{
            var imgArrowSrc=imgArrowDown.src.toLowerCase();
    	    var arrow=getReference("imgArrowDown" +id)
	        arrow.src= imgArrowSrc.replace(/\.on\./g,".off.");
		}catch(e){}
	}
}