//无限级popMenu
var pops = new Array();
function CreatePopup(degree)
{
    if (degree < 0)
        return null;
    if (pops[degree] != null)
        return pops[degree];

    if (degree == 0)
        pops[0] = window.createPopup();
    else{
        if (pops[degree - 1] == null)
            pops[degree - 1] = CreatePopup(degree - 1)
        pops[degree] = pops[degree - 1].document.parentWindow.createPopup();
    }
    pops[degree].document.body.setAttribute("degree", degree);
    return pops[degree];
}

//最大popMenu级数
CreatePopup(10);

//显示popMenu
function showPopMenu(obj,str,w,h,degree){
 var dhpop = window.createPopup();
 if(window.event == null){
  var lefter = w-10 ;
  var topper = obj.parentElement.offsetTop;
 }else{
  var lefter = window.event.offsetX;
  var topper = window.event.offsetY;
  if (lefter<100 &&topper<40){
  	lefter = -35;
	topper = document.getElementById('start_space').height;
	}else{
	topper = document.getElementById('start_space').height;
	}
 }
 

 pops[degree].document.body.style.fontSize = "12px";
 pops[degree].document.body.style.border = "1px solid #333333";
 pops[degree].document.body.style.backgroundColor = "lightyellow";
 pops[degree].document.body.style.margin = "5px";
 pops[degree].document.body.innerHTML = str;
 pops[degree].show(lefter, topper, w, h, obj);
}

//隐藏popMenu
function hidePopMenu(i){
 pops[i].hide();
}

//增加菜单
var str="";
function addMenu(menuid){
//alert("addmenu");
var str=""
 + "{'atid':'11','fid':'1','modulename':'请先登陆系统!','depth':'1','existssub':'0','onclick':'top.loginOS(\"in\")'}"
 //+ ",{'atid':'12','fid':'1','modulename':'空菜单','depth':'1','existssub':'0','onclick':'alert(\"ok\")'}"
 //+ ",{'atid':'13','fid':'1','modulename':'空菜单','depth':'1','existssub':'0','onclick':'alert(\"ok\")'}"
 //+ ",{'atid':'14','fid':'1','modulename':'空菜单','depth':'1','existssub':'0','onclick':'alert(\"ok\")'}"
 
//var strMenu="<span id='Menu"+menuid+"' style='display:none'>" +str +"</span>"
var strMenu="<span id='Menu"+menuid+"' >" +str +"</span>"
document.body.insertAdjacentHTML("beforeEnd",strMenu);
//document.getElementById("msg").innerText="完成！";
return str;

}

//AJAX方式加载栏目
function getMenu(menuid){

var obj=document.getElementById("Menu"+menuid);
if(obj != null){
ajaxRV="{'MenuList':["+obj.innerText+"]}";
}else{
//alert("未设置"+"Menu"+menuid+"菜单！");
//document.getElementById("msg").innerText="导入菜单............................................................";
str=addMenu(menuid);
ajaxRV="{'MenuList':["+str+"]}";
//alert("请您登陆系统!");
//loginOS("in");//登陆
}
 //将服务器端获得的数据使用JSON方式转换数据类型
 return eval("("+ajaxRV+")");


}


//将加载栏目数据组合成popMenu文档
function showMenu(obj,menuid){
try {
var tempObj = getMenu(menuid);
var mlList = tempObj.MenuList.length;
var tempArr = new Array();
 var depth = 0;
 for(var i=0;i<mlList;i++){

  //使用JSON方式转换数据类型后的数据
 var name=tempObj.MenuList[i].modulename ;
 var url=tempObj.MenuList[i].onclick;

 if(url!=""){name="<span style='cursor:hand' onclick="+url+">"+name+"</span>";}

  //生成是否有子栏目不同的显示文档
  
  if(tempObj.MenuList[i].existssub == 1){
   tempArr.push("<div style=\"height:20px;line-height:20px;cursor:default;\" onmouseover=\"top.showMenu(this,"+tempObj.MenuList[i].atid+");\"><table width='100%' cellpadding='0' cellspacing='0'><tr height='20' onmouseover=\"this.style.backgroundColor='highlight';this.style.color='highlighttext';\" onmouseout=\"this.style.backgroundColor='';this.style.color='';\"><td width='20'></td><td style='font-size:12px;' valign='middle'>"+name+"</td><td width='15' style='font-size:12px;' valign='middle'><font style='font-family:webdings;'>4</font></td></tr></table></div>");
  }else{
   tempArr.push("<div style=\"height:20px;line-height:20px;cursor:default;\" ><table width='100%' cellpadding='0' cellspacing='0'><tr height='20' onmouseover=\"this.style.backgroundColor='highlight';this.style.color='highlighttext';\" onmouseout=\"this.style.backgroundColor='';this.style.color='';\"><td width='20'></td><td style='font-size:12px;' valign='middle'>"+name+"</td><td width='15'></td></tr></table></div>");
  }
  //当前栏目层深
  depth = tempObj.MenuList[i].depth;
 }

var tempStr = tempArr.join("");
 if(tempStr != "" && tempStr != null){
  //加载popMenu
  showPopMenu(obj,tempStr,160,mlList*20+12,depth);
  //隐藏切换后的popMenu
  for(var di=0;di<pops.length-1;di++){
   if(di>depth){
    top.hidePopMenu(di);
   }
  }
 }



} catch(err) {
    document.writeln("Error name: " + err.name + "");
    document.writeln("Error message: " + err.message);
}
finally{
   // alert(mlList);
}

 return false;
}
