Ext.namespace('NGMP.grid');

NGMP.grid.GridPanel = function(A) {
	if (!A.keys) { A.keys = [];}
	if (!A.store) {A.store=A.ds;}
	A.keys.push({
		keys:Ext.EventObject.DELETE,
		fn:function() {
			this.deleteSelected(this.deleteConfig);
		},
		scope:this
	});
	if (A.paging) {
		A.bbar = new Ext.PagingToolbar({
			store: A.store,
			pageSize: 25,
			displayInfo:true,
			displayMsg:LANG_items,
			emptyMsg:LANG_no_items
		});
		if (!A.store.baseParams) {A.store.baseParams = {};}
		A.store.baseParams.limit = 25;
	}
	NGMP.grid.GridPanel.superclass.constructor.call(this,A);
	if (!this.deleteConfig) {
		this.deleteConfig = {};
	}
};
Ext.extend(NGMP.grid.GridPanel, Ext.grid.GridPanel, {
	paging: false,
	deleteSelected:function(B) {
		if (!B) {B=this.deleteConfig;}
		if (!B.deleteParam) {B.deleteParam = 'delete_keys';}
		var C = this.selModel.selections.keys;
		var D = {};
		D[B.deleteParam] = Ext.encode(this.selModel.selections.keys);
		var A = {
			store: this.store,
			params: D,
			count:this.selModel.selections.keys.length
		};
		if (B.callback) {
			A.callback = B.callback;
		}
		if (B.success) {
			A.success = B.success;
		}
		if (B.failure) {
			A.failure = B.failure;
		}
		if (B.scope) {
			A.scope = B.scope;
		}
		NGMP.deleteItems(A);
	}
});

NGMP.grid.LinksPanel=function(A){
	var B;
 	if(!A){
		A={};
	}
	if(!this.link_id){
		this.link_id=0;
	}
	if(!this.link_type){
		this.link_type=0;
	}
	A.store=new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:"links",
			link_id:this.link_id,
			link_type:this.link_type
		},
		root:"results",
		totalProperty:"total",
		id:"link_id",
		fields:["icon","link_type","name","type","url","mtime","id","module"],
		remoteSort:true
	});
 	A.store.setDefaultSort("mtime","desc");
	this.linksDialog = new NGMP.dialog.LinksDialog({
		linksStore:A.store
	});
	
	A.columns=[{
			header:"",
			width:28,
			dataIndex:"icon",
			renderer:this.IconRenderer
   		},{
			header:LANG_name,
			dataIndex:"name",
			css:"white-space:normal;",
			sortable:true
		},{
			header:LANG_type,
			dataIndex:"type",
			sortable:true
		},{
			header:LANG_mtime,
			dataIndex:"mtime",
			sortable:true
		}
	];
	A.tbar=[
		this.linkButton=new Ext.Button({
			id:"link",
			iconCls:"btn-link",
			text:LANG_btn_link,
			cls:"x-btn-text-icon",
			handler:function(){
				this.linksDialog.show();
				},
				scope:this
			}),
			this.unlinkButton = new Ext.Button({
				id:"unlink",
				iconCls:"btn-unlink",
				text:LANG_btn_unlink,
				cls:"x-btn-text-icon",
				handler:function(){
					var F=[];
 					var C=this.getSelectionModel();
					var D=C.getSelections();
					if(D.length>0){
						for(var E=0; E<D.length;E++){
							F.push({link_id:D[E].data.id,link_type:D[E].data.link_type});
						}
						this.store.baseParams.unlinks=Ext.encode(F);
 						this.store.reload();
 						delete this.store.baseParams.unlinks;
					}
				 },
				scope:this}
			)
	];
	A.paging=true;
	A.layout="fit";
	A.view=new Ext.grid.GridView({autoFill:true,forceFit:true});
	A.disabled=true;
	A.loadMask={msg:LANG_loading};
	A.sm=new Ext.grid.RowSelectionModel({});
	A.title=LANG_links;
	NGMP.grid.LinksPanel.superclass.constructor.call(this,A);
 };

