// JavaScript Document

//menu config
var xmlfile="menuxml.php";
//子菜单左偏移
//var menuleft=10;
var menuleft=0;
//子菜单上偏移
//var menutop=15;
var menutop=0;
//菜单项间隔
var menuvstep=2;
//菜单项左右间隔
var menuhstep=2;
//菜单项高度 更改同时要更改对应CSS样式
var menuheight=28;
//顶级菜单宽度 更改同时要更改对应CSS样式
var topmenuw=65;
//子菜单宽度 更改同时要更改对应CSS样式
var submenuw=120;
//鼠标指向标志，用于自动隐藏菜单
var overflag=false;
//自动消失菜单计时器
var hidetimer=false;
//菜单自动隐藏时间
var hidemenutime=1000;
var root;
//FireFox不支持selectNodes方法，在网上找到这段代码解决了这个问题。兼容了IE和FireFox.
//创建selectNodes方法
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }
   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        oXmlHttp = new XMLHttpRequest();
    }
}
function CreateXMLDOM()
{
createXMLHttpRequest();
oXmlHttp.open( "GET", xmlfile, false ) ;
oXmlHttp.send(null) ;
root = oXmlHttp.responseXML.documentElement;
}
CreateXMLDOM()

//检测是否有下级
function CheckPullMenu(passPid)
{

var currentItems = root.selectNodes("//Proot/Item[@pid = " + passPid + "]");
var iItems = currentItems.length;
if(iItems > 0)
{
  return true;
}
    else
{
  return false;
}
}
//显示隐藏层
function showSubMenu(sid)
{
	//alert(sid);
	var nowobj=document.getElementById("div"+sid);
	try{
var whichEl = document.getElementById( "piv" +sid);
if (whichEl.style.display == "none")
  {
	  //隐藏同级菜单
	  hideothermenu(sid);
	  //设置新菜单位置
	  setLoc(sid);
           whichEl.style.display="";
		   //alert(nowobj);
		   //alert(nowobj.className);
		   if (nowobj.className.substr(0,8)!="selected") nowobj.className="selected"+nowobj.className;
		   //alert(nowobj.className);
  }
else
  {
     whichEl.style.display="none";
	
	if (nowobj.className.substr(0,8)=="selected") nowobj.className=nowobj.className.substr(8);
  }
	}
	catch(e)
	{
	}
}

//设置层位置
function setLoc(sid)
{
	try{
	//得到上级的位置，显示在右边，右边不够显示在左边
	var sitem = document.getElementById("piv" +sid);
	var linkItem = root.selectNodes("//Proot/Item[@id = " + sid + "]");
	var pid=linkItem[0].attributes[1].value;
	var pitem=document.getElementById("div"+sid);
	if (pid==0)
	{
		//alert(pitem.offsetLeft);
		sitem.style.left=pitem.offsetLeft+menuleft+(document.body.clientWidth-914)/2+"px";//style.left;
		//sitem.style.top=pitem.style.top+pitem.clientHeight+5;
		sitem.style.top=document.getElementById("piv0").offsetTop+document.getElementById("piv0").clientHeight+pitem.offsetTop+menutop;
	}
	else
	{
	var ppitemn=root.selectNodes("//Proot/Item[@id = " + pid + "]");
	var ppitem=document.getElementById("piv"+ppitemn[0].attributes[0].value);
		sitem.style.left=ppitem.offsetLeft+ppitem.clientWidth+menuhstep+"px";
		//alert(pitem.offsetTop);
		sitem.style.top=pitem.offsetTop+ppitem.offsetTop-menuvstep+"px";
	}
	}
	catch(e)
	{
	}
	
}

//隐藏同级菜单
function hideothermenu(sid)
{
	try{
var currentItems = root.selectNodes("//Proot/Item[@id = " + sid + "]");
var pitems=root.selectNodes("//Proot/Item[@pid = " + currentItems[0].attributes[1].value + "]");
var iItems = pitems.length;
	  for(var i = 0; i < iItems; i++)
 	 {
		 
   		var _id = pitems[i].attributes[0].value;
		if (document.getElementById("piv" + _id))		
		{
			document.getElementById("piv" + _id).style.display="none";
			//alert(document.getElementById("div" + _id).className);
			if (document.getElementById("div" + _id).className.substr(0,8) == "selected") document.getElementById("div" + _id).className=document.getElementById("div" + _id).className.substr(8);
		}

		
if(CheckPullMenu(_id))
   {
	   var tItems = root.selectNodes("//Proot/Item[@pid = " + _id + "]");
	   var ttitems=tItems.length;
	   for (var j=0;j<ttitems;j++)  hideothermenu(tItems[j].attributes[0].value);
	 }
	 }
	}
	catch(e)
	{
	}

}

function hideallmenu()
{
	if (!overflag)
	{
	var currentItems = root.selectNodes("//Proot/Item[@pid = 0]");

	hideothermenu(currentItems[0].attributes[0].value);
	}
}

