//var winId	= $.getUrlParam("winId");
//var channelsetid = top.$("#"+winId).panel("options").data;
var AllTree = null;
var nodeLimit = 20,nodeMore = "加载更多..";
$(function(){
	initTreePanel();
	//加载下拉选项
	/* setTimeout(loadValue,500); */
});

//选中行政节点
function setchecked(data){
	var id = "-1";
	if(data){
		id = data.id;
	}
	/*var id = "sbGroup";//设备分组
	var flag = "sbGroup";
	if(data && data.id!="xzTree"  ){
		id = data.id;
		if(!data.taskGroup){
			flag = "xzGroup";//行政分组
		}else{
			flag = "rwGroup";//任务分组
		}
	}*/
	$("#ywvideocurrentstate").prop("src",'ywvideocurrentstate.jsp?id='+id);		
}
/*function doSearch_group(node){
	if(node.treeType == 'xz'){
		loadXzChannelToTree(node);
	}
}*/

/*function loadXzChannelToTree(node , startIndex){
	if( node ) {
		startIndex = startIndex || 0;
		var param = {
				xzbh : node.treeId,
				sbtd_start: startIndex,
				sbtd_limit: nodeLimit
		};
		
		$.ajax({
			 type:'post',
			 async:false,
			 dataType : 'json',
			 data:param,
			 url:'menuConfigAction!getXzTreeChannelListFromSystem.do'
		 }).done(function(rs){
			 if(rs && rs.length){
					var onLineList = [];
					var unLineList = [];
					for(var i=0;i<rs.length;i++){
						 var obj = VideoBlockUtil.showChannelIcon_xztree(rs[i]);
						rs[i].iconCls = obj.icon; 
						
						if(rs[i].sfzx == "1"){
							onLineList.push(rs[i]);
						}else{
							unLineList.push(rs[i]);
						}
					}
					//var list = onLineList.concat(unLineList);
					var list = rs;
					if(rs.length == nodeLimit){
						var mm = {};
						mm.id = node.id+"_more";
						mm.treeType = "xz";
						mm.pid	=	node.id;
						mm.name = nodeMore;
						list.push(mm);
					}
					AllTree.tree('append', {
						parent : AllTree.tree('find', node.id).target,
						data   : list
					});
					
				}
		})
		
	}
}*/

/*function doSearchMore(node,startIndex){
	var nodeMore = AllTree.tree('find', node.id+"_more");
	AllTree.tree('remove', nodeMore.target);
	loadXzChannelToTree(node,startIndex);
}
function checkChildren(data){
	if(data && data.children && data.children.length>0 ){
		for(var i=0;i<node.children.length;i++){
			if(node.children.children && node.children.children.length>0){
				return true;
			}
		}
	}
    for (var i in data) {
        if (data[i].name == name) {
            break;
        } else {
            getArray(data[i].children, name);
        }
    }
}*/
//初始化行政树
function initTreePanel(){
	if(AllTree) return;
	AllTree = $('#tree').tree({
	        lines:false,
			animate: false, 
	    	multiple:false,
	    	editable:false,
	    	border:true,
	    	xzNode : false,
	    	parentField:"pid",  
	    	textFiled:"name",  
	    	idFiled:"id",
	    	collapse :false,
	    	data: [],
	    	onClick:function(node){
	    		if(node  && node.id){
					setchecked(node);
				} 
		 	},
		 	onBeforeExpand:function(node){
		 		if(node && node.id){
					setchecked(node);
				}
			},
			onDblClick:function(node){
				if(node  && node.id){
					setchecked(node);
				}
			},
			onLoadSuccess:function(){
				setchecked("");
				
			}
	});
	
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 //url:'ptxzsAction!query_Ptxzs.do'
		 url:'menuConfigAction!query_Ptxzs.do'
	 }).done(function(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		= "xzTree";
			xzfz.start 	= 0; //iconCls
		 	xzfz.xzbh = "-1";
		 	xzfz.xzmc = "行政分组";
		 	xzfz.state = "closed";
		 	xzfz.treeId = "-1";
		 	xzfz.sjxzbh = "xzTree";
		 	xzfz.name = "行政分组";
			data.push(xzfz);
			/*data.push({	
				name: '设备分组' ,
				id  	:"xzTree",
				state	:'open',
				treeId	:'xzTree',
				start :0
			});
			
			$.ajax({
				type:'post',
				async:false,
				dataType : 'json',
				url:'ywvideotaskAction!query_Ywvideotask.do'
			}).done(function(res){
				$.each(res,function(i,o){
					o.state 	='closed';
					o.treeId 	= o.taskid;
					o.name 		= o.taskname;
					o.treeType 	= "xz";
					o.id		= o.taskid;
					o.pid		= "-3";
					o.start 	= 0; //iconCls
					o.taskGroup = true;
					data.push(o);
				}) 
				var xzfzTask = {};
				xzfzTask.treeType 	= "xz";
				xzfzTask.id		= "-3";
				xzfzTask.pid		= "xzTree";
				xzfzTask.start 	= 0; //iconCls
				xzfzTask.xzbh = "-3";
				xzfzTask.xzmc = "任务分组";
				xzfzTask.state = "closed";
				xzfzTask.treeId = "-3";
				xzfzTask.sjxzbh = "xzTree";
				xzfzTask.name = "任务分组";
				xzfzTask.taskGroup = true;
				data.push(xzfzTask);*/
				
				AllTree.tree('append', {
					/*parent : AllTree.tree('find', "xzTree").target, */
					data   : data
				});
			});
}
