function Menunode(id,name,link) {
         this.nodes = [];
         this.id = id;
         this.name = name;
         this.link = link;
         this.open = false;
         this.parent = null;
}
function Menutree(id) {
         this.nodes = [];
         this.id = id;
       this.url='';
       this.open=false;
}
var menutree=new Menutree('0');
Menunode.prototype.add= function (node) {
    this.nodes[this.nodes.length]=node;
    node.parent=this;
};
Menunode.prototype.show= function () {
    var html='';
    if(this.nodes.length > 0)
         html+="<a href='javascript:void(0)' onclick=\"javascript:clickMenuNode('"+this.id+"')\">"+this.name+"</a>\r";
    else
        html+="<a href='"+this.link+"'>"+this.name+"</a>\r";
    if(this.open){
        html+='<ul>';
        for(var index=0;index<this.nodes.length;index=index+1){
           html+="<li>";
           html+=this.nodes[index].show();
           html+="</li>";
        }
         html+='</ul>';
   }
   return html;
};
function  clickMenuNode(id){
     var node=findNode(id,menutree.nodes);

     if(node != null){
          if(node.open)
             node.open=false;
          else
             node.open=true;
     }
     menutree.show();
}

function findNode(id,nodes){
     for(var i=0;i<nodes.length;i++){
         if(nodes[i].id == id)
            return nodes[i];
         var node=findNode(id,nodes[i].nodes);
         if(node != null)
            return node;
     }
    return null;
}

Menutree.prototype.init = function (cid,vid) {
          this.nodes=[];
       this.id=cid;
       this.open=true;
       var sUrl = "subcat.ajx?vid="+vid+"&cid="+cid;
       new net.ContentLoader(sUrl,callback,"get","text",null);
};
Menutree.prototype.show = function () {
     if(this.nodes.length == 0){
         window.location=this.url;
         return ;
     }
   var html='';
     if(this.open){
         html='<ul>';
         for(var index=0;index<this.nodes.length;index=index+1){
            html+="<li>";
            html+=this.nodes[index].show();
            html+="</li>";
         }
          html+='</ul>';
          document.getElementById("menu"+this.id).innerHTML = html;
          document.getElementById("menu"+this.id).style.display='';
      }
      else {
         document.getElementById("menu"+this.id).innerHTML = html;
         document.getElementById("menu"+this.id).style.display='none';
      }
};
Menutree.prototype.add= function (node) {
    this.nodes[this.nodes.length]=node;
};
function clickCategory(vid,cid) {
  if(menutree.id == cid){
      if(menutree.open)
         menutree.open=false;
      else
         menutree.open=true;
      menutree.show();
  }
  else {
     menutree.init(cid,vid);
  }
}
function callback(){
   var txt=this.req.responseText;
    var res=eval("(" + txt + ")");
    menutree.id=res.cid;
    menutree.url=res.URL;
    readsubcats(res.childs);
   menutree.show();
}
function readsubcats(subcates){
   for(var i=0;i<subcates.length;i++){
       var menunode=new Menunode(subcates[i].cid,subcates[i].name,subcates[i].URL);
       addsubcats(subcates[i].childs,menunode);
       menutree.add(menunode);
    }
}
function addsubcats(subcates,menunode){
   for(var i=0;i<subcates.length;i++){
       var node=new Menunode(subcates[i].cid,subcates[i].name,subcates[i].URL);
       menunode.add(node);
      addsubcats(subcates[i].childs,node);
   }
}
