
function Dictionary(config) {
	this.win = null;
	this.title = config.title;
	this.container = config.id;
	this.dict = config.dict;

	if ( this.dict == 'synonyms' || this.dict == 'phrases' ) {
		this.width = 600;
		this.lwidth = 250;
		this.twidth = 300;
		this.height = 300;
	} else {
		this.width = 400;
		this.twidth = 220;
		this.lwidth = 130;
		this.height = 250;
	}

	this.infoPanel = new Ext.Panel({
		region: 'center',
// 		xtype: 'panel',
// 		border: false,
		bodyStyle: 'padding:6px 9px;',
		layout: 'column'
	});

	this.hTriggerClick = function() {
		Ext.Ajax.request({
			scope: this,
			url: 'dict/php/dictionary.php',
			success: function(r) {
				var json = eval(r.responseText);
				if ( json.stat == '1' ) {
					this.infoPanel.body.update(json.definition);
				} else {
					this.infoPanel.body.update('Думата я няма в речника.');
				}
				this.treePanel.getLoader().dataUrl = 'dict/php/words.php?word=' + this.trigger.getValue() + '&dict=' + this.dict;
				this.treePanel.getRootNode().reload();
			},
			method: 'GET',
			params: {
				word: this.trigger.getValue(),
				dict: this.dict
			}
		});
	}

	this.trigger = new Ext.form.TriggerField({
		width: this.twidth,
		name: 'word',
		'fieldLabel': 'Търсене',
		'triggerClass': 'x-form-search-trigger',
 		'onTriggerClick': this.hTriggerClick.createDelegate(this)
	});

	this.trigger.on('specialkey', function(f, e) {
		if(e.getKey() == e.ENTER) {
			this.trigger.onTriggerClick();
		}
	}, this);

	this.treePanel = new Ext.tree.TreePanel({
		region: 'west',
		width: this.lwidth,
		split: true,
		rootVisible: false,
		singleExpand: true,
		lines: false,
		expanded: true,
		autoScroll: true,
		iconCls: '',
		dataUrl: 'dict/php/words.php?dict=' + this.dict,
		root: {
			nodeType: 'async',
			text: 'Ext JS',
			draggable: false,
			id: 'source'
		}
	});

	this.treePanel.on('dblclick', function(node, e) {
		this.trigger.setValue(node.attributes.text);
		this.trigger.onTriggerClick();
	}, this);

	this.treePanel.getRootNode().on('load', function(node) {
		if ( node.item(0) )
			this.treePanel.selectPath(node.item(0).getPath());
	}, this);

	this.handler = function() {
// 		create the window on the first click and reuse on subsequent clicks
		if ( !this.win ) {
			this.win = new Ext.Window({
				title: this.title,
				collapsible: true,
				constrainHeader: true,
				y: 300,
				applyTo: this.container,
				layout: 'border',
				width: this.width,
				height: 250,
				closeAction: 'hide',
				plain: true,
// 				bbar: [],
				tbar: [
					' ', ' Дума: ',  ' ',
					this.trigger
				],

				items: [
					this.treePanel,
					this.infoPanel
				]
			});
		}

// 		this.treePanel.getRootNode().expand();

		var nav = new Ext.KeyNav(this.treePanel.getEl(), {
			"enter": function(e) {
				var selectedNode = this.treePanel.getSelectionModel().getSelectedNode();
				this.trigger.setValue(selectedNode.attributes.text);
				this.trigger.onTriggerClick();
			}
		});

		this.win.show(this);
	};
};

Ext.onReady(function(){

	var bulgarian_slovak = new Dictionary({
		title: 'Българо-словашки речник',
		id: 'bulgarian-slovak-container',
		dict: 'bulgarian_slovak'
	});

	var slovak_bulgarian = new Dictionary({
		title: 'Словашко-български речник',
		id: 'slovak-bulgarian-container',
		dict: 'slovak_bulgarian'
	});

	var synonyms = new Dictionary({
		title: 'Синонимен речник',
		id: 'synonyms-container',
		dict: 'synonyms'
	});

	var phrases = new Dictionary({
		title: 'Фразеологичен речник',
		id: 'phrases-container',
		dict: 'phrases'
	});

	Ext.get('m14').on('click', function() { bulgarian_slovak.handler() });
	Ext.get('m15').on('click', function() { slovak_bulgarian.handler() });
	Ext.get('m16').on('click', function() { synonyms.handler() });
	Ext.get('m17').on('click', function() { phrases.handler() });

});