function funCreatePullMenu(passPid,ChildId)
{
var currentItems = root.selectNodes("//Proot/Item[@pid = " + passPid + "]");
var iItems = currentItems.length;
var k=0;
if(iItems > 0)
{
  
  var pdiv = document.createElement("DIV");
  pdiv.id="piv" + passPid;
  //pdiv.style="padding:0px;";
  pdiv.className = passPid==0?"tmenubox":"submenu";//"piv" + ChildId;
  pdiv.name = "piv" + passPid;
  if(passPid>0)
  {
       pdiv.style.display="none";
	   document.getElementById("odiv").appendChild(pdiv);
    //document.getElementById("div" + passPid).appendChild(pdiv);
  }
  else
  {
    document.getElementById("odiv").appendChild(pdiv);
  }
  for(var i = 0; i < iItems; i++)
  {
   var _id = currentItems[i].attributes[0].value;
   var newChild = document.createElement("DIV");
   newChild.id="div" + _id;
   newChild.style.top=i*menuheight+"px";
   //newChild.style="top:"+i*25+"px;";
   newChild.className = passPid==0?"tmenu":"menuitem";//"div" + ChildId;
   newChild.name = "div" + _id;
   
   var _v ;
   if(CheckPullMenu(_id))
   {
    _1= _id
   newChild.onmouseover=function(){
	   
	   //alert(document.getElementById("div"+this.id.substr(3)).className.substr(0,8));
	   //alert(document.getElementById("div"+this.id.substr(3)).className.substr(0,8) !="selected");
	   //alert(showSubMenu);
	   if (document.getElementById("div"+this.id.substr(3)).className.substr(0,8) !="selected") 
	   	{
			//alert(document.getElementById("div"+this.id.substr(3)).className.substr(0,8));
			showSubMenu(this.id.substr(3));
			overflag=true;
			//alert(showSubMenu);
		}
	   //if (this.className.substr(0,8) !="selected") showSubMenu(this.id.substr(3));
	   };
	  newChild.onmouseout=function(){overflag=false;};
   	//alert(newChild.onmouseover);
    //_v = "<a href='javascript:showSubMenu(" + _1 + ")' onmouseover='javascript:showSubMenu(" + _1 + ")'> " +currentItems[i].firstChild.data + "</a>";
    _v = "" +currentItems[i].firstChild.data + "";
   }
   else
   {
	   var linkItem = root.selectNodes("//Proot/Item[@id = " + _id + "]");
	   var tlink=linkItem[0].attributes[2].value;
	   var ttarget=linkItem[0].attributes[3].value;
	   var ttitle="";
	   if (linkItem[0].attributes.length>4) ttitle=linkItem[0].attributes[4].value;
	   if (passPid>0) 
	   {
		   newChild.className="menulinkitem";
		   newChild.onmouseover=function(){this.className="selectedmenuitem";hideothermenu(this.id.substr(3));overflag=true;};
		   newChild.onmouseout=function(){this.className="menulinkitem";overflag=false;};
	   }
	   else
	   {
		   newChild.onmouseover=function(){this.className="selectedtmenu";hideallmenu();overflag=true;};
		   newChild.onmouseout=function(){this.className="tmenu";overflag=false;};
	   }
	   newChild.onclick=function(){
		   var _url=document.getElementById("link"+this.id.substr(3)).href;
		   switch(document.getElementById("link"+this.id.substr(3)).target)
		   {
			   case "_blank":
			   	 window.open(_url);
			   	 break;
			   case "_self":
			     location=_url;
			   	 break;
			   case "_top":
			     window.top.location=_url;
			   	 break;
			   case "_parent":
			     window.parent.location=url;
			     break;
			   default:
			   	 break;
		   }
		   //alert(document.getElementById("link"+this.id.substr(3)).href);
			//alert(document.getElementById("link"+this.id.substr(3)).target);
		   };
    _v = "<a id='link"+_id+"' href='"+tlink+"' target='"+ttarget+"' title='"+ ttitle +"' onclick='javascript:return false;void(0);'>"+ currentItems[i].firstChild.data +"</a>";
	//_v = "<a href='ProductList.aspx?type=" + _id + "'>"+ currentItems[i].firstChild.data +"</a>";

   }
   newChild.innerHTML=_v;
   
   
   document.getElementById("piv" + passPid).appendChild(newChild);
   if (passPid==0)
   {	var tmp=document.createElement("DIV");
	tmp.className="menujg";
	//tmp.innerHTML="|";
	document.getElementById("piv0").appendChild(tmp);

   }
   //document.getElementById("odiv").appendChild(pdiv);
   
   if (!hidetimer) hidetimer=setInterval("hideallmenu()",hidemenutime);
   
   if(CheckPullMenu(_id))
   {
   funCreatePullMenu(_id,ChildId+1)
   }
  }
}
if (passPid==0)
{
var currentItems = root.selectNodes("//Proot/Item[@pid = " + passPid + "]");
var iItems = currentItems.length;
	  for(var i = 0; i < iItems; i++)
 	 {
   		var _id = currentItems[i].attributes[0].value;
		document.getElementById("div" + _id).style.left=i*(topmenuw+menuhstep)+"px";
		document.getElementById("div" + _id).style.top=0;
	 }

}
}