Ext.extend(NGMP.grid.LinksPanel,NGMP.grid.GridPanel,{
	afterRender:function(){
		NGMP.grid.LinksPanel.superclass.afterRender.call(this);
		if(this.isVisible()){
			this.onShow();}
		},
	onShow:function(){
		NGMP.grid.LinksPanel.superclass.onShow.call(this);
		if(!this.loaded){
			this.store.load();
			this.loaded=true;
		}
	},
	setWritePermission:function(A){
		this.linkButton.setDisabled(!A);
		this.unlinkButton.setDisabled(!A);
	},
	rowDoulbleClicked:function(D,C,E){
		var A=this.getSelectionModel();
		var B=A.getSelected();
		if(NGMP.linkHandlers[B.data.link_type]){
			NGMP.linkHandlers[B.data.link_type].call(this,B.data.id);
		} else {
			Ext.Msg.alert(LANG_error,LANG_no_handler_definded_for_link_type + B.data.link_type);
		}
	},
	IconRenderer:function(C,A,B){
		return'<div class="ngmp-link-icon-'+B.data.link_type+' "></div>';
	},
	loadLinks:function(A,B){
		if(A>0){
			this.setDisabled(false);
		} else {
			this.setDisabled(true);
		}
		if (this.link_id!=A){
			 this.link_type=B;
			 this.link_id=A;
			 this.store.baseParams.link_id=A;
			 this.store.baseParams.link_type=B;
			 this.linksDialog.setSingleLink(this.link_id,this.link_type);
			 this.loaded=false;
		}
	},
	afterRender:function(){
		NGMP.grid.LinksPanel.superclass.afterRender.call(this);
		this.addListener("rowdblclick",this.rowDoulbleClicked,this);
	}
});

NGMP.panel.IFrameComponent=Ext.extend(Ext.BoxComponent,{
	onRender:function(B,A){if(!this.name){this.name="";}
		this.el=B.createChild({
			tag:"iframe",
			id:this.id,
			frameBorder:0,
			src:this.url,
			name:this.name
		});
	}
});

NGMP.grid.SimpleSelectList=function(B){
	if(!B){B={}; }
	var A;
	if(!B.tpl){
		A="";
 		if(B.title){
			A+="<b>"+B.title+":</b>";
		}
 		A+='<tpl for="."><div id="{dom_id}" class="ngmp-item-wrap">{name}</div></tpl>';
 		B.tpl=new Ext.XTemplate(A);
	}
 	else {
		A=B.tpl;
	}
	B.singleSelect=true;
 	B.autoHeight=true;
 	B.overClass="ngmp-view-over";
 	B.itemSelector="div.ngmp-item-wrap";
 	B.selectedClass="ngmp-view-selected";
 	NGMP.grid.SimpleSelectList.superclass.constructor.call(this,B);
};
Ext.extend(NGMP.grid.SimpleSelectList,Ext.DataView,{
	onRender:function(B,A){
		this.el=B.createChild({tag:"div",cls:"ngmp-select-list"});
 		NGMP.grid.SimpleSelectList.superclass.onRender.apply(this,arguments);
	}
});

NGMP.grid.CheckColumn=function(A){
	Ext.apply(this,A);
 	if(!this.id){
		this.id=Ext.id();
	}
	this.renderer=this.renderer.createDelegate(this);
 	this.addEvents({change:true} );
 	NGMP.grid.CheckColumn.superclass.constructor.call(this);
};
Ext.extend(NGMP.grid.CheckColumn,Ext.util.Observable,{
	init:function(A){
		this.grid=A;
 		this.grid.on("render",function(){
			var B=this.grid.getView();
 			B.mainBody.on("mousedown",this.onMouseDown,this);
		},this);
	},
	onMouseDown:function(E,C){
		if(C.className&&C.className.indexOf("x-grid3-cc-"+this.id)!=-1){
			E.stopEvent();
 			var B=this.grid.getView().findRowIndex(C);
 			var A=this.grid.store.getAt(B);
 			if(!A.data.disabled){
				var D=A.data[this.dataIndex]=="1"?"0":"1";
 				A.set(this.dataIndex,D);
 				this.fireEvent("change",A,D);
			}
		}
	},
	renderer:function(C,D,B){
		D.css+=" x-grid3-check-col-td";
		var A;
 		if(C===true){
			if(B.data.disabled){
				A="-on x-item-disabled";
			}else{
				A="-on";
			}
		} else {
			if(B.data.disabled){
				A=" x-item-disabled";
			} else {
				A="";
			}
		}
		return'<div class="x-grid3-check-col'+A+" x-grid3-cc-"+this.id+'">&#160;</div>';
	}
});

