﻿function Menu(dataTable,instance)
{
  this.LastLeftID="";  
  this.table=dataTable
  this.instance=String(instance);
  this.init=function()
  {
    var shtml="<div id=\"PARENT\"><ul id=\"nav\" style='padding-left:0'>"
    for(var i=0;i<dataTable.Rows.length;i++)
    {
        var selectBackColor=""
      
        selectBackColor=dataTable.Rows[i].Select==true?"#a5b2de":""
        var href="";
        href=dataTable.Rows[i].Url==""?"#Menu=ChildMenu"+i:dataTable.Rows[i].Url;
        if(dataTable.Rows[i].Cells.length>0)
        {
            shtml+="<li><a border=0 href='"+href+"' style='background-color:"+selectBackColor+"' onclick=\"javascript:"+this.instance+".DoMenu('ChildMenu"+i+"')\"><span>"+dataTable.Rows[i].Name+"</span></a><ul id=\"ChildMenu"+i+"\"" 
            if(dataTable.Rows[i].Select==false)
            {
              shtml+=" class=\"collapsed\">";
            }
            else
            {
                shtml+=" class=\"expanded\">";
                var self=this;              
               this.LastLeftID="ChildMenu"+i;
            }
        shtml+=this.GetRow(dataTable.Rows[i])
        shtml+="</ul></li>\n"
        }
        else
        {
            shtml+="<li><a border=0 style='background-color:"+selectBackColor+"' href='"+href+"'>"+dataTable.Rows[i].Name+"</a>"
            shtml+="</li>\n"
        }
    }
    shtml+="</ul></div>"
    return shtml
  }
  this.GetRow=function(datarow)
  {
      var shtml=""
      for(var i =0;i<datarow.Cells.length;i++)
      {
        shtml+=this.GetCell(datarow.Cells[i]);
      }
      return shtml
  }
  this.GetCell=function(dataCell)
  {
     var backcor=dataCell.Select?"#a5b2de":""

        var shtml="<li><a style='background-color:"+backcor+"' href='"+dataCell.Value+"'>"+dataCell.Text+"</a></li>\n"
     
     return shtml;
  }
  this.DoMenu=function(emid)
    {
        var self=this;
	    var obj = document.getElementById(emid);	
	    obj.className = (obj.className.toLowerCase() == "expanded"?"collapsed":"expanded");
       try{
         document.getElementById(emid).getElementsByTagName("A")[0].style.getbackgroundColor="#a5b2de"
	    }
	    catch(ex){}
	    if((self.LastLeftID!="")&&(emid!=self.LastLeftID))	//关闭上一个Menu
	    {
		    document.getElementById(self.LastLeftID).className = "collapsed";
		     document.getElementById(self.LastLeftID).style.backgroundColor=""
	    }
	    self.LastLeftID = emid;
	    
    }

}