GO.filesearch.EditPanel = function (config) {

	if(!config)
		config={};

	config.title = GO.filesearch.lang.edit;
	config.width = 450;
	config.waitMsgTarget=true;
	config.autoScroll=true;
	config.bodyStyle='padding:5px;';
	config.items= [
		{
			xtype:'fieldset',
			anchor:'100%',
			title:GO.lang['strProperties'],
			items:[
				{
					xtype: 'textfield',
					fieldLabel: GO.lang['strName'],
					name: 'name',
					anchor: '100%'
//					validator:function(v){
//						return !v.match(/[\/:\*\"<>|\\]/);
//					}
				}, new Ext.form.TextArea({
					name: 'comment',
					fieldLabel: '',
					hideLabel: true,
					anchor:'100% 100%'
				})
			],
			autoHeight:true
		}
	];
	
	
	this.saveButton = new Ext.Button({
		text:GO.filesearch.lang.save,
		disabled:true,
		handler: function(){
			this.save();
		},
		scope: this
	});
	
	this.saveNextButton = new Ext.Button({
		text:GO.filesearch.lang.saveNext,
		disabled:true,
		handler: function(){
			this.save(true);
		},
		scope: this
	});
	
	config.buttons = [
		this.saveButton,
		this.saveNextButton
//	{
//		text:GO.filesearch.lang.save,
//		handler: function(){
//			this.save();
//		},
//		scope: this
//	},{
//		text:GO.filesearch.lang.saveNext,
//		handler: function(){
//			this.save(true);
//		},
//		scope: this
//	}
	]

	GO.filesearch.EditPanel.superclass.constructor.call(this,config);

	this.addEvents({
		'save':true
	});
}


Ext.extend(GO.filesearch.EditPanel, Ext.form.FormPanel, {
	load : function(file_id,config)
	{
		config = config || {};

		this.setFileID(file_id);

		this.saveButton.setDisabled(false);
		this.saveNextButton.setDisabled(false);

		if(!this.rendered)
			this.render(Ext.getBody());

		this.form.reset();

		var params = {
			id: file_id
		};

		if(config.loadParams)
		{
			Ext.apply(params, config.loadParams);
		}

		this.form.load({
			url: GO.url("files/file/load"),
			params: params,
			success: function(form, action) {
				this.setWritePermission(action.result.data.write_permission);

				if(action.result.data.id)
				{
					this.setFileID(action.result.data.id);
				}

				this.folder_id=action.result.data.folder_id;

				GO.filesearch.EditPanel.superclass.show.call(this);
			},
			failure: function(form, action) {
				Ext.MessageBox.alert(GO.lang['strError'], action.result.feedback);
			},
			scope: this
		});
	},

	setWritePermission : function(writePermission)
	{
		this.form.findField('name').setDisabled(!writePermission);
	},

	setFileID : function(file_id)
	{
		this.file_id = file_id;
	},
	
	save : function(next)
	{
		this.form.submit({

			url:GO.url("files/file/submit"),
			params: {
				id: this.file_id				
			},
			waitMsg:GO.lang['waitMsgSave'],
			
			success:function(form, action){
				this.fireEvent('save', this, this.file_id, this.folder_id);
				if(next)
					this.next();
			},
			failure: function(form, action) {
				var error = '';
				if(action.failureType=='client')
				{
					error = GO.lang['strErrorsInForm'];
				}else
				{task: 'file_properties'
					error = action.result.feedback;
				}

				Ext.MessageBox.alert(GO.lang['strError'], error);
			},
			scope:this
		});
	},
	next : function() {
		var sm = this.resultsGrid.getSelectionModel();
		sm.selectNext(false);
		this.load(sm.getSelected().id);
		if(this.filePanel)
			this.filePanel.load(sm.getSelected().id);
	},
	reset : function() {
		this.form.reset();
		this.saveButton.setDisabled(true);
		this.saveNextButton.setDisabled(true);
	}
	
});