Uwd.System.ProgramTree = function(c){
	if (typeof c === 'object') {
		if (!c._checkedIds) {
			this.checkedIds = [];	
		} else {
			this.checkedIds = c._checkedIds;	
		}
	} else {
		this.checkedIds = [];
	}
	
	var config = {
		id : c.id,
		baseCls: 'x-plain',
		animate : true,
		useArrows : true,
		border : c.border,
		userManagement: c.userManagement || false,
		autoHeight : true,
		rootVisible : false,
		autoScroll : false,
		bodyStyle: c.bodyStyle || '',
		borderWidth : Ext.isBorderBox ? 0 : 2,
		_selModel: c._selModel || false,
		objIndex: c.objIndex,
		objId: c.objId,
		root : new Ext.tree.TreeNode({
			text:'programs'
		})
	};
	Uwd.System.ProgramTree.superclass.constructor.call(this, config);
	
	this.on({
		render : {scope: this, fn: this.cleanCss}
	});
	
	
	var programGroups = [];
	// Die erstellten Programm Menu Objekte in einem Array
	var programNodes = [];
	
	
	Ext.each(Uwd.System.ProgramStack.getStack(), function(module){
		// Verzeichnis der Programmgrupe hinzufügen falls dieses noch nicht existiert
		var programGroup = module.getProgramFolder();
		if (Ext.isEmpty(programGroup)) {
			programGroup = 'undefined';
		}
		if (!programGroups.contains(programGroup)) {
			programGroups.push(programGroup);
			programNodes[programGroup] = new Ext.tree.TreeNode({
				text : programGroup,
				iconCls: 'startmenu-folder-icon',
				expanded : true,
				attributes : {
					isFolder : true,
					dbId : false	
				}
			});
		}
		var node = programNodes[programGroup];
		
		node.appendChild(new Ext.tree.TreeNode({
			text : module.getName(),
			checked : this.checkedIds.contains(module.getId()),
			iconCls: module.getIconClass(),
			isFolder : false,
			dbId : module.getId()	
		}));
		
		this.root.appendChild(node);
	}.createDelegate(this));
	
	
	this.on('checkchange', function(node, checked) {
		if (!this.userManagement || !this.userManagement.userPanel) {
			return;	
		}
		
		this.userManagement.userPanel.sm.selectRow(this.userIndex);
		
		if (checked) {
			this.addRelation(this.objId, node.attributes.dbId, node);
		} else {
			this.removeRelation(this.objId, node.attributes.dbId, node);
		}
	}.createDelegate(this));
	
	// disable selecting
	if (this._selModel && this._selModel.selectRow) {
		this.getSelectionModel().select = function(node){
			this._selModel.selectRow(this.objIndex);
			if (this._selectNode === true) {
				var last = this.selNode;
				if(last != node && this.fireEvent('beforeselect', this, node, last) !== false){
					if(last){
						last.ui.onSelectedChange(false);
					}
					this.selNode = node;
					node.ui.onSelectedChange(true);
					this.fireEvent("selectionchange", this, node, last);
				}
			}
			//alert(this.userIndex);
			return node;
		}.createDelegate(this);
    }
	
};

