{
	xtype: 'panel',
	require: [
		'Ext.ux.navigation.controller.UserRole',
		'Ext.ux.navigation.view.UserRolePanel'
	],
	border: false,
	autoScroll: true,
	bodyStyle: 'background-image: none;',

    layout: {
        type: 'hbox',
        align: 'stretch'
    },

	border: false,
	header: false,

	onInitialize: function(widget, owner) {

		var i_record = owner.dataParent.getRecord();

		var s_msg1 = i_record.get("ROLDSC") +  ip2.i18n({
			da: " kan tildele brugere følgende roller",
			en: " can assign users the following roles"
		});
		var i_roleAllowed = Ext.widget('userrolepanel', {
			title: s_msg1,
			flex: 1,
			record: i_record,
			entity: widget.entity,
			itemId: "roleAllowed",
			split: true
		});

		var s_msg2 = ip2.i18n({
			da: "Følgende roller kan tildele brugere rollen som ",
			en: "The following roles can assign users the role as "
		}) + i_record.get("ROLDSC");

		var i_roleUsable = Ext.widget('userrolepanel', {
			title: s_msg2,
			flex: 1,
			itemId: "roleUsable",
			record: i_record,
			entity: widget.entity,
			split: true
		});

		var i_storeAllowed = i_roleAllowed.store;
		var i_storeUsable = i_roleUsable.store;

		i_storeAllowed.on({
			beforeload: {
				fn: function(store) {

					var i_proxy = store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();
					Ext.apply(o_extraParams, {
						token: i_record.get(i_record.idProperty),
						action: "ip2navProxy.listRole2RoleRelation",
						entity: "NAVRLRRL00",
						direction: "from"
					});
				}
			}
		});

		i_storeUsable.on({
			beforeload: {
				fn: function(store) {

					var i_proxy = store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();
					Ext.apply(o_extraParams, {
						token: i_record.get(i_record.idProperty),
						action: "ip2navProxy.listRole2RoleRelation",
						entity: "NAVRLRRL00",
						direction: "to"
					});
				}
			}
		})

		widget.roleAllowed = i_roleAllowed;
		widget.roleUsable = i_roleUsable;

		widget.add(this.roleAllowed);
		widget.add(this.roleUsable);
	},

	doDelete: function(f_callback) {
		this.rolePermissionTree.getController().doDelete();

		f_callback && f_callback();
	},

	doSave: function(f_callback) {

		var n_ticker = 2;

		var f_done = function() {
			n_ticker--;
			if (n_ticker == 0) {
				f_callback && f_callback();
			}
		};

		var i_self = this;
		var i_roleAllowed = i_self.roleAllowed;
		var i_roleUsable = i_self.roleUsable;
		var i_record = i_self.ownerCt.dataParent.getRecord();

		var a_from = i_roleAllowed.getController().getRoles();
		var a_to = i_roleUsable.getController().getRoles();

		i_self._saveAllowed(i_record, a_from, function() {
			f_done();
		});

		i_self._saveUsable(i_record, a_to, function() {
			f_done();
		});
	},

	_saveAllowed: function(i_record, a_result, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: 'POST',
			jsonData: {
				token: i_record.get(i_record.idProperty),
				action: "ip2navProxy.updateRole2RoleRelation",
				entity: "NAVRLRRL00",
				direction: "from",
				rows: a_result
			},
			success: function() {
				if (f_callback) {
					f_callback();
				}
			},
			faliure: function(err) {
				if (f_callback) {
					f_callback();
				}
			}
		});

	},

	_saveUsable: function(i_record, a_result, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: 'POST',
			jsonData: {
				token: i_record.get(i_record.idProperty),
				action: "ip2navProxy.updateRole2RoleRelation",
				entity: "NAVRLRRL00",
				direction: "to",
				rows: a_result
			},
			success: function() {
				if (f_callback) {
					f_callback();
				}
			},
			faliure: function(err) {
				if (f_callback) {
					f_callback();
				}
			}
		});
	},

	isDirty: function() {

		var i_self = this;
		var b_dirty = false;
		var i_roleAllowed = i_self.roleAllowed;
		var i_roleUsable = i_self.roleUsable;
		b_dirty = i_roleAllowed.isDirty();
		if (!b_dirty) {
			b_dirty = i_roleUsable.isDirty();
		}
		return b_dirty;
	}
}
