var TreeMenu=new function()
{   
    this._url=null;
    
    //定义图标显示用数组
	this.icon=new Array();
	this.icon["folderOpen"]='pic/treeMenu_folderopened.gif';
	this.icon["folderClose"]='pic/treeMenu_folderclosed.gif';
	this.icon["open"]='pic/treeMenu_opened.gif';
	this.icon["close"]='pic/treeMenu_closed.gif';	
     
	/**
	 * 获取指定节点的子节点
	 * @param _parentId:指定节点的id
	 */ 
	this.getChildren=function(_parentId)
	{
	    if (this.alreadyGetChileren(_parentId))	{
		    var childContainer=document.getElementById(_parentId+"_subContainer");
		    if (childContainer) {
		        childContainer.style.display=(childContainer.style.display=="none")?"":"none";
		        var _parentNode=document.getElementById(_parentId);
		        if (_parentNode.firstChild && _parentNode.firstChild.tagName=="IMG") {
		            _parentNode.firstChild.src=(childContainer.style.display=="none")?this.icon["close"]:this.icon["open"];
		        }
		    }
		    return;
		}

		iwms.ajSrc=null;
		$.post(this._url,{act:'ajax',cmd:'fld',pId:_parentId},function(r){
			TreeMenu.addChildren(_parentId,r);
		},'xml');
	}
	
	/**
	 * 根据获取的数据，设置指定节点的子节点
	 * @param _parentId:指定节点id
	 * @param _data:获取的数据
	 */
	this.addChildren=function(_parentId,_data)
	{
	    if (this.alreadyGetChileren(_parentId)) {
	        return;
	    }
	    
	    var _parentNode=document.getElementById(_parentId);
	    if (_parentNode.firstChild && _parentNode.firstChild.tagName=="IMG") {
	        _parentNode.firstChild.src=this.icon["open"];
	    }
	    //子级容器，所有子级选项都放一个容器中
	    _nodeContainer=document.createElement("div");
		_nodeContainer.id=_parentId+"_subContainer";
		//子级容器放入父级容器
		_parentNode.appendChild(_nodeContainer);
		var _children=_data.getElementsByTagName("root")[0].childNodes;
		var _child=null;
		var _point=this;
		for(var i=0; i<_children.length; i++)
		{
			_child=_children[i];
			_node=document.createElement("div");
			_img=document.createElement("IMG");
			_span=document.createElement("SPAN");
			if (i!=_children.length-1) {
			    _node.style.cssText="padding-bottom:5px";
			}
			_node.innerHTML="";
			_node.id=_child.getAttribute("id");
			_span.style.cssText="cursor:pointer;line-height:16px;height:16px;padding-left:20px;background-image:url("+this.icon["folderClose"]+");background-repeat:no-repeat";
			//若节点存在下级节点
			if (_child.getAttribute("hasChildren")=="1") {
				_img.style.cssText="cursor:pointer;vertical-align:text-bottom;margin-right:4px";
				_img.src=this.icon["close"];
				_img.width=9;
				eval("_img.onclick=function(){TreeMenu.getChildren('"+ _child.getAttribute("id") +"')};");
				_node.appendChild(_img);
			}else{
				_span.style.marginLeft="13px";
			}
			_span.name="treeText";
			_span.title=_child.getAttribute("id");
			_span.onclick=function(){treeNodeChoosed(this);};
			_span.innerHTML=_child.firstChild.data;
			_node.appendChild(_span);
			//节点加入子级容器
			_nodeContainer.appendChild(_node);
		}
		_nodeContainer.style.cssText="border-left:0px solid #ccc;margin-left:7px;margin-top:5px;padding-left:10px";
	}
	
	/**
	 * 判断指定节点是否已经获取子节点
	 * @param _nodeId 指定节点id
	 * @return [boolean]true为已经获取，false为未获取
	 */
	this.alreadyGetChileren=function(_nodeId)
	{
	    var obj=document.getElementById(_nodeId+"_subContainer");
	    if (obj){
	        return true;
	    }
	    return false;
	}	
}

/**
 * 点击菜单后的动作
 */
function treeNodeChoosed(_obj)
{
    var choosedColor="lightblue";
    var unChoosedColor="#eee";
    var o_ifm=document.getElementById("ifm_files");
    if (_obj.style.backgroundColor==choosedColor) {
        _obj.style.backgroundColor=unChoosedColor;           
    }else{
        //var allNodeText=document.getElementsByName("treeText");
        var allNodeText=document.getElementsByTagName("SPAN");
        for (var i=0; i<allNodeText.length; i++){
            allNodeText[i].style.backgroundColor=unChoosedColor;
            allNodeText[i].style.backgroundImage="url("+TreeMenu.icon["folderClose"] +")";
        }
        _obj.style.backgroundColor=choosedColor;
        _obj.style.backgroundImage="url("+ TreeMenu.icon["folderOpen"] +")";
        o_ifm.src="admin_upfiles.aspx?folder="+escape(_obj.title);
    }    
}