Voyeur.Application.Tool = Ext.extend(Voyeur.Application, {
	constructor : function(config) {
		this.toolName = config.toolName;
		Voyeur.Application.Tool.superclass.constructor.call(this, arguments);
		this.addListener('CorpusSummaryResultLoaded', function(data) {
			if (this.query.hideBars == null) {
				Ext.getCmp('logo').collapse(false);
			}
			Ext.getCmp('main-panel').getLayout().setActiveItem(1);
		}, this);
		this.addListener('unhandledevent', function(src, originalEvent, originalSrc, data) {
			var params = {corpus: this.getCorpus().get('id')};
			
			// only consider types
			if (data.type) {params.type = data.type;}
			if (data.docIdType) {
				if (!params.type) {params.type = [];}
				if (typeof params.type == 'string') {params.type = [params.type];}
				var types = (typeof data.docIdType == 'string') ? [data.docIdType] : data.docIdType;
				for (var i=0;i<types.length;i++) {
					var parts = types[i].split(":");
					if (parts[1]) {params.type.push(parts[1])}
				}
				
			}
//			Ext.applyIf(params, data);
			var tool = null;
			var args = arguments;
			
			if (this.query.toolFlow) {
				var simpleFlow = 'Cirrus,DocumentTypeKwicsGrid,Reader,DocumentTypeKwicsGrid;Cirrus,DocumentTypeFrequenciesGrid,DocumentTypeKwicsGrid,Reader,DocumentTypeKwicsGrid';
				var toolFlow = this.query.toolFlow=='simple' ? simpleFlow : this.query.toolFlow;
				var scenarios = toolFlow.split(";");
				var scenario = scenarios.length==1 ? scenarios[0] : scenarios[this.getCorpus().getSize()==1 ? 0 : 1];
				var tools = scenario.split(',');
				for (var i=0;i<tools.length-1;i++) {
					if (tools[i]==this.toolName) {
						tool = tools[i+1];
						break;
					}
				}
			}
//			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) {
				url = this.getBaseUrl()+'tool/'+tool+'/?'+Ext.urlEncode(params)+'&event='+originalEvent+'&toolFlow='+this.query.toolFlow;
			}
			else {
				url = this.getBaseUrl()+'?'+Ext.urlEncode(params)+'&skin=simple&event='+originalEvent;
			}
			var win = window.open(url, originalEvent);
			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(){
    	var toolConfig = {
        	id: 'tools-container',
    		layout: 'fit'
        }
    	if (Voyeur.Tool[this.toolName] == undefined) {
    		toolConfig.html = '<div style="margin: 10px; color: #B23B2E;">'+this.localize('app.invalidTool')+'</div>'
    	} else {
    		toolConfig.xtype = 'voyeur'+this.toolName; 
    	}
    	
    	var items = [{
            region: 'north',
            xtype: 'voyeurHeader',
            singleTool: true
        },{
            region: 'center',
            layout: 'card',
			activeItem: 0,
			id : 'main-panel',
			defaults: {border: false},
			layoutConfig: {align: 'stretch', pack: 'start', animate: true, titleCollapse: true},
			items : [{
				layout: 'hbox',
				defaults: {
					border: false
				},
				items: [{
					width: 100
				},{
					xtype: 'voyeurDocumentInputAdd',
					header: true,
					border: true,
					frame: true,
					collapsible: false,
					margins: '25 0 0 0',
					height: 175,
					flex: 1,
					pack: 'center'
				},{
					width: 100
				}]
			},
            toolConfig
            ]
        },{
            region: 'south',
            xtype: 'voyeurFooter'
        }];
    	
    	if (this.query.hideBars) {
    		items = items[1];
    	}
    	
    	var v = new Ext.Viewport({
            layout: 'border',
            defaults: {
            	border: false
            },
            items: items
        });
    }
	
});

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>.'},
	'app.invalidTool' : {en : 'The specified tool does not exist.'} 
})

