Ext.ns('Ext.ux.grid');

Ext.ux.grid.DemonModuleSupport = function(config) {
	
	Ext.apply(this, config);
	this.addEvents(
		'show'
		,'cancel'
	);
	Ext.ux.grid.DemonModuleSupport.superclass.constructor.call(this);

	this._spotlight = new Ext.DemonSpotlight({duration:0.1});
	
	this._window = new Ext.ux.DemonWindow({
		title: this.title
		,border: true
		,autoScroll: true
		,layout: 'fit'
		,width: Ext.getBody().getWidth() - 200
		,height: Ext.getBody().getHeight() - 150
		,bodyStyle: 'padding:10px; background: #ffffff;'
		,resizable: false
		,draggable: true
		,closable: false
		,modal: this.modal
		,iconCls: 'iconModuleSupport'
		,title: this.title
		,buttons: [
					{
						text: this.closeTitle,
						iconCls: 'iconClose',
						handler: this.hide,
						scope: this
					}
		]
	});
	
	this._window.on('show', function(){
		this.fireEvent('show', this);
	}, this);

	this._buttonSupport = new Ext.Button({ 
						xtype:'tbbutton', 
						text: this.titleSupport, 
						iconCls:'iconModuleSupport',
						tooltip :'Filtrowanie tabeli',
						handler: this.show,
						scope: this
	});

};
 
Ext.extend(Ext.ux.grid.DemonModuleSupport, Ext.util.Observable, {

	module: null
    
	,title: 'Okno pomocy'
	
    ,titleSupport: 'Pomoc dla modułu'
	
	,closeTitle: 'Zamknij'
	
	,modal: false	
	
	,_window: null	
	
	,_spotlight: null
	
	,init:function (grid) {
        this.grid = grid;
		grid.on({
			 scope:this
			,render:this.onRender
		});
	}
	
	,show: function(el){
		var c = this._window.show(el);
		if (typeof(this._spotlight) != 'undefined') { this._spotlight.show(); }

		var connection = new Ext.data.Connection();
		connection.request({
			url: '../backend/supports/view',
			method: 'post',
			params: { module: this.module },
			success: function(responseObject) {
				 var json = Ext.decode(responseObject.responseText);
				 if (json.data.support_content !='') {
					c.body.update(json.data.support_content);
				 } else {
					c.body.update('... brak pomocy.');
				 }	
			},
			failure: function(responseObject){
				 Ext.Msg.show({ title: 'Komunikat', msg: 'Nieznany blad.', icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK });
			}
		 });

	}
	
	,hide: function(){
		if (typeof(this._spotlight) != 'undefined') {this._spotlight.hide(); }
		this._window.hide()
	}
	
	,destroy: function(){
		this._window.hide();
		this.purgeListeners();
		var self = this;
		delete self;
	}
	
	,onRender:function() {
		this.tbar = this.grid.getTopToolbar();
		this.tbar.add('->');
		this.tbar.add('-');
		this.tbar.add(this._buttonSupport);
	
	}

});

Ext.reg('demonsupport', Ext.ux.grid.DemonModuleSupport); 