
Voyeur.Application.Tool = Ext.extend(Voyeur.Application, {
	constructor : function(config) {
		this.toolName = config.toolName;
		Voyeur.Application.Tool.superclass.constructor.call(this, arguments);
		this.addListener('unhandledevent', function(src, originalEvent, originalSrc, data) {
			var params = {corpus: this.getCorpus().get('id')}
			Ext.applyIf(params, data);
			var tool = null;
			var args = arguments
			if (originalEvent=='corpusTypeSelected' || originalEvent=='corpusTypesSelected') {
				tool = 'DocumentTypeFrequenciesGrid';
			}
			else if (originalEvent=='documentTypeSelected' || originalEvent=='documentTypesSelected') {
				tool = 'DocumentTypeKwicsGrid';
			}
			else if (originalEvent=='corpusDocumentSelected') {
				tool = 'DocumentTypeFrequenciesGrid';
			}
			else if (originalEvent=='CorpusGridBootstrap') {
				tool = 'CorpusGrid';
			}
			else if (originalEvent=='CorpusTypeFrequenciesGridBootstrap') {
				tool = 'CorpusTypeFrequenciesGrid';
			}
			if (tool) {
				var url = this.getBaseUrl()+'tool/'+tool+'/?'+Ext.urlEncode(params);
				var win = window.open(url, tool);
				if (win) {win.focus();}
				else {
					Ext.Msg.alert(this.localize('app.error'), Ext.DomHelper.createTemplate(this.localize('app.noOpenWin')).applyTemplate([url, tool]));
				}
			}
		}, this);

	},

    initPage: function(){

    	new Ext.Viewport({
            layout: 'border',
            items: [
				{
					xtype : 'voyeur'+this.toolName
					,layout: 'fit'
					,region : 'center'
					,listeners : {
		        		'render' : {
		        			fn : function(cmp) {
//								if (this.query) {this.dispatchEvent(this.toolName+'Bootstrap',cmp,this.query)}
								/*
		        				var params = window.location.search.substring(1);
		        				if (params) {
		        					var query = Ext.urlDecode(params);
		        					if (query.input) {
		        						Ext.applyIf(query, {corpusCreateIfNotExists: true})
		        					}
		        					this.dispatchEvent(this.toolName+'Bootstrap',cmp,query)
		        				}
		        				*/
		        			}
		        			,scope : this
		        		}
		        	}
				}
				,{
					xtype : 'voyeurFooter'
					,region : 'south'
					,title : null
					,autoHeight : true
				}
			]
        });

		
    }
	
});

Voyeur.localization.load({
	'app.noOpenWin' : {en : 'The application seems to have been unable to open a new window with relevant results, probably because you have blocked pop-up windows. If possible, you should enable pop-up windows for this site. In the meantime, you can click the following link to <a href="{0}" target="{1}">see the results</a>.'}
})
