var windowId, sysUrl = top.sysUrl, toEdit;
var AllTree = null;
$(function() {
	initTreePanel();
});

// 初始化行政树
function initTreePanel() {
	if (AllTree)
		return;
	AllTree = $('#tree').tree(
			{
				lines : false,
				animate : false,
				multiple : false,
				editable : false,
				border : false,
				xzNode : false,
				parentField : "pid",
				textFiled : "name",
				idFiled : "id",
				collapse : false,
				data : [
				/*
				 * { name:'个人分组', id :"grTree", state :'open', treeId :'grTree'
				 * },{ name: '公共分组', id :"ggTree", state :'open', treeId
				 * :'ggTree' },
				 */
				/*
				 * { name: '行政分组' , id :"xzTree", state :'open', treeId
				 * :'xzTree' }
				 */],
				onClick : function(node) {
					if (node && node.xzbh) {
						setchecked(node);
					}
					if (node.children && node.children.length > 0) {
						for (var i = 0; i < node.children.length; i++) {
							if (node.children.children
									&& node.children.children.length > 0) {
								$(this).tree(
										node.state === 'closed' ? 'expand'
												: 'collapse', node.target);
							}
						}
					}
				},
				onBeforeExpand : function(node) {
					if (node && node.xzbh) {
						setchecked(node);
					}
					if (node.children && node.children.length > 0) {
						for (var i = 0; i < node.children.length; i++) {
							if (node.children.children
									&& node.children.children.length > 0) {
								node
										&& !$(this).tree("getChildren",
												node.target).length
										&& doSearch_group(node);
							}
						}
					}

				},
				onDblClick : function(node) {
					if (node && node.xzbh) {
						setchecked(node);
					}
				},
				onLoadSuccess : function() {
					var datas = {};
					datas.xzbh = "";
					setchecked(datas);

				}
			});

	$.ajax({
		type : 'post',
		async : false,
		dataType : 'json',
		// url:'ptxzsAction!query_Ptxzs.do'
		url : 'ywvideosyncaccountAction!query_Ptxzs.do'
	}).done(function(data) {
		if (data) {
			$.each(data, function(i, o) {
				o.state = 'closed';
				o.treeId = o.xzbh;
				o.name = o.xzmc;
				o.treeType = "xz";
				o.id = o.xzbh;
				o.pid = o.sjxzbh;
				o.start = 0; // iconCls
			})
			var xzfz = {};
			xzfz.treeType = "xz";
			xzfz.id = "-1";
			xzfz.pid = "-1";
			xzfz.start = 0; // iconCls
			xzfz.xzbh = "-1";
			xzfz.xzmc = "行政分组";
			xzfz.state = "open";
			xzfz.treeId = "-1";
			xzfz.sjxzbh = "-1";
			xzfz.name = "行政分组";
			data.push(xzfz)
			AllTree.tree('append', {
				/* parent : AllTree.tree('find', "xzTree").target, */
				data : data
			});
		}
	});
}

//选中行政节点
function setchecked(data){
	var xzbh = "";
	var childrenNodes;
	var childrenXzbh = new Array();
	if(data != null){
		xzbh = data.xzbh;
		if (xzbh == "-1") {
			xzbh = "";
		} else {
			// 获取子所有集节点
			childrenNodes = $('#tree').tree('getChildren',data.target);
			for (i = 0; i < childrenNodes.length; i++) {
				childrenXzbh.push(childrenNodes[i].xzbh);
			}
		}
	} else {
		xzbh = "-1";
	}
	childrenXzbh.push(xzbh);
	$("#sbtdspsrInfo").prop("src",'ywvideosyncchannel.jsp?childrenXzbh='+childrenXzbh);
}