var treeObj = null;
        var setting = {
        		check : {
        			enable : true,
        			chkDisabledInherit : true
        		},
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                view: {
                    fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
                },
            	callback : {
            		beforeCheck : beforeCheck,
            		onCheck : onCheck,
            		onClick: zTreeOnClick
            	}
        };
        
// tree节点选中之前进行判断
var className = "dark";
function beforeCheck(treeId, treeNode) {
	className = (className === "dark" ? "" : "dark");
	return (treeNode.doCheck !== false);
}

//计算tree选中的节点，for循环去除计算父节点在内
var treecheck = [];
function onCheck() {
	var checkCount = 1, totalcount = 4;
	var zTree = jQuery.fn.zTree.getZTreeObj("tree");
	if (zTree != null) {
		checkCount = zTree.getCheckedNodes(true).length;
		var nodes = zTree.getCheckedNodes(true);
		totalcount = checkCount;
		treecheck = [];

		for ( var i = 0; i < totalcount; i++) {
			if (nodes[i].id > 4000) {
				treecheck.push(nodes[i].id);
				/*********************************
				 * 设置加载实时数据界面 add by yexb -----start
				 *********************************/
				//实时谐波频谱，实时数据列表，实时矢量图
				if(blrealharmonic){
					window.main_right.refreshRealHarmoinc();
					blrealharmonic=false;
				}
				else if(blrealdatalist){
					window.main_right.refreshRealDatalist();
					blrealdatalist=false;
				}
				else if(blrealvector){
					window.main_right.refreshRealVector();
					blrealvector=false;
				}
			} else {
				checkCount--;
			}
		}

		if (number != 0) {
			if (checkCount >= number) {
				disabledNode(true);
			} else {
				disabledNode(false);
			}
		}
	}
}

function zTreeOnClick() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    
    if (zTree != null) {
        var nodes = zTree.getSelectedNodes();   //获取树控件选中对象集合
        
        // 关闭所有节点的高亮
        var allNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < allNodes.length; i ++) {
            allNodes[i].highlight = false;
            treeObj.updateNode(allNodes[i]);
        }
        
        // 高亮并展开搜索到的节点
        for (var i = 0; i < nodes.length; i ++) {
            nodes[i].highlight = true;
            treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        }
        
        if (nodes[0].level == 5) {    //如果选中监测点信息则进行处理，其他信息不处理

            //修改选中监测点的信息展示
        	var subvname = nodes[0].getParentNode();
            var devname = nodes[0].getParentNode().getParentNode();
            var bdname = nodes[0].getParentNode().getParentNode().getParentNode();
            var gdname = nodes[0].getParentNode().getParentNode().getParentNode().getParentNode();
            $('#pointInfo').text(gdname.name + '->' + bdname.name + '->' + devname.name + '->' + subvname.name + '->' + nodes[0].name);

        	var line_index = nodes[0].nodeIndex;
            $("#lineIndex").val(line_index);
            $("#lineName").val(nodes[0].name);
            
            var status = nodes[0].status;
            var state = nodes[0].state;
            if(state == 2){
            	$("#image").val("gray");
            }else if(state == 1){
            	$("#image").val("yellow");
            }else{
            	if(status == 1){
            		$("#image").val("green");
            	}else{
            		$("#image").val("red");
            	}
            }
			var endTime=getEndTime();
			var startTime=getStartTime("年份", endTime);
        }
    }
}
 
