/*Khoi tao menu*/
function initMenu(rowIndex){
	//if(sState==null)sState="collapse";
	if(rowIndex==null)rowIndex=0;
	var tblMenu =document.getElementById("menuMain");
	var rows=tblMenu.rows; 
	var row;
	for(i=0;i<rows.length;i++){
		row=rows[i]	;
		row.onmouseover=mnuMouseOver;
		row.onclick = mnuMouseClick;
		sState= row.getAttribute("state");
		if(sState==null)sState="collapse";
		initMenuItem(row,sState);
	}
	row =rows[rowIndex];
	mnuSelect(row);
}
/*-----------------------------------------------------------------*/
/*Khoi tao tung menu item*/
function initMenuItem(item,sState){
	item.setAttribute("state",sState);
	if(item.getAttribute("hasChild")!=null){
		//chi khi co thuoc tinh hasChild thi collapse, expand moi co y nghia
		item.style.display=''; //node cha luon luon visible
	}
	else{
		sClassName = item.className; 
		if (sClassName =='menu1'){
			item.style.display='';//menu cap 1 du khong co menu con van luon visible
		}
		else{
			//truong hop menu con, visible hay khong phu thuoc vao menu cha
			var parentItem = item;
			while(parentItem!=null&&parentItem.className!="menu1"){
				parentItem =parentItem.previousSibling;
			}
			if(parentItem!=null){
				var sTmp= parentItem.getAttribute('state');
				//alert(sTmp);
				if(sTmp==null||sTmp=='collapse'){
					item.style.display='none'; 
				}
				else{
					item.style.display=''; 
				}
			} 
		}
	}
}
/*-----------------------------------------------------------------*/
/*Dat state cho tung menu item*/
function setMenuState(rowIndex,sState){
	var tblMenu =document.getElementById("menuMain");
	var rows=tblMenu.rows; 
	var	row =rows[rowIndex];
	row.setAttribute("state",sState);
}
/*-----------------------------------------------------------------*/
function mnuSelect(row){
	var tblMenu =document.getElementById("menuMain");
	var rows=tblMenu.rows; 
	var rowTmp;
	var colTmp;
	var sClassName;
	var sIndex;
	var objImg;
	//Begin chuyen toan bo cac row ve trang thai binh thuong
	for(i=0;i<rows.length;i++){
		rowTmp=rows[i]	;
		var sClassName=rows[i].className;
		sIndex			=	sClassName.substr(sClassName.length-1,1);
		rowTmp.className=	"menu"+sIndex;	
		colTmp = getFirstChild(rowTmp);
		
		objImg = getFirstChild(colTmp);
		if(objImg!=null){
			objImg.src='images/bullet-inactive.gif';
		}
	}
	//End chuyen toan bo cac row ve trang thai binh thuong
	
	//Begin chuyen row lua chon ve trang thai select
	sClassName=row.className;
	sIndex			=	sClassName.substr(sClassName.length-1,1);
	row.className=	"selectMenu"+sIndex ;
	colTmp = getFirstChild(row);
	objImg = getFirstChild(colTmp);
	objImg.src='images/bullet-active.gif';
	//End chuyen row lua chon ve trang thai select
	
	//Begin Xem xem menu co la menu con cap 2 khong, neu la menu con cap 2 thi select not cai menu cap 1
	/*if(sIndex=="2"){
		row= getPreviousSibling(row);
		while(row!=null && !(row.className=='menu1'||row.className=='selectMenu1')){
			row= getPreviousSibling(row);
		}
		if(row!=null &&(row.className=="menu1"||row.className=='selectMenu1')){
			row.className=	"selectMenu1";
		}
	}*/
	//End Xem xem menu co la menu con cap 2 khong, neu la menu con cap 2 thi select not cai menu cap 1
}
/*-----------------------------------------------------------------*/
function mnuMouseClick(){

	var sLink=this.getAttribute("link");
	var sState= this.getAttribute("state");
	if(sLink!=null){
		window.location.href=sLink;
	}

	if(this.getAttribute("hasChild")!=null){
		
		if(sState==null||sState=="collapse"){
			sState= "expand";
		}
		else{
			sState= "collapse";
		}
		
		initMenuItem(this,sState);
		
	
		if((this.className=="menu1")&&(this.getAttribute("hasChild")!=null)){
			//menu cap 1 , co menu con o trong
			/*Begin collapse, expand cac menu con o trong*/
		
			var childMenu= getNextSibling(this);
			while(childMenu !=null && childMenu.className!='menu1'){
			//	alert(childMenu);
				initMenuItem(childMenu,sState); 
				//childMenu = childMenu.nextSibling; 
				childMenu= getNextSibling(childMenu);
			}
			/*End collapse, expand cac menu con o trong*/
			/*Begin collapse cac menu cap mot khac ngang cap*/
			if(sState=="expand"){
				var tblMenu =this.parentNode;
				for(i=0;i<tblMenu.rows.length;i++){
					var row =tblMenu.rows[i];
					if(row!=this){
						row.setAttribute("state","collapse");
						initMenuItem(row,"collapse");
					}
				} 
			}
			/*End collapse cac menu cap mot khac ngang cap*/

		}
	}

}
/*-----------------------------------------------------------------*/
function mnuMouseOver(){
	this.style.cursor ='pointer';
}
/*-----------------------------------------------------------------*/

