/*JavaScript Document
//Written by: PioNeeR
//Free to copy
//Email me any idea: im_blackhorse@gmail.com
-----------------------------------------------------------------------------------------------------------------
Version history:
0.1: initial version
0.1.2: - customized for redcross.org.vn
	   - allow display w/o 2nd row (removed 2nd row)
	   - now support findMenuByName() for advanced find
*/

function XMenu(id, img, text, link, bullet, subCssClass, color, spacing, newWindow, idx){
	this.id=id;
	this.image=img;
	this.link=link;
	this.text=text;
	this.bullet=bullet;
	this.subCssClass=subCssClass;
	this.color=color;
	this.children=new Array();
	this.spacing=spacing;
	this.newWindow=newWindow;
	this.index=idx;
}

function XsubMenu(id, parent_id, text, link, newWindow){
	this.id=id;
	this.link=link;
	this.text=text;
	this.newWindow=newWindow;
	this.parentId=parent_id;
}

XMenu.prototype.addChild=function(id, text, link, newWindow){
	var submenu = new XsubMenu(id, this.id, text, link, newWindow);
	this.children.push(submenu);
}

XMenu.prototype.space=function(i) {
	var s = new String();
	for(var j=1; j<=i; j++){
		s = s.concat('&nbsp;');
	}
	return s;
}

XMenu.prototype.showChildren=function(){
	var s = '';	
	for(var i=0;i<this.children.length;i++){
		var smnu = this.children[i];
		//s = s.concat("<div class='fl'>");
		s = s.concat(this.spacing && i==0 >0?this.space(this.spacing):"").concat(i>0?"&nbsp;":"");
		//s = s.concat("</div>");
		//s = s.concat("<div class='fl' onmouseover='m186_mouseover("+this.index+");' onmouseout='m186_mouseout();'>&nbsp;");
		s = s.concat(this.bullet).concat("<a onmouseover='m186_mouseover("+this.index+");' onmouseout='m186_mouseout();' "+(this.color.length>0?" style='color:"+this.color+";' ":"")+" href='"+smnu.link+"' class='"+this.subCssClass+"' "+(smnu.newWindow==1?" target='_ws_"+smnu.id+"'":"")+">"+smnu.text+"</a>");
		//s = s.concat("</div>");
	}
	
	return s;
}

function Menu186(bgColor, textColor, selectedBgColor, selectedTextColor, bgImage, menuCss, separator, leftImage, rightImage, leftSubImage, rightSubImage, childMenuId){
	this.selectedIndex=0;
	this.menuBgColor=bgColor;
	this.menuTextColor=textColor;
	this.menuSelectedBgColor=selectedBgColor;
	this.menuSelectedTextColor=selectedTextColor;
	this.bgImage=bgImage;
	this.css=menuCss;
	this.separator=separator;
	this.leftImage=leftImage;
	this.rightImage=rightImage;
	this.leftSubImage=leftSubImage;
	this.rightSubImage=rightSubImage;
	this.childMenuId=childMenuId;
	this.delayInterval=500;
	this.timerHandle=0;
	this.oldHoverIdx=-1;
	this.menus = new Array();
	this.opt = 0;
	this.show2ndRow = 1;
}

function Menu186(bgColor, textColor, selectedBgColor, selectedTextColor, bgImage, menuCss, separator, leftImage, rightImage, leftSubImage, rightSubImage, childMenuId, showChildren){
	this.selectedIndex=0;
	this.menuBgColor=bgColor;
	this.menuTextColor=textColor;
	this.menuSelectedBgColor=selectedBgColor;
	this.menuSelectedTextColor=selectedTextColor;
	this.bgImage=bgImage;
	this.css=menuCss;
	this.separator=separator;
	this.leftImage=leftImage;
	this.rightImage=rightImage;
	this.leftSubImage=leftSubImage;
	this.rightSubImage=rightSubImage;
	this.childMenuId=childMenuId;
	this.delayInterval=500;
	this.timerHandle=0;
	this.oldHoverIdx=-1;
	this.menus = new Array();
	this.opt = 0;
	this.show2ndRow = showChildren;
}

Menu186.prototype.addChild=function(id, img, text, link, bullet, subCssClass, spacing, newWindow){
	var menu = new XMenu(id, img, text, link, bullet, subCssClass, this.menuSelectedTextColor, spacing, newWindow, this.menus.length);
	this.menus.push(menu);
	
	return menu;
}