function  zTreeOnAsyncSuccess(pastIndex){
    var zTree = $.fn.zTree.getZTreeObj("tree");
    
    var nodes = zTree.getNodes();    
    zTree.expandNode(nodes[0], true);    
   
    var childNodes = zTree.transformToArray(nodes[0]);    
    
    if(pastIndex.length==0){
	    var i=0;
	    for(i=0;i<childNodes.length;i++){
	    	var type=childNodes[i].level;
	    	if(type==5){
	    		var subvname = childNodes[i].getParentNode();
	            var devname = childNodes[i].getParentNode().getParentNode();
	            var bdname = childNodes[i].getParentNode().getParentNode().getParentNode();
	            var gdname = childNodes[i].getParentNode().getParentNode().getParentNode().getParentNode();
	            
	            zTree.expandNode(gdname,true);
	            zTree.expandNode(bdname,true);
	            zTree.expandNode(devname,true);
	            zTree.expandNode(subvname,true);
	            zTree.expandNode(childNodes[i], true); 
	            
	            $('#pointInfo').text(gdname.name + '->' + bdname.name + '->' + devname.name + '->' + subvname.name + '->' + childNodes[i].name);
	            
	        	var line_index = childNodes[i].nodeIndex;
	            $("#lineIndex").val(line_index);
	            $("#lineName").val(childNodes[i].name);
	            
	            var status = childNodes[i].status;
	            var state = childNodes[i].state;
	            if(state == 2){
	            	$("#image").val("gray");
	            }else if(state == 1){
	            	$("#image").val("yellow");
	            }else{
	            	if(status == 1){
	            		$("#image").val("green");
	            	}else{
	            		$("#image").val("red");
	            	}
	            }
	            
				var endTime=getEndTime();
				var startTime=getStartTime("年份", endTime);
				// 关闭所有节点的高亮
	            var allNodes = treeObj.transformToArray(treeObj.getNodes());
	            for (var n = 0; n < allNodes.length; n ++) {
	                allNodes[n].highlight = false;
	                treeObj.updateNode(allNodes[n]);
	            }
	            
	            // 高亮并展开搜索到的节点
	            childNodes[i].highlight = true;
	            treeObj.updateNode(childNodes[i]); // 更新节点，让高亮生效
	            
	    		break;
	    	}
	    }
    }else{
    	var i=0;
	    for(i=0;i<childNodes.length;i++){
	    	var index=childNodes[i].nodeIndex;
	    	if(index==pastIndex){
	    		var subvname = childNodes[i].getParentNode();
	            var devname = childNodes[i].getParentNode().getParentNode();
	            var bdname = childNodes[i].getParentNode().getParentNode().getParentNode();
	            var gdname = childNodes[i].getParentNode().getParentNode().getParentNode().getParentNode();
	            
	            zTree.expandNode(gdname,true);
	            zTree.expandNode(bdname,true);
	            zTree.expandNode(devname,true);
	            zTree.expandNode(subvname,true);
	            zTree.expandNode(childNodes[i], true); 
	            
	            $('#pointInfo').text(gdname.name + '->' + bdname.name + '->' + devname.name + '->' + subvname.name + '->' + childNodes[i].name);
	            
	        	var line_index = childNodes[i].nodeIndex;
	            $("#lineIndex").val(line_index);
	            $("#lineName").val(childNodes[i].name);
	            
	            var status = childNodes[i].status;
	            var state = childNodes[i].state;
	            if(state == 2){
	            	$("#image").val("gray");
	            }else if(state == 1){
	            	$("#image").val("yellow");
	            }else{
	            	if(status == 1){
	            		$("#image").val("green");
	            	}else{
	            		$("#image").val("red");
	            	}
	            }
	            
				var endTime=getEndTime();
				var startTime=getStartTime("年份", endTime);
				// 关闭所有节点的高亮
	            var allNodes = treeObj.transformToArray(treeObj.getNodes());
	            for (var n = 0; n < allNodes.length; n ++) {
	                allNodes[n].highlight = false;
	                treeObj.updateNode(allNodes[n]);
	            }
	            
	            // 高亮并展开搜索到的节点
	            childNodes[i].highlight = true;
	            treeObj.updateNode(childNodes[i]); // 更新节点，让高亮生效
	    		break;
	    	}
	    }
    }
    	
}


        function getTree() {
        	jQuery.ajax({
        		type : 'POST',
        		dataType : "json",
        		url : '/pqs9200/device/tree',
        		data : {},
        		async:false,
        		error : function() {},
        		success : function(data) {
        			var datalist = [];
        			for ( var i = 0; i < data.length; i++) {
        				var str = '';
        				str += '{' + "id:" + data[i].id;
        				str += ',' + "name:'" + data[i].name + "'";
        				str += ',' + "nodeIndex:" + data[i].nodeIndex;
        				str += ',' + "nodeType:" + data[i].nodeType;
        				str += ',' + "ppId:" + data[i].ppId;
        				if(data[i].nodeType == "0"){
        					str += ',' + "open:" + true;
        				}else{
        					str += ',' + "open:" + false;
        				}
        				str += ',' + "state:" + data[i].state;
        				str += ',' + "status:" + data[i].status;
        				str += ',' + "pId:" + data[i].pId + '}';
        				datalist.push(str);
        			}
        			datalist = eval('[' + datalist + ']');// string->json数组
        			var zNodes = datalist;
        			var k = 0;
        			for ( var index = 0; index < zNodes.length; index++) {
        				if (zNodes[index].nodeType == 0) {
        					zNodes[index].icon = "../images/img/base.png";
        				}else if (zNodes[index].nodeType == 1) {
        					zNodes[index].icon = "../images/img/power.png";
        				} else if (zNodes[index].nodeType == 2) {
        					zNodes[index].icon = "../images/img/substation.png";
        				} else if (zNodes[index].nodeType == 3) {
        					zNodes[index].icon = "../images/img/device.png";
        				} else if (zNodes[index].nodeType == 4) {
        					zNodes[index].icon = "../images/img/subvstation.png";
        				} else if (zNodes[index].nodeType == 5) {
        					if (zNodes[index].state == 2) {
        						zNodes[index].icon = "../images/img/gray.png";
        					} else if (zNodes[index].state == 1) {
        						zNodes[index].icon = "../images/img/yellow.png";
        					} else {
        						if(zNodes[index].status == 0){
        							zNodes[index].icon = "../images/img/green.png";
        						}else{
        							zNodes[index].icon = "../images/img/red.png";
        						}
        					}
        				}
        			}

        			treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
        		}
        	});
        }


        $(document).ready(function(){
        	getTree();
        });
        
        
        // 模糊搜索name满足条件的节点
        function searchNode(value) {
            if (value == "") return;
            
            // 获取搜索的节点
            var nodes = treeObj.getNodesByParamFuzzy("name", value);
            // 关闭所有节点的高亮
            var allNodes = treeObj.transformToArray(treeObj.getNodes());
            for (var i = 0; i < allNodes.length; i ++) {
                allNodes[i].highlight = false;
                treeObj.updateNode(allNodes[i]);
            }
            // 展开第一层节点
            treeObj.expandAll(false);
            treeObj.expandNode(treeObj.getNodes()[0],true);
            // 高亮并展开搜索到的节点
            for (var i = 0; i < nodes.length; i ++) {
                nodes[i].highlight = true;
                treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
                treeObj.expandNode(nodes[i].getParentNode(),true);
            }
        }
        
        // 设置高亮字体
        function setHighlight(treeId, treeNode) {
            return (!!treeNode.highlight) ? {color:"#000000", "font-weight":"bold"} : {color:"#666", "font-weight":"normal"};
        }
        
        // 延时搜索，用于输入框的按键事件，如果不延时，树就会不断的跳动
        var timeoutId = null;
        function searchNodeLazy(value) {
            if (timeoutId) {
                clearTimeout(timeoutId);
            }
            timeoutId = setTimeout(function(){
                searchNode(value);  
            }, 500);
        }
        
        //根据传入的json对象，更新终端图表
        function updateIcon(json){
        	var lineIndex = $("#lineIndex").val();
        	
        	var jsonArr=[];
        	for(var i =0 ;i < json.length;i++){
                jsonArr[i] = json[i];
        	}
        	
        	var zTree = $.fn.zTree.getZTreeObj("tree");
            
            var nodes = zTree.getNodes();    
            var childNodes = zTree.transformToArray(nodes[0]);
            
            for(var i=0;i<childNodes.length;i++){
            	var type=childNodes[i].level;
    	    	if(type==5){ 
    	    		for(var n=0;n<jsonArr.length;n++){ 
	    				var status = jsonArr[n].status;
	    				var devFlag = jsonArr[n].devflag;
	    				
    	    			if(jsonArr[n].lineid == childNodes[i].nodeIndex){
    	    				if(jsonArr[n].lineid == lineIndex){
    	    					if (devFlag == 2) {
        	    					childNodes[i].icon = "../images/img/gray.png";
        	    					$("#image").val("gray");
            					} else if (devFlag == 1) {
            						childNodes[i].icon = "../images/img/yellow.png";
            						$("#image").val("yellow");
            					} else {
            						if(status == 1){
            							childNodes[i].icon = "../images/img/green.png";
            							$("#image").val("green");
            						}else{
            							childNodes[i].icon = "../images/img/red.png";
            							$("#image").val("red");
            						}
            					}
        	    				
        	    				zTree.updateNode(childNodes[i]);
    	    				}else{
	    	    				if (devFlag == 2) {
	    	    					childNodes[i].icon = "../images/img/gray.png";
	        					} else if (devFlag == 1) {
	        						childNodes[i].icon = "../images/img/yellow.png";
	        					} else {
	        						if(status == 1){
	        							childNodes[i].icon = "../images/img/green.png";
	        						}else{
	        							childNodes[i].icon = "../images/img/red.png";
	        						}
	        					}
	    	    				
	    	    				zTree.updateNode(childNodes[i]);
    	    				}
    	    			}
    	    		}
    	    	}
            }
        }