Xsn.GroupMutationDialog = function(){
	this.init();
}

Ext.extend(Xsn.GroupMutationDialog, Ext.util.Observable, {

	show : function(node){
	 	this.node = node;
		this.dlg.getLayout().getRegion('north').getActivePanel().getEl().child('div').dom.innerHTML = 'Naar welke groep wilt u de personen uit \'' + node.text + '\' kopieeren ?';	 
		this.tree.getSelectionModel().clearSelections();
		this.tree.root.reload();
	 	this.dlg.show();	
	},

	hide : function(){
		this.dlg.hide();
	},
	
	init: function(){
		var dlg = new Ext.LayoutDialog(Ext.id(), {
		        modal:true,
		        autoCreate: true,
				title: 'Groepsmutatie',
		        width:525,
		        height:460,
		        shadow:true,
                closable:false,
                draggable:false,
                collapsible:false,
                animateTarget:null,
                resizable:false,				                
		        north: {
					titlebar:false,
					initialSize:33	
				},
		        center: {
		            titlebar: false,
		            autoScroll:true
		        }
		});

		dlg.addKeyListener(27, this.hide, this);
		dlg.addButton('Ok', function(){
		 	var node = tree.getSelectionModel().getSelectedNode();
			if (node){
				this.onOk(node);				
			} else {
				this.hide();
			}
		}, this);		
		dlg.addButton('Annuleer', this.hide, this);
		
		var label = dlg.getLayout().getEl().createChild();		
		label.createChild({style:'padding:5px;background-color:#d6dff7;margin:1px 1px 0 1px;border-bottom:1px solid #adbed6'});
		var panel = dlg.getLayout().getEl().createChild({tag:'div', id:'group-mutation-tree'});
		var tree = new Ext.tree.TreePanel(panel, {
		    animate:true, 
		    loader: new Ext.tree.TreeLoader({dataUrl:'index.php?action=getGroupsNodes&hideEmptyGroup=1'}),
		    enableDD:false,
		    rootVisible:false,                
		    containerScroll: true
		});
		
		var root = new Ext.tree.AsyncTreeNode({
		    text: 'Alle groepen', 
		    draggable:false, 
		    cls: 'folder',
		    id:'grp-0',
		    expanded: true
		});
		tree.setRootNode(root);
		tree.render();

		tree.on('dblclick', this.onOk, this);

		dlg.getLayout().add('north', new Ext.ContentPanel(label, {fitToFrame: true}));
		dlg.getLayout().add('center', new Ext.ContentPanel(panel, {tree:tree, fitToFrame: true}));
		dlg.getLayout().getRegion('center').getEl().setStyle('border-top', '0px');
		dlg.getLayout().getRegion('north').getEl().setStyle('border-bottom', '0px');

		this.tree = tree;		
		this.dlg = dlg;
	},
	
	onOk : function(node){
	 	Ext.Ajax.request({url:'index.php?action=mutateGroup&src=' + this.node.id + '&target=' + node.id, scope:this, success:function(r,o){
			Ext.MessageBox.alert('Groepsmutatie', 'De contactpersonen zijn succesvol gekopieerd.');
			this.hide();			
		}});
	}
});