Menu186.prototype.drawMenus = function(){
	var s = new String();
	var bg = (this.bgImage.length>0?" background='"+this.bgImage+"'":"");

	s+="<table cellspacing='0' cellpadding='0' border='0' width='100%' "+bg+"><tr>";
	s+=(this.leftImage.length>0?"<td width='1'><img src='"+this.leftImage+"' border='0' /></td>":"");
	s+="<td>";
	s+="<table cellspacing='0' cellpadding='0' border='0' height='31px'><tr>";
	s+=(this.bgImage.length>0?"<td width='10' "+bg+"><img src='"+this.bgImage+"' border='0' /></td>":"");
	for(var i=0;i<this.menus.length;i++){
		var menu = this.menus[i];
		var id=menu.id;
		if(menu.image.length>0){
			s+="<td valign='bottom' height='20px'>";
			s+="<a onmouseover='m186_mouseover("+i+",1);' onmouseout='m186_mouseout(1);' id='menu_"+id+"_a' href='"+menu.link+"' "+(menu.newWindow==1?" target='_w_"+menu.id+"'":"")+">"+menu.image+"</a>";
			s+="</td>";
		}else{
			s+="<td id='menu_"+id+"_ho'>"; /*style='background:"+this.menuBgColor+";'*/
			s+="<div style='overflow:hidden;height:31px;'>";
			s+="<div class='pmenu-normalleft fl' id='menu_"+id+"_l'></div>";
			s+="<div class='pmenu-normal fl' id='menu_"+id+"_c' onmouseover='m186_mouseover("+i+",0);' onmouseout='m186_mouseout(0);'><a id='menu_"+id+"_a' style='color:"+this.menuTextColor+";' class='"+this.css+"' href='"+menu.link+"' "+(menu.newWindow==1?" target='_w_"+menu.id+"'":"")+">"+menu.text+"</a></div>";
			s+="<div class='pmenu-normalright fl' id='menu_"+id+"_r'></div>";
			s+="</div>";
			s+="</td>";
		}
	}
	s+="<td width='1'>&nbsp;</td></tr>";
	s+="</table>";
	s+="</td>";
	s+=(this.rightImage.length>0?"<td width='1'><img src='"+this.rightImage+"' border='0' /></td>":"");
	s+="</tr></table>";
	s+="<table "+(this.show2ndRow==1?"":" style='display:none;' ")+" cellspacing='0' cellpadding='0' border='0' width='100%' bgcolor='"+this.menuSelectedBgColor+"'>";
	s+="<tr>";
	s+=(this.leftSubImage.length>0?"<td width='1' background='"+this.leftSubImage+"'><img src='"+this.leftSubImage+"' border='0' /></td>":"");
	//s+="<td height='25px' style='padding:5px 2px 0px 6px;'><div style='background:"+this.menuSelectedBgColor+";width:980px;' class='sub-menu' id='"+this.childMenuId+"'>&nbsp;</div></td>";
	s+="<td height='25px' style='padding:5px 2px 5px 6px;' style='background:"+this.menuSelectedBgColor+";' class='sub-menu' id='"+this.childMenuId+"'>&nbsp;</td>";
	s+=(this.rightSubImage.length>0?"<td width='1' background='"+this.rightSubImage+"'><img src='"+this.rightSubImage+"' border='0' /></td>":"");
	s+="</tr>";
	s+="</table>";
	
	return s;
}

Menu186.prototype.selectMenu = function(idx){
	this.mouseOver(idx);
	this.selectedIndex=idx;
}

Menu186.prototype.findMenu = function(id){
	for(var i=0;i<this.menus.length;i++){
		var menu = this.menus[i];
		if(menu.id==id){
			this.selectMenu(i);
			return 0;
		}
		
		for(var j=0;j<menu.children.length;j++){
			var smnu = menu.children[j];
			if(smnu.id==id){
				this.selectMenu(i);
				return 0;
			}
		}
	}
	
	return -1;
}

Menu186.prototype.findMenuByName = function(link){
	for(var i=0;i<this.menus.length;i++){
		var menu = this.menus[i];
		if(menu.link==link){
			this.selectMenu(i);
			return 0;
		}
		
		for(var j=0;j<menu.children.length;j++){
			var smnu = menu.children[j];
			if(smnu.link==link){
				this.selectMenu(i);
				return 0;
			}
		}
	}
	
	return -1;
	//this.selectMenu(0);
}

Menu186.prototype.mouseOver = function(idx, opt){
	var el = document.getElementById(this.childMenuId);
	this.opt = opt;
	if(el){
		this.clearTimer();
		if(this.oldHoverIdx!=idx){
			if(this.oldHoverIdx!=-1) this.deactiveMenu(this.oldHoverIdx);
			this.activeMenu(idx);
			var m = this.menus[idx];
			el.innerHTML = m.showChildren();
			
			if(el.innerHTML.length<=10){
				var el2 = document.getElementById("idDefaultMenuItems");
				if(el2) el.innerHTML = el2.innerHTML.replace("_S_ID_","idSearchContent");
			}
		}
		this.oldHoverIdx=idx;
	}
}

Menu186.prototype.mouseOut = function(opt) {
	this.opt = opt;
	this.setTimer();
}

Menu186.prototype.clearTimer=function() {
	if(this.timerHandle!=0) clearTimeout(this.timerHandle);
}

Menu186.prototype.setTimer=function() {
	this.clearTimer();
	this.timerHandle=setTimeout("currentMenu()",this.delayInterval);	
}

Menu186.prototype.activeMenu=function(idx) {	
	if(idx>=0) {
		var menu = this.menus[idx];
		var el = document.getElementById('menu_' + menu.id + '_c');
		if(el) el.className = 'pmenu-active fl';
		
		el = document.getElementById('menu_' + menu.id + '_l');
		if(el) el.className = 'pmenu-activeleft fl';
		
		el = document.getElementById('menu_' + menu.id + '_r');
		if(el) el.className = 'pmenu-activeright fl';
		
		el = document.getElementById('menu_' + menu.id + '_ho');
		if(el) el.style.background=this.menuSelectedBgColor;
		
		el = document.getElementById('menu_' + menu.id + '_a');
		if(el) el.style.color=this.menuSelectedTextColor;
		
	}		
}

Menu186.prototype.deactiveMenu=function(idx) {	
	if(idx >= 0) {
		var menu = this.menus[idx];
		var el = document.getElementById('menu_' + menu.id + '_c');
		if(el) el.className = 'pmenu-normal fl pmenu-normalState';
		
		el = document.getElementById('menu_' + menu.id + '_l');
		if(el) el.className = 'pmenu-normalleft fl pmenu-normalState';
		
		el = document.getElementById('menu_' + menu.id + '_r');
		if(el) el.className = 'pmenu-normalright fl pmenu-normalState';				
		
		el = document.getElementById('menu_' + menu.id + '_ho');
		if(el) el.style.background=this.menuBgColor;
		
		el = document.getElementById('menu_' + menu.id + '_a');
		if(el) el.style.color=this.menuTextColor;
	}	
}