NGMP.grid.PermissionsPanel = function(A) {
	this.config = A || {};
	this.config.title = (A.type == 'READ') ? LANG_read_permissions : LANG_write_permissions;
	this.config.header = false;
	this.config.layout = 'anchor';
	this.config.border = false;
	this.config.anchor = '100% 100%';
	this.config.name = (A.type == 'READ') ? 'read_permissions' : 'write_permission';
	this.p_type = (A.type=='READ') ? 'read':'write';

	this.addEvents({
		enable:true
	});
	this.aclGroupStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		totalProperty:'total',
		root:'results',
		id:'gid',
		baseParams:{
			func:'authGetAuthorizedGroups',
			type: this.p_type
		},
		fields:[
			{name:'name', type:'string'},
			{name:'gid', type:'int'}
		]
	});

	this.aclUserStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		totalProperty:'total',
		root:'results',
		id:'uid',
		baseParams:{
			func:'authGetAuthorizedUsers',
			type:this.p_type
		},
		fields:[
			{name:'fullname', type:'string'},
			{name:'uid', type:'string'}
		]
	});

	this.aclGroupGrid = new NGMP.grid.GridPanel({
		anchor:'100% 50%',
		title: LANG_authorized_groups,
		store: this.aclGroupStore,
		cm: new Ext.grid.ColumnModel([
			{header:LANG_group_name, dataIndex:'name', sortable: true}
		]),
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit:true
		}),
		loadMask: {
			msg: LANG_loading_authorized_groups
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		layout:'fit',
		tbar:[{
			iconCls:'icon-add',
			text:LANG_add,
			cls:'x-btn-text-icon',
			handler: function() {
				this.showAddGroupsDialog();
			},
			scope: this
		},{
			iconCls:'icon-delete',
			text:LANG_delete,
			cls:'x-btn-text-icon',
			handler: function() {
				this.aclGroupGrid.deleteSelected();
			},
			scope: this
		}]
	});
	this.aclUserGrid = new NGMP.grid.GridPanel({
		anchor:'100% 50%',
		title: LANG_authorized_users,
		store: this.aclUserStore,
		cm: new Ext.grid.ColumnModel([
			{header:LANG_name, dataIndex:'fullname', sortable: true}
		]),
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit: true
		}),
		loadMask: {
			msg: LANG_loading_authorized_users
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
		layout:'fit',
		tbar:[{
			iconCls:'icon-add-user',
			text:LANG_add,
			cls:'x-btn-text-icon',
			handler: function() {
				this.showAddUsersDialog();
			},
			scope: this
		},{
			iconCls:'icon-delete-user',
			text:LANG_delete,
			cls:'x-btn-text-icon',
			handler: function() {
				this.aclUserGrid.deleteSelected();
			},
			scope: this
		}]
	});
	this.config.items = [this.aclGroupGrid, this.aclUserGrid];
	NGMP.grid.PermissionsPanel.superclass.constructor.call(this, this.config);
};
Ext.extend(NGMP.grid.PermissionsPanel, Ext.Panel, {
	setAcl:function(A) {
		if (!A)
			A = 0;
		this.acl_id = A;
		this.aclGroupStore.baseParams.acl_id = A;
		this.aclUserStore.baseParams.acl_id = A;
		this.setDisabled(A===0);
		if (this.isVisible()) {
			this.aclGroupStore.load();
			this.aclUserStore.load();
			this.loaded=true;
		}
	},
	onShow: function() {
		if (this.acl_id > 0) {
			this.aclGroupStore.load();
			this.aclUserStore.load();
			this.loaded = true;
		}
		NGMP.grid.PermissionsPanel.superclass.onShow.call(this);
	},
	afterRender:function() {
		NGMP.grid.PermissionsPanel.superclass.afterRender.call(this);
		if (this.isVisible() && !this.loaded && this.acl_id >0) {
			this.aclGroupStore.load();
			this.aclUserStore.load();
			this.loaded = true;
		}
	},
	showAddGroupsDialog:function() {
		if (!this.addGroupsDialog) {
			this.addGroupsDialog = new NGMP.dialog.SelectGroups({
				handler: function(A) {
					if (A.selModel.selections.keys.length>0) {
						this.aclGroupStore.baseParams.add_groups=Ext.encode(A.selModel.selections.keys);
						this.aclGroupStore.load();
						delete this.aclGroupStore.baseParams.add_groups;
					}
				},
				scope:this
			});
		}
		this.addGroupsDialog.show();
	},
	showAddUsersDialog:function() {
		if (!this.addUsersDialog) {
			this.addUsersDialog = new NGMP.dialog.SelectUsers({
				handler:function(A) {
					if (A.selModel.selections.keys.length>0) {
						this.aclUserStore.baseParams.add_users = Ext.encode(A.selModel.selections.keys);
						this.aclUserStore.load();
						delete this.aclUserStore.baseParams.add_users;
					}
				},
				scope:this
			});
		}
		this.addUsersDialog.show();
	}
});
