﻿var TreeMenu=Class.create();
TreeMenu.prototype={
    initialize:function(container,struct,path,level,approot,toShow){
        this.level=level?level:0;
        for(var i=0;i<struct.length;i++){
            var _l1=document.createElement('div');
            //_l1.className='treemenu_l'+this.level;
            _l1.Id=struct[i].Id;
            _l1.ItemLink=struct[i].ItemLink;
            var span=document.createElement('span');
            span.appendChild(document.createTextNode(struct[i].ItemText));
            _l1.appendChild(span);
            container.appendChild(_l1);
            var _isParent=false;
            var _newPath='';
            if(path.length>0){
                var _path=path.split(',');
                if(_path[0]==_l1.Id){
                    _isParent=true;
                    _path.shift();
                    _newPath=_path.join(',');
                }
            }
            var me=this;
            Event.observe(_l1,'click',function(e){me.clicked(e,approot);Event.stop(e);});
            //var _toShow=this.level==0 || (this.level>0 && _isParent);
            var _toShow=this.level==0 || toShow;
            _l1.style.display=_toShow?'':'none';
            _l1.className=_isParent?'treemenu_l'+this.level+'_on':'treemenu_l'+this.level;
            var _level=this.level+1;
            if(struct[i].Children) new TreeMenu(container,struct[i].Children,_newPath,_level,approot,_isParent);            
        }
    },
    clicked:function(e,approot){
        var src=e.srcElement || e.target;
        while(src.tagName!='DIV') src=src.parentNode;
        var _link=src.ItemLink;
        if(_link.length>0)
            window.location.href=approot+_link;
    }    
}
