
Voyeur.Application.Default = Ext.extend(Voyeur.Application, {
	firstCorpusTermsFrequenciesRowClick :false
	,constructor : function(config) {
		Voyeur.Application.Default.superclass.constructor.call(this, config);
		this.addListener('CorpusSummaryResultLoaded', function(data) {
			Ext.getCmp('top-tools').hide();
			Ext.getCmp('middle-tools').show();
			Ext.getCmp('bottom-tools').show()
			if (!Ext.isIE) {
				Ext.getCmp('bottom-tools').collapse(true);
			}
			Ext.getCmp('logo').collapse(true);
//			if (data.corpus.documents.length==1) {
//				Ext.getCmp('voyeurCorpusTypeFrequenciesGrid').collapse(true);
//			}
		}, this);

//		this.addListener('unhandledevent', function(src, originalEvent) {
//			if (originalEvent=='CorpusGridBootstrap') {
//				Ext.getCmp('corpusTabPanel').activate(Ext.getCmp('voyeurCorpusGrid'));
//			}
//		}, this);
		this.addListener('CorpusGridBootstrap', function(){
			Ext.getCmp('corpusTabPanel').activate(Ext.getCmp('voyeurCorpusGrid'));
		})
		this.addListener('corpusTypeSelected', function(){
			this.expandIfCollapsed('voyeurDocumentTypeFrequenciesGrid', 'bottom-tools');
		})
		this.addListener('corpusTypesSelected', function(){
			this.expandIfCollapsed('voyeurDocumentTypeFrequenciesGrid', 'bottom-tools');
		})
		this.addListener('corpusDocumentSelected', function(){
			this.expandIfCollapsed('voyeurDocumentTypeFrequenciesGrid');
		})
		this.addListener('CorpusTypeFrequenciesGridBootstrap', function(){
			this.expandIfCollapsed('CorpusTypeFrequenciesGridBootstrap');
		})
		this.addListener('documentTypeSelected', function(){
			this.expandIfCollapsed('bottom-tools');
		})
		
	},

	expandIfCollapsed: function() {
		var panel;
		for (var i=0;i<arguments.length;i++) {
			panel = Ext.getCmp(arguments[i]);
			if (panel && panel.collapsed) {panel.expand(false);}
		}
	}
    ,initPage: function(){
		var defaults = {
			split : true,
			useSplitTips : true,
			splitTip : Voyeur.localization.get('application.split_tip'),
			collapsibleSplitTip : Voyeur.localization.get('application.collapsible_split_tip'),
			collapsible : true,
			//collapseMode : 'mini',
			border : false
		}

		var lang = this.getLocalization().getLang();
        new Ext.Viewport({
            layout: 'border',
			defaults : defaults,
            items: [
				{
					region : 'north',
					id : 'logo',
					html : '<div id="logo-container"></div>',
					bodyStyle: {'text-align': 'center'},
					collapseMode : undefined,
					header: true,
					tools : [],
					listeners: {
						'collapse': {
							fn: function(panel) {
								var collapsed = panel.ownerCt.layout.north.collapsedEl;
								var collapsedEl = collapsed.first();
								if (collapsedEl.id==collapsed.last().id) {
									var save = Ext.DomHelper.insertBefore(collapsedEl, '<div class="x-tool x-tool-save" ext:qtip="'+this.localize('default_skin.saveTip')+'"> </div>', true);
									save.addListener('click', function (ev) {
										ev.stopPropagation();
										Ext.Msg.alert(this.localize('default_skin.saveTitle'), new Ext.Template(this.localize('default_skin.saveMsg')).apply([this.getBaseUrl()+'?corpus='+this.getCorpus().getId()]));
									}, this)
									var help = Ext.DomHelper.insertBefore(save, '<div class="x-tool x-tool-help" ext:qtip="'+this.localize('default_skin.helpTip')+'"> </div>', true);
									help.addListener('click', function (ev) {
										ev.stopPropagation();
										Ext.Msg.alert(this.localize('default_skin.helpTitle'),this.localize('default_skin.helpTip'));
									}, this)
								}
							}
							,scope: this
						}
					}
				},
				{
					region: 'center'
					,layout: 'border'
					,defaults : defaults
					,id : 'main-panel'
					,bodyStyle: {
						'background-color': 'white'
					}
					,items : [
						{
							region: 'north'
							,id: 'top-tools'
							,split : false
							,collapsible : false
							//,autoHeight: true
							,frame: true
							,xtype: 'voyeurDocumentInputAdd'
							,height: 175
							,margins : {
								left: 100
								,right: 100
								,top: 25
							}
						}
						,{
							region: 'center'
							,layout: 'border'
							,defaults : defaults
//							,height : 200
//							,hidden : true
							,id: 'middle-tools'
							,items : [
								{
									region : 'west'
									,width : '35%'
									,defaults : defaults
									,layout : 'fit'
									,collapsed: false
									,listeners : {
										'expand' : function(panel) {
											panel.getComponent(0).expand(true);
										}
									}
									,items: [{
										xtype : 'voyeurCorpusTypeFrequenciesGrid'
										,id : 'voyeurCorpusTypeFrequenciesGrid'
										,collapsed: false
										,listeners : {
											'collapse' : {
												fn : function(panel) {panel.ownerCt.collapse(true);}
											}
											,'expand' : {
												fn : function(panel) {panel.ownerCt.expand(true);}
											}
										}
									}]
								},{
									

									
									xtype : 'panel'
									,region : 'center'
									,header: true
									,layout: 'fit'
									,collapsible: false
									,tools: [{
										id : 'gear'
										,qtip : '<h3>' + this.localize('tool.options') + '</h3>' +
											this.localize('tool.optionsTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onOptions.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									},{
										id : 'save'
										,qtip : '<h3>' + this.localize('tool.export') + '</h3>' +
											this.localize('tool.exportTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onExport.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									},{
										id : 'help'
										,qtip : '<h3>' + this.localize('tool.help') + '</h3>' +
											this.localize('tool.helpTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onHelp.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									}]
									,items : [
									          {
									        	  
									          xtype: 'tabpanel'
									        	  ,activeTab: 0
													,id: 'corpusTabPanel'
													,items : [
																{
																	xtype : 'voyeurCorpusSummary'
																	,id : 'voyeurCorpusSummary'
																	,tabTip: Voyeur.Tool.CorpusSummary.prototype.i18n.help[lang]
																},{
																	xtype : 'voyeurCorpusGrid'
																	,id : 'voyeurCorpusGrid'
																	,tabTip: Voyeur.Tool.CorpusGrid.prototype.i18n.help[lang]
																	,viewConfig : {
																		emptyText : '<b>'+Voyeur.localization.get('voyeurTool.noResults') + '</b> Add new texts to populate the corpus.'
																	}
																}								
															]
									          }
									]
								}
								,{
									region : 'east'
									,defaults : defaults
									,layout : 'fit'
									,width : '35%'
									,collapsed: !Ext.isIE
									,listeners : {
										'expand' : function(panel) {
											panel.getComponent(0).expand(true);
										}
									}
									,items: [{
										xtype : 'voyeurDocumentTypeFrequenciesGrid'
										,id : 'voyeurDocumentTypeFrequenciesGrid'
										,collapsed: !Ext.isIE
										,listeners : {
											'collapse' : function(panel) {
												panel.ownerCt.collapse(true);
											}
											,'expand' : function(panel) {
												panel.ownerCt.expand(true);
											}
										}
										,viewConfig : {
											emptyText : '<b>'+this.localize('tool.noResults') + '</b> Select one or more rows from the <i>Corpus Types</i> tool.'
										}
									}]
								}
							]
						},
						{
							region : 'south'
							,layout : 'border'
							,defaults : defaults
							,height : 300
							,id : 'bottom-tools'
//							,hidden : true
							,items : [
								{
									region : 'west',
									width : '30%',
									layout : 'fit',
									listeners : {
										'expand' : function(panel) {
											panel.getComponent(0).expand(true);
											panel.ownerCt.expand(true);
										}
										,'collapse' : function(panel) {
											panel.getComponent(0).collapse(true);
										}
									}
									,items : [{
										xtype : 'voyeurTypeFrequenciesChart'
										,html : '<span class="x-grid-empty"><b>'+this.localize('tool.noResults') + '</b> Select one or more rows from the <i>Corpus Types</i> tool.</span>'
										,id : 'voyeurTypeFrequenciesChart'
										,listeners : {
											'collapse' : function(panel) {
												panel.ownerCt.collapse(true);
											}
											,'expand' : function(panel) {
												panel.ownerCt.expand(true);
											}
										}
										,collapsible : true
									}]
								},{
									xtype : 'panel'
									,region : 'center'
									,width : '70%'
									,header: true
									,layout: 'fit'
									,tools: [{
										id : 'gear'
										,qtip : '<h3>' + this.localize('tool.options') + '</h3>' +
											this.localize('tool.optionsTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onOptions.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									},{
										id : 'save'
										,qtip : '<h3>' + this.localize('tool.export') + '</h3>' +
											this.localize('tool.exportTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onExport.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									},{
										id : 'help'
										,qtip : '<h3>' + this.localize('tool.help') + '</h3>' +
											this.localize('tool.helpTip')
										,handler : function(ev, tool, panel, toolConfig) {
											var realPanel = panel.findByType('tabpanel', true)[0].getActiveTab();
											realPanel.onHelp.call(realPanel, ev, tool, realPanel, toolConfig);
										}
									}]
									,items : [
									          {
									        	  
									          xtype: 'tabpanel'
									        	  ,activeTab: 0
									        	  ,id: 'documentTypeDataPanel'
													,items : [

													   /*{
														xtype: 'voyeurLinks'
															,viewConfig : {
																emptyText : '<b>'+this.localize('tool.noResults') + '</b> Select one or more rows from the <i>Document Types</i> tool.'
															}
															,id : 'voyeurLinks'											
															,tabTip: Voyeur.Tool.Links.prototype.i18n.help[lang]
														},*/
																{
																	xtype: 'voyeurDocumentTypeKwicsGrid'
																	,viewConfig : {
																		emptyText : '<b>'+this.localize('tool.noResults') + '</b> Select one or more rows from the <i>Document Types</i> tool.'
																	}
																	,id : 'voyeurDocumentTypeKwicsGrid'											
																	,tabTip: Voyeur.Tool.DocumentTypeKwicsGrid.prototype.i18n.help[lang]
																	/*,listeners: {
																		'collapse': function(panel){
																			panel.ownerCt.collapse(true);
																			}
																		}*/
																	}
																,{
																	xtype: 'voyeurDocumentTypeCollocateFrequenciesGrid',
																	id: 'voyeurDocumentTypeCollocateFrequenciesGrid',
																	tabTip: Voyeur.Tool.DocumentTypeCollocateFrequenciesGrid.prototype.i18n.help[lang],
																	listeners: {
																		'collapse': function(panel){
																			panel.ownerCt.collapse(true);
																		}
																	},
																	viewConfig: {
																		emptyText : '<b>'+this.localize('tool.noResults') + '</b> Select one or more rows from the <i>Document Types</i> tool.'
																	}
																}								
															]
									          }
									]
								}
								
							]
						}
					]
				},
				{
	                region: 'south',
					xtype : 'voyeurFooter',
					split : false,
					collapsible : false,
					collapseMode : null,
					border : false,
					title : null
            	}
			]
			,listeners : {
        		'render' : {
        			fn : function(cmp) {
        				
        				var query = Ext.urlDecode(window.location.search.substring(1));
        				if (query.corpus || query.input) {
        					Ext.getCmp('top-tools').hide();
        					if (query.input) {
        						Ext.applyIf(query, {corpusCreateIfNotExists: true})
        					}
        					Ext.getCmp('middle-tools').show();
        					Ext.getCmp('bottom-tools').show();
        					this.update({
        						title : this.localize('app.loading')
        						,params : query
        						,tool : 'CorpusSummary'
        					})
        				}
        				else {
            				Ext.getCmp('middle-tools').hide();
            				Ext.getCmp('bottom-tools').hide();
        				}
        			}
        			,scope : this
        		}

        	}
        });

		
    }
	
});

Voyeur.localization.load({
	'default_skin.saveTitle' : {en : 'Save'}
	,'default_skin.saveTip' : {en : 'Click here to generate a URL to access this corpus again.'}
	,'default_skin.saveMsg' : {en : '<p>You can click or copy the following URL to open the current corpus again:</p><div><a href="{0}" target="_blank">{0}</a></div><p>Please note that this corpus may be removed after a couple of days of inactivity or may be invalidated by an update to Voyeur.</p>'}
	,'default_skin.helpTitle' : {en : 'Help'}
	,'default_skin.helpTip' : {en : '<p>Voyeur is the web-based tool for reading and analyzing your digital texts.</p><p>Visit <a href=\'http://hermeneuti.ca/voyeur/\'>http://hermeneuti.ca/voyeur/</a> for more information about Voyeur, to access tutorial materials, and to submit feedback and bug reports.</p>'}
})

