var zTree;
setting = {
		editable: false,//参数设置为true便可使用拖拽功能
		edit_renameBtn:false,
		edit_removeBtn:false,
		dragCopy:true,//拖拽功能为复制然后在目的菜单添加
		dragMove:false,//拖拽功能为先删除然后在目的菜单添加
		data:	{
					key: {
							children: "nodes"
						 },
			simpleData : {
							enable : false,
							idKey : "id",
							pIdKey : "pId",
							rootPId : null
						 }

				},
		view: 	{
					showLine: true,
					showTitle:false,
					showIcon:false,
					selectedMulti: false
				},
		callback:{
					onClick: zTreeOnClick,
					//beforeClick: zTreeBeforeClick,
					 onRightClick: OnRightClick
				 }
	};
$(document).ready(function()
{
	reloadTree();
	updateCatinfo();
	getBreadcrumb();
});
var preSelectedNode1;
//单击事件
function zTreeOnClick(event, treeId, treeNode) 
{
	if (treeId=="treeDemo") 
	{
		if (preSelectedNode1 == treeNode) 
		{
			zTree.cancelSelectedNode();
			preSelectedNode1 = null;
		} 
		else 
		{
			preSelectedNode1 = treeNode;
		}
	}
}
function showRMenu1(type, x, y) 
{
	$("#AddMenu1").show();
	$("#AddMenu1").css({"top":y+"px", "left":(x+10)+"px", "display":"block"});
}
function showRMenuModi(type, x, y) 
{
	$("#ModifyMenu").show();
	$("#ModifyMenu").css({"top":y+"px", "left":(x+10)+"px", "display":"block"});
}
//hidden rightclick menu 
function hideRMenu()
{
	$("#AddMenu1").hide();
	$("#ModifyMenu").hide();
}
function OnRightClick(event,treeId, treeNode)
{   
    var count=0;
	var a1,a2;
	var tree_area = document.getElementById("tree_area"); 
	if(count==0)
	{
	var a1 = tree_area.scrollTop;
	count++;
	}
	if (!treeNode) 
	{
		zTree.cancelSelectedNode();			
		hideRMenu();
	} 
	else if (treeNode && !treeNode.noR) 
	{
		if (event.target.tagName != "a" && $(event.target).parents("a").length == 0) 
		{			
			zTree.cancelSelectedNode();	
			hideRMenu();
		} 
		else if(treeId =="treeDemo") 
		{
			zTree.selectNode(treeNode);
			var rightclick_node = zTree.getSelectedNodes();
			$("#CurDescId").val(rightclick_node[0].ID);
			if(rightclick_node[0].level==1)//Add  descriptor
			{		
				$("#Desc_OperateType").val("1");
				showRMenu1("node", event.clientX, event.clientY-tree_area.scrollTop+a1);
				$("#ModifyMenu").hide();
			}
			else
			{
				showRMenuModi("node", event.clientX, event.clientY-tree_area.scrollTop+a1);
				$("#AddMenu1").hide();
			}
		}
	}
}
//树菜单的初始化。
function reloadTree() 
{
	zTree = $.fn.zTree.init($("#treeDemo"),setting, CATNodes);
}	
function updateCatinfo()
{
	$("#curpsiid").val(psiid);
	$("#version_num").val(version_num);
}