function ClsPubTree(cfg){
	var _this = this;
	_this.schbox = 'div_search';
	_this.waitWindow = null;
	_this.tree = null;
	_this.c_app = null;
	_this.clsRegular = null;
	_this.id_base = '';
	_this.window = null;
	_this.tree = null;
	_this.grid = null;
	_this.panel = null;
	_this.btnpnl = null;
	_this.district_id = 0;
	_this.user_id = 0;
	_this.currentTreeNode = null;
	_this.tree_w = 0;
	_this.panel_width = 0;
	_this.panel_height = 0;
	_this.textfield_id = '_cn2live_district_tree_textfield';
	_this.treepanel_id = '_cn2live_district_tree_panel';
	_this.application_id = 0;
	_this.node_clicked = function(node, e){};
	_this.hdl_node_mouseover = function(node, e){};
	_this.hdl_node_mouseout = function(node, e){};
	_this.hdl_node_loaded = function(node){};
	_this.icon_root = '';
	_this.expand_all = false;
	_this.dispub_form = null;
	_this.dispub_window = null;
	var tree_toolbar = null;
	
	var oldPosition = null;
	var oldNextSibling = null;

	_this.icon_root = Cfg(cfg,'icon_root','');
	
	var tree_url = g_root + 'pub_tree.php?opr=tree&cam_chk='+Cfg(cfg,'cam_chk',0)+'&tip='+Cfg(cfg,'tip',0);
	tree_url += "&icon_root="+_this.icon_root;
	if (Cfg(cfg,'show_runtime',0)==1) tree_url += "&show_runtime="+Cfg(cfg,'show_runtime',0);
	if (Cfg(cfg,'check_endgroup',1)==1) tree_url += "&check_endgroup="+Cfg(cfg,'check_endgroup',1);
	if (Cfg(cfg,'appcodes','')!=='') tree_url += "&appcodes="+Cfg(cfg,'appcodes','');
	if (Cfg(cfg,'sameAppOf',0)!=0) tree_url += "&sameAppOf="+Cfg(cfg,'sameAppOf',0);
	
	
    this.createTreeToolBar = function() {
		var tbar = new Ext.Toolbar({
			xtype:'toolbar'
			,items:[
				{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'}
				,{text:'展开',handler:function(){
					//_this.tree.getRootNode().expandChildNodes(true);
					if (_this.currentTreeNode) {
						_this.currentTreeNode.expand();
						_this.currentTreeNode.expandChildNodes(true);
					}
					else{
						Ext.QuickMsg.msg('您好', '请选择一个区域');
					}
				}}
				,{text:'收缩',handler:function(){
					if (_this.currentTreeNode) {
						_this.currentTreeNode.collapse();
						_this.currentTreeNode.collapseChildNodes(true);
					}
					else{
						Ext.QuickMsg.msg('您好', '请选择一个节点');
					}
				}}
				,'-'
				,{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'}
				,{xtype:'textfield', id:_this.textfield_id, width:100}
				,{text:'添加',handler:function(){
					_this.add();
				}}
				,{text:'修改',handler:function(){
					_this.modify();
				}}
				,{text:'删除',handler:function(){
					_this.del();
				}}
				,'-'
				,{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'},{xtype: 'tbspacer'}
				,{text:'取消公开',handler:function(){
					disPublic();
				}}
			]
		});
		return tbar;
    };
	var getChkCamIds = function() {
		var ids = '';
		Ext.select('._camchk').each(function(extobj){
			if (extobj.dom.getAttribute('ex_checked')=='true') {
				var id = extobj.dom.getAttribute('ex_value');
				ids += (ids.length==0 ? "" : ",") + id;
			}
		});
		return ids;
	};
    var disPublic = function() {
		var ids = getChkCamIds();
		if (!ids) {Ext.QuickMsg.msg('','请勾选要取消公开的视频点'); return false;};
		
		showDispubWindow(null, ids, dispub_msg_title, dispub_msg_content);
    };
	var showDispubWindow = function(animobj, ids, msg_title, msg_content){		
		if (_this.dispub_form) {_this.dispub_form.destroy(); _this.dispub_form=null;}
		if (!_this.dispub_form) {
			_this.dispub_form = new Ext.form.FormPanel({
				id: 'dispub_form', autoHeight:true, frame:true, bodyBorder:false, border:false, bodyStyle: 'padding:10px',
				items: [{xtype:'hidden', name:'opr', value:'dispub'},
						{xtype:'hidden', name:'ids', value:ids},
						{xtype:'textfield',name:'msg_title',value:msg_title,fieldLabel:"消息标题",width:320},
						{xtype:'textarea',name:'msg_content',value:msg_content,fieldLabel:"消息内容",width:320,height:100}
				]
			});
		}
		
		if (_this.dispub_window) {_this.dispub_window.close(); _this.dispub_window.destroy(); _this.dispub_window=null;}
		if (!_this.dispub_window) {
			_this.dispub_window = new Ext.Window({
				closeAction: 'close', layout: "fit", modal: true, plain: true, resizable: false, header: true, closable: true, shadow: true,
				autoHeight: true, width: 500, bodyStyle: 'padding:0px', border: false,
				title: '消息设定',
				items:[_this.dispub_form],
				buttons:[
					{text:'确定', handler:function(){doDisPub();}}
					,{text:'取消', handler:function(){_this.dispub_window.close();}}
				],
				keys:[{  
			         key:Ext.EventObject.ENTER,   
			         fn:doDisPub,   
			         scope:this
			    }]
			});
		}
		_this.dispub_window.show(animobj);
	};
	var doDisPub = function() {
		showWaitWindow();
		var paras = _this.dispub_form.getForm().getValues(true);
		
		//提交
		Ext.Ajax.request({
	        url: g_root + 'pub_tree.php',
	        params: paras,
	        success: function(response, options){
	        	hideWaitWindow();
	            try{
		        	var rjdata = Ext.util.JSON.decode(response.responseText);
		            if (rjdata.success==true) {
		            	var ids = _this.dispub_form.getForm().findField('ids').value;
		            	var idy = ids.split(',');
		            	
		            	Ext.QuickMsg.msg('', '已取消公开视频点 ' + idy.length + ' 个');
		            	
		            	//移除节点
		            	_this.dispub_window.close();
		            	for (var i=0; i<idy.length; i++) {
		            		var node = _this.tree.getNodeById('c_'+idy[i]);
		            		node.parentNode.removeChild(node);
		            	}
		            }
		            else{
		            	Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), width:280, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
		            }
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
	        },
	        failure: function(response, options){
	        	hideWaitWindow();
	        	alert('意外错误 [pub_tree:doDisPub]\r\n\r\n'+qpDHanzi(response.responseText));
	        }
	    });
	}

	this.createTree = function(tree_width, tree_height, root_visible, tree_toolbar) {
		if (typeof(tree_width)=='undefined') tree_width=253;
		//alert('beforcreateTree');
		var tmpobj = {
			plugins: new NodeMouseoverPlugin(),
	    	width:tree_width, height:tree_height
	    	,autoScroll: true
	    	,containerScroll: true
	    	,border: false
			,animate: false		//展开,收缩动画,false时,则没有动画效果
			,enableDrag: false	//树的节点可以拖动Drag(效果上是),注意不是Draggable
			,enableDD: false	//不仅可以拖动,还可以通过Drag改变节点的层次结构(drap和drop)
			,enableDrop: false	//仅仅drop
			,lines: true	//节点间的虚线条
			,loader: new Ext.tree.TreeLoader({dataUrl:tree_url})	//加载节点数据
			,root: new Ext.tree.AsyncTreeNode({id:'0',text:'公开视频',expanded:true, icon:g_root+"images/app_closed.png"})
			,rootVisible: root_visible	//false不显示根节点,默认为true
			,trackMouseOver:false	//false则mouseover无效果
			,useArrows:false		//小箭头
			//,tbar:tree_toolbar
	    };
	    if (typeof(tree_toolbar)!='undefined' && tree_toolbar!=null) tmpobj.tbar = tree_toolbar;
		
	    var tree = new Ext.tree.TreePanel(tmpobj);
	    
	    _this.currentTreeNode = tree.getRootNode();
	    
		if (_this.expand_all) {
			tree.on('load',function(node){
				_this.hdl_node_loaded(node);
				node.expand();
				node.expandChildNodes();
			});
		}
	    //alert('aftercreateTree');
		return tree;
	}
	this.createPanel = function(w, h, tree, grid, treetbar) {
		var panel = new Ext.Panel({
			header: false, width:w, height:h, layout:'border', defaults: {border:true}, border:true
			,items:[
				grid
				,{xtype:'panel', id:_this.treepanel_id, layout:'fit', region:'center'}
			]
		});
		return panel;
	}
	this.createWindow = function(w, title) {
		var window = new Ext.Window({
			closeAction: 'close', layout: "fit", modal: true, plain: true, resizable: false, header: true,
			title: title,
			closable: true, shadow: true,
			width: w, autoHeight: true,
			bodyStyle: 'padding:0px',
			border: false, 
			defaults: {border:true}
		});
		return window;
	}
	this.getGrid = function(w) {
		if (!_this.c_app) {
			var cfg = {no_sm:true};
			_this.c_app = new ClsApplication(cfg);
			_this.c_app.editmode = false;
			_this.c_app.page_size = 0;
			_this.c_app.loadData();
			/*_this.c_app.store.on('load',function(){
				
			});*/
		}
		_this.grid = _this.c_app.createGrid(null, w, 'west');
		_this.c_app.grid = _this.grid;
		return _this.grid;
	}

	this.createButtonPanel = function(w,h) {
		var panel = new Ext.Panel({
			cls: 'v_middle', width:w, autoHeight:true, border:false, defaults:{border:false}, bodyStyle:"padding-top:"+((h-50)/2)+"px"
			/*,items:[
				{cls: 'v_middle', xtype:'button', text:'<-保存', handler:saveAccess}
			]*/
		});
		return panel;
	}
    
    this.doIni = function(renderto_id, panel_width, panel_height, title, tree_w, grid_w) {
    	if (renderto_id=='' || renderto_id==null) renderto_id  = false;
    	_this.tree_w = tree_w;
    	_this.panel_width = panel_width;
    	_this.panel_height = panel_height;
    	
    	//_this.tree = _this.createTree(tree_w, panel_height, true, '授权用户');
    	/*_this.tree.on('click', function(node, e){
    		_this.currentTreeNode = node;
    		showRegulars(node);
    	});*/
    	
    	_this.btnpnl = _this.createButtonPanel(10, panel_height);
    	_this.grid = _this.getGrid(grid_w);
    	_this.grid.on('rowclick', function(gd, rowIndex, e){
			da = _this.grid.getStore().getAt(rowIndex).data;
			_this.application_id = da.Id;
			showTree(da.Id, da.Name);
    	});
    	
    	tree_toolbar = _this.createTreeToolBar();
    	_this.panel = _this.createPanel(panel_width, panel_height, null, _this.grid, null);
    	
    	
    	
    	if (!renderto_id) {
    		_this.window = _this.createWindow(panel_width, title);
    		_this.window.show();
    		
    		_this.window.add(_this.panel);
    		_this.window.doLayout();
    		_this.window.center();
    	}
    	else{
    		_this.panel.render(renderto_id);
    		_this.panel.doLayout();
    	}
    }
    
    this.getMTree = function() {
   		_this.district_id = 0;
    	
    	//存在则销毁
    	if (_this.tree) {
    		//tree_toolbar.destroy();
    		_this.tree.destroy();
    		if (Ext.getCmp(_this.treepanel_id))	Ext.getCmp(_this.treepanel_id).remove(_this.tree);
    	}
    	
    	//工具条
    	if(Cfg(cfg,'control_bar')==true)
			var tmpbar = _this.createTreeToolBar();
		else
			var tmpbar = null;

		//树
    	_this.tree = _this.createTree(_this.tree_w, _this.panel_height, true, tmpbar);
    	_this.tree.on('click', function(node, e){
    		if (node.attributes.classtype) {
    			if (node.attributes.classtype=='group') {
		    		if (Ext.getCmp(_this.textfield_id)) {
		    			Ext.getCmp(_this.textfield_id).setValue(node.text);
		    		}    				
    			}
    		}
    		
    		_this.currentTreeNode = node;
    		_this.node_clicked(node, e);
    	});
		_this.tree.on('mouseover', function(node, e){
			_this.hdl_node_mouseover(node,e);
		});
		_this.tree.on('mouseout', function(node, e){
			_this.hdl_node_mouseout(node,e);
		});
    	if(Cfg(cfg,'control_bar')==true){	
    		_this.tree.enableDrag=true;
    		_this.tree.enableDD=true;
    		_this.tree.enableDrop =true;
			_this.tree.on('startdrag', function(tree, node, event){
			    oldPosition = node.parentNode.indexOf(node);
			    oldNextSibling = node.nextSibling;
		});		
			_this.tree.on('movenode', function(tree, node, oldParent, newParent, index){
				moveNode(tree, node, oldParent, newParent, index);
		});}
    }
    
    
    var createWindow = function(w, title) {
		var window = new Ext.Window({
			closeAction: 'close', layout: "fit", modal: true, plain: true, resizable: false, header: true,
			title: title,
			closable: true, shadow: true,
			width: w, autoHeight: true,
			bodyStyle: 'padding:0px',
			border: false, 
			defaults: {border:true}
		});
		return window;
	}
    
    var showTree = function(app_id, caption) {
		_this.getMTree(app_id, caption);
		
		//添加到容器
		Ext.getCmp(_this.treepanel_id).add(_this.tree);
		//渲染
		_this.panel.doLayout();
    }
    
    this.showAppDistrict = function(renderto_id) {
    	_this.getMTree();
    	
    	if (!renderto_id) {
    		_this.window = _this.createWindow(_this.tree_w+50);
    		_this.window.show();
    		
    		_this.window.add(_this.tree);
    		_this.window.doLayout();
    		_this.window.center();
    		
    		_this.window.on('beforeclose', function(){
    			_this.tree.destroy();
    			_this.window.remove(_this.tree);
    		});
    	}
    	else{
    		_this.tree.render(renderto_id);
    	}
    }
    
	
	//显示等待窗口
	var showWaitWindow = function(msgstr){
		if (_this.waitWindow) {_this.waitWindow.hide(); _this.waitWindow = null;}
		if (!_this.waitWindow) {
			if (typeof(msgstr)=='undefined') msgstr = '请稍候...';
			_this.waitWindow = new Ext.LoadMask(Ext.getBody(), {msg:msgstr, removeMask:true});
		}
		_this.waitWindow.show();
	}
	//关闭等待窗口
	var hideWaitWindow = function(){
		if (_this.waitWindow) _this.waitWindow.hide();
	}
	
	this.add = function(pid, name){
		if (typeof(pid)=='undefined' || pid==null) {
			if (_this.currentTreeNode) var pid = _this.currentTreeNode.id;
			if (pid) {
				if (pid+''=='0') {
					pid = 0;
				}
				else{
					pid = convert_int(tagContent('_', '', pid), 0);
				}
			}
			else{
				pid = 0;
			}
		}
		if (typeof(name)=='undefined' || name==null) {
			name = Ext.getCmp(_this.textfield_id).getValue();
		}
		
		if (name.length==0) {Ext.QuickMsg.msg('您好', '请指定名称'); return false;}
		if (pid>0) {
			if(_this.currentTreeNode.attributes.classtype=='camera'){
				Ext.QuickMsg.msg('您好','不能在摄像头下添加区域！'); return false;
			}
		}
		var paras = "opr=add&pid="+pid+"&name="+name;
		
		//显示等待窗口
		showWaitWindow();
		
		//提交
		Ext.Ajax.request({
	        url: g_root + 'pub_tree.php',
	        params: paras,
	        success: function(response, options){
	        	hideWaitWindow();
	            try{
		        	var rjdata = Ext.util.JSON.decode(response.responseText);
		            if (rjdata.success=='true') {
		            	var node = new Ext.tree.TreeNode({id:'g_'+rjdata.id, text:name, expanded:false, leaf:false, icon:g_root+"plugin/ext/resources/images/default/tree/folder.gif"});
		            	node.attributes.type = 'group';
		            	node.attributes.classtype = 'group';
						if (_this.currentTreeNode) _this.currentTreeNode.appendChild(node);
		            	Ext.QuickMsg.msg('', '已添加');
		            }
		            else{
		            	Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), width:280, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
		            }
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
	        },
	        failure: function(){
	        	hideWaitWindow();
	        	alert('意外错误')
	        }
	    });
	}
	
	this.modify = function(id, name){
		if (typeof(id)=='undefined' || id==null || convert_int(id,0)==0) {
			if (!_this.currentTreeNode)  {Ext.QuickMsg.msg('您好', '请指定一个区域a'); return false;}
			if (_this.currentTreeNode.attributes.classtype!='group') {Ext.QuickMsg.msg('您好', '请指定一个区域b'); return false;}
			id = _this.currentTreeNode.id;
			id = tagContent('_','',id);
		}
		if (name==null || typeof(name)=='undefined') name = Ext.getCmp(_this.textfield_id).getValue();
		if (name.length==0) {Ext.QuickMsg.msg('您好', '请指定名称'); return false;}
		
		var paras = "opr=modify&id="+id+"&name="+name;
		
		showWaitWindow();
		
		//提交
		Ext.Ajax.request({
	        url: g_root + 'pub_tree.php',
	        params: paras,
	        success: function(response, options){
	        	hideWaitWindow();
	            try{
		        	var rjdata = Ext.util.JSON.decode(response.responseText);
		            if (rjdata.success=='true') {
						_this.currentTreeNode.setText(name);
		            	
		            	Ext.QuickMsg.msg('', '已修改');		            	
		            	//Ext.Msg.show({title:'成功', msg:qpDHanzi(rjdata.data), buttons: Ext.Msg.OK, icon:Ext.MessageBox.INFO, fn:function(btn){document.location.reload();}});
		            }
		            else{
		            	Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), width:280, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
		            }
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
	        },
	        failure: function(){
	        	hideWaitWindow();
	        	alert('意外错误');
	        }
	    });
	}
	
	this.del = function(id){
		if (typeof(id)=='undefined' || id==null || convert_int(id,0)==0) {
			if (!_this.currentTreeNode)  {Ext.QuickMsg.msg('您好', '请指定一个区域'); return false;}
			if (_this.currentTreeNode.attributes.classtype!='group') {Ext.QuickMsg.msg('您好', '请指定一个区域'); return false;}
			id = _this.currentTreeNode.id;
			id = tagContent('_','',id);
		}
		
		var paras = "opr=delete&id="+id;
		
		showWaitWindow();
		
		//提交
		Ext.Ajax.request({
	        url: g_root + 'pub_tree.php',
	        params: paras,
	        success: function(response, options){
	        	hideWaitWindow();
	            try{
		        	var rjdata = Ext.util.JSON.decode(response.responseText);
		            if (rjdata.success=='true') {
						if (_this.currentTreeNode) {
							if(rjdata.data){
								for(i=0;i<(rjdata.data).length;i++){
								//alert((rjdata.data)[i].id);
									_this.currentTreeNode.parentNode.appendChild(new Ext.tree.TreeNode({id:(rjdata.data)[i].id,text:(rjdata.data)[i].text,leaf:(rjdata.data)[i].leaf,type:(rjdata.data)[i].type,icon:g_root+"images/camera.gif"}));
								}
							}
							_this.currentTreeNode.parentNode.removeChild(_this.currentTreeNode);
							_this.currentTreeNode = null;
						}
		            	
		            	Ext.QuickMsg.msg('', '已删除');
		            	//Ext.Msg.show({title:'成功', msg:qpDHanzi(rjdata.data), buttons: Ext.Msg.OK, icon:Ext.MessageBox.INFO, fn:function(btn){document.location.reload();}});
		            }
		            else{
		            	Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), width:280, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
		            }
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
	        },
	        failure: function(){
	        	hideWaitWindow();
	        	alert('意外错误')
	        }
	    });
	}
	
	var moveNode = function(tree, node, oldParent, newParent, position) {
		if (newParent.attributes.classtype) {
			if (newParent.attributes.classtype!='group') {
				//复原树
	            tree.suspendEvents();
	            oldParent.appendChild(node);
	            if (oldNextSibling){
	                oldParent.insertBefore(node, oldNextSibling);
	            }
	            
	            tree.resumeEvents();
	            tree.enable();
			}
		}
		var id = tagContent('_','',node.id);
		if(newParent.id+''=='0')
			var t_id = 0;
		else
			var t_id = tagContent('_','',newParent.id);
		var type = node.attributes.classtype;
		var paras = "opr=changeparent&id="+id+"&t_id="+t_id+"&type="+type;
		
		showWaitWindow();
		
		tree.disable();
		//提交
		Ext.Ajax.request({
	        url: g_root + 'pub_tree.php',
	        params: paras,
	        success: function(response, options){
	        	hideWaitWindow();
	            try{
		        	var rjdata = Ext.util.JSON.decode(response.responseText);
		        	
		            if (rjdata.success=='true') {
		            	Ext.QuickMsg.msg('成功', qpDHanzi(rjdata.data));
		            	tree.enable();
		            	//Ext.Msg.show({title:'成功', msg:qpDHanzi(rjdata.data), buttons: Ext.Msg.OK, icon:Ext.MessageBox.INFO, fn:function(btn){document.location.reload();}});
		            }
		            else{
		            	Ext.Msg.show({title:'错误', msg:qpDHanzi(rjdata.data), width:280, buttons: Ext.Msg.OK, icon:Ext.MessageBox.WARNING});
		            	options.failure();
		            }
				}
				catch(e){alert(e.message + "\r\n\r\n" + response.responseText);}
	        },
	        failure: function(response, options){
	        	hideWaitWindow();
	        	//复原树
	            tree.suspendEvents();
	            oldParent.appendChild(node);
	            if (oldNextSibling){
	                oldParent.insertBefore(node, oldNextSibling);
	            }
	            
	            tree.resumeEvents();
	            tree.enable();
	            
	            Ext.QuickMsg.msg('错误', '操作失败');
	        }
	    });
	}
	
	this.showSearch = function(renderto){
		if (_this.c_app) {
			_this.c_app.showSearch(renderto);
		}
	}
}