Ext.extend(Uwd.System.ProgramTree, Ext.tree.TreePanel, {
		   
	cleanCss : function() {
		this.el.removeClass('x-tree');
	},
	
	getToRemove : function() {
		var toRemove = [];
		// get to remove...
		Ext.each(this.checkedIds, function(id) {
			Ext.each(this.root.childNodes, function(node) {
				Ext.each(node.childNodes, function(n) {
					var ui = n.getUI();
					if (n.attributes.dbId === id && ui.isChecked() !== true) {
						toRemove.push(id);
					}
				}.createDelegate(this));
			}.createDelegate(this));
		}.createDelegate(this));
		return toRemove;
	},

	getToAdd : function() {
		var toAdd = [];
		// get to remove...
		Ext.each(this.root.childNodes, function(node) {
			Ext.each(node.childNodes, function(n) {
				var ui = n.getUI();
				if (ui.isChecked() === true && !this.checkedIds.contains(n.attributes.dbId)) {
					toAdd.push(n.attributes.dbId);
				}
			}.createDelegate(this));
		}.createDelegate(this));
		return toAdd;
	},
	
	hasChanged : function() {
		if (!this.rendered) {
			return false;	
		}
		var r = this.getToRemove();
		var a = this.getToAdd();
		return r.length + a.length > 0;
	},
	
	clear : function() {
		this.checkedIds = this.getCurrent();
		return true;
	},
	
	getCurrent : function() {
		var ret = null;
		ret = [];
		Ext.each(this.root.childNodes, function(node) {
			Ext.each(node.childNodes, function(n) {
				var ui = n.getUI();
				if (ui.isChecked() === true) {
					ret.push(n.attributes.dbId);
				}
			}.createDelegate(this));
		}.createDelegate(this));
		return ret;
	},
	
	removeRelation : function(id, pId, node) {
		if (id === 0 || this.isError === true) {
			if (this.isError === true) {
				this.isError = false;	
			}
			return;	
		}
		
		// do the request
		Ext.Ajax.request({
			url: window.system.config.getBackendUrl(),
			method:'post',
			params: {
				controller: 'UserManagementGroup',
				basePath: this.userManagement.program.getBackendPath(),
				action: 'removeProgram',
				id: id,
				programId: pId
			},
			scope: this,
			success: function (result, request) { 
				var jsonData = Ext.decode(result.responseText);
				
				if (jsonData.success == false) {
					this.userManagement.win.createWindow({
						title: Uwd.System.SystemLanguage.error,
						iconCls: 'icon-error',
						layout: 'fit',
						width: 200,
						height: 120,
						border: false,
						fixToParent: true,
						resizable: false,
						constrainHeader: true,
						modal: true,
						html: '<div class="x-window-dlg"><div class="ext-mb-icon ext-mb-warning"></div></div> ' + /*this.removeErrorString*/'ging nicht'
					});
					this.isError = true;
					node.ui.toggleCheck();			
				}
				//alert(result.responseText);
				// set loading on false
			}.createDelegate(this),
			failure: function (result, request) { 
				this.userManagement.win.createWindow({
					title: Uwd.System.SystemLanguage.error,
					iconCls: 'icon-error',
					layout: 'fit',
					width: 200,
					height: 120,
					border: false,
					fixToParent: true,
					resizable: false,
					constrainHeader: true,
					modal: true,
					html: '<div class="x-window-dlg"><div class="ext-mb-icon ext-mb-warning"></div></div> ' + /*this.removeErrorString*/'ging nicht'
				});
				this.isError = true;
				node.ui.toggleCheck();			
			}.createDelegate(this) 
		});
	},

	addRelation : function(id, pId, node) {
		if (id === 0 || this.isError === true) {
			if (this.isError === true) {
				this.isError = false;	
			}
			return;	
		}
		// do the request
		Ext.Ajax.request({
			url: window.system.config.getBackendUrl(),
			method:'post',
			params: {
				controller: 'UserManagementGroup',
				basePath: this.userManagement.program.getBackendPath(),
				action: 'addProgram',
				id: id,
				programId: pId
			},
			scope: this,
			success: function (result, request) { 
				var jsonData = Ext.decode(result.responseText);
				
				if (jsonData.success == false) {
					this.userManagement.win.createWindow({
						title: Uwd.System.SystemLanguage.error,
						iconCls: 'icon-error',
						layout: 'fit',
						width: 200,
						height: 120,
						border: false,
						fixToParent: true,
						resizable: false,
						constrainHeader: true,
						modal: true,
						html: '<div class="x-window-dlg"><div class="ext-mb-icon ext-mb-warning"></div></div> ' + this.addErrorString
					});
					this.isError = true;
					node.ui.toggleCheck();			
				}
				//alert(result.responseText);
				// set loading on false
			}.createDelegate(this),
			failure: function (result, request) { 
				this.userManagement.win.createWindow({
					title: Uwd.System.SystemLanguage.error,
					iconCls: 'icon-error',
					layout: 'fit',
					width: 200,
					height: 120,
					border: false,
					fixToParent: true,
					resizable: false,
					constrainHeader: true,
					modal: true,
					html: '<div class="x-window-dlg"><div class="ext-mb-icon ext-mb-warning"></div></div> ' + this.addErrorString
				});
				this.isError = true;
				node.ui.toggleCheck();			
			}.createDelegate(this) 
		});
	}

});
