var EditLineMainEx = function(config) {
	this.config = config;
	
	this.targetEx = null;
}

EditLineMainEx.prototype = {
	_init: function() {
		this.htmleditor = new Ext.form.TextArea({
			id: 'cube_editor_text_area',
			xtype: 'textarea',
			name: 'warning',
			fieldLabel: 'Текст, который будет показан пользователям при обращении к закрытому курсу ДО введения пароля (только для закрытого курса, не более 1500 символов)',
			height: 210,
			anchor: '98%'
		});

		this.fckeditor = null;

		this.fs = new Ext.FormPanel({
			frame: true,
			region: 'center',
			labelAlign: 'top',
			labelWidth: 85,
			width: 340,
			waitMsgTarget: true,

			// configure how to read the XML Data
			reader: new Ext.data.XmlReader({
				record: 'cour',
				success: '@success'
			}, [
			'title', 'warning', 'pass', 'lock'
			]),

			// reusable error reader class defined at the end of this file
			errorReader: new Ext.form.XmlErrorReader(),

			defaultType: 'textfield',

			items: [{
				fieldLabel: 'Название курса',
				name: 'title',
				width: '100%'
			},{
				fieldLabel: 'Закрыть курс паролем',
				name: 'lock',
				width: '100%',
				xtype: 'checkbox'
			},{
				fieldLabel: 'Пароль (только для закрытого курса)',
				name: 'pass',
				width: '100%'
			},
			this.htmleditor],
			
			clear: function(){
				
			}
		});

		// explicit add
		var submit = this.fs.addButton({
			text: 'Save',
			disabled: true,
			handler: function() {
				var target_id = this.target_id;
				var params = {id: target_id};
 
				// FCKEditor
				document.getElementById('cube_editor_text_area').value = FCKeditorAPI.GetInstance('cube_editor_text_area').GetXHTML();
				this.fs.getForm().submit({
					params: params,
					method: 'POST',
					url: 'actions/panelmain.uno.save.xml.php',
					waitMsg: 'Сохранение...',
					failure: function(form, action) {
						// пока так, хотя сюда же попадает Auth Problem
						//msg_error('System error is occured while saving this course (id = ' + this.target_id + '). Try to reload the page');
					},
					success: function(form, action) {
						this.win.hide(this.targetEl, function() {
							app.cour_main.reload();
						}, this);
					},
					scope: this
				});
			},
			scope: this
		});

		// simple button add
		this.fs.addButton('Cancel', function(){
			this.win.hide(this.targetEl);
		}, this);

		this.fs.on({
			actioncomplete: {
				fn: function(form, action) {
					if (action.type == 'load') {
						// FCKEditor
						var xml = this.fs.reader.xmlData;
						var root = xml.documentElement || xml;
						var html_short = Ext.DomQuery.selectValue('warning', root);
						
						submit.enable();
						
						if (typeof(FCKeditorAPI) != 'undefined') {
							FCKeditorAPI.GetInstance('cube_editor_text_area').SetData(document.getElementById('cube_editor_text_area').value);
						}
					}
				},
				scope: this
			}
		});

		var cfg = {
			title: 'Edit cube',
			layout: 'border',
			minWidth: 500,
			minHeight: 300,
			modal: true,
			closeAction: 'hide',
			border: false,
			items: this.fs,
			keys: {
				key: 27, // Esc key
				handler: function(){ this.win.hide(this.targetEl); },
				scope: this
			},
			listeners: {
				show: function() {
					$('.post embed').css('visibility', 'hidden');
				},
				hide: function() {
					$('.post embed').css('visibility', 'visible');
				}
			}
		};
		Ext.apply(cfg, this.config);
		
		this.win = new Ext.Window(cfg);
	},

	_show_window: function() {        
		this.win.setTitle(id == 0 ? 'Добавить курс' : 'Редактировать курс');
		if (this.target_id == 0) {
			//Ext.Msg.alert('Warning', 'Выберите курс, пожалуйста (id = 0)');
			//return;
		}
		
		this.win.show(this.targetEl);
		/* Firefox fix - BEGIN */
		$('.x-toolbar.x-small-editor').css('width', 'auto');
		$('.x-toolbar.x-small-editor').css('height', 'auto');
		$('#x-form-el-html_short iframe').css('height', '220');
		/* Firefox fix - END */
		/* Tree toolbar compatibility fix - BEGIN */
		$('.x-form-element .x-form-text').css('width', '100%');
		$('.x-form-element .x-form-text').css('height', '22px');
		/* Tree toolbar compatibility fix - END */
		this.animateTarget = this.targetEl;
		//if (this.target_id != 0) {
			this.fs.getForm().load({
				method: 'GET',
				url: 'actions/panelmain.uno.get.xml.php',
				waitMsg: 'Загрузка',
				params: {
					id: this.target_id
				},
				failure: function(form, action) {
					msg_error('Requested object (id = ' + this.target_id + ') does not exist on the server');
					this.win.hide(this.targetEl);
				},
				scope: this
			});
		//}
	},
	
	add: function(trg){
		this.targetEx = trg;
		this.show(0);
	},
	
	edit: function(trg, id){
		this.targetEx = trg;
		this.show(id);
	},

	show : function(id) {
		this.target_id = id;

		if (!this.win) {
			this._init();
			var th = this;
			
			$.getScript("fckeditor/fckeditor.js", function(data, textStatus) {
				th._show_window();

				th.fckeditor = new FCKeditor('cube_editor_text_area');
				th.fckeditor.BasePath = 'fckeditor/';
				th.fckeditor.ToolbarSet = 'IntCMF';
				th.fckeditor.Height = '240';
				th.fckeditor.ReplaceTextarea();
			});
		} else {
			this._show_window();
		}
	}
};

// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
	Ext.form.XmlErrorReader.superclass.constructor.call(this, {
		record : 'field',
		success: '@success'
	}, [
	'id', 'msg'
	]
	);
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);
