ontos.Dispatcher = function(aConfig){
	this.config = aConfig;
	this.addEvents({
		  "sourcesetChanged" : true
		, "filterChanged" : true
		, "sortChanged" : true
		, "objectSelected" : true
		, "objectListLoaded" : true
		, "entitySelected" : true
		, "relationSelected" : true
    });
	ontos.Dispatcher.superclass.constructor.call(this, aConfig);
} 
Ext.extend(ontos.Dispatcher, Ext.util.Observable, {});

var _dispatcher = new ontos.Dispatcher({});

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.apply(Ext.QuickTips, {True:false,interceptTitles: true,autoHide:false,hideDelay:1500,hideOnClick:true});
	Ext.Ajax.timeout=300000;
	
	var sfield = new ontos.widgets.SearchField('search', {
        loadingText: ontos.locale.searching, dispatcher: _dispatcher
    });
    
    var exportForm = new Ext.form.FormPanel({
    	items: [
             new Ext.form.Radio({
             		id: 'exporttype1',
             		value: 'rdfxml',
                    fieldLabel: 'RDF/XML',
                    checked: true,
                    name: 'exporttype'
                    //anchor: '99%',
             }), new Ext.form.Radio({
             		id: 'exporttype2',
             		value: 'ntriples',
             		fieldLabel: 'N-Triples',
                    checked: false,
                    name: 'exporttype'
                    //anchor: '99%',
             }), new Ext.form.Radio({
             		id: 'exporttype3',
             		value: 'nlp',
             		fieldLabel: 'Ontos NLP',
                    checked: false,
                    name: 'exporttype'
                    //anchor: '99%',
             })
             ], buttons: [
                {xtype:'button',text: ontos.locale.Download,name: 'swspage_go',scope: this,handler: 
                    function(){
                    	ontos.exportType = Ext.getCmp('exporttype1').getValue() ? 'rdfxml' : 
                    		(Ext.getCmp('exporttype2').getValue() ? 'ntriples' : 'nlp');
                    	//alert(Ext.getCmp('exporttype').getGroupValue());
                    	//this.loadData(document.getElementById("swspage_text").value);
                        ontos.inlineExportDialog.hide();
                        if (ontos.inlineExportDialog.onexport) {
                        	ontos.inlineExportDialog.onexport();
                        }
                    }
                },
                {xtype:'button',text: ontos.locale.Cancel,scope: this,handler: 
                    function(){
                        ontos.inlineExportDialog.hide();
                    }
                }
        ],baseCls: 'x-plane',form: true,labelAlign: 'right',fieldAlign: 'right',labelWidth: 120,bodyStyle: 'background-color:white; padding:5px 5px 0;'
    });
	ontos.inlineExportDialog = new Ext.Window({items:[exportForm], layout: 'fit',plain: true,border: false,height: 150,width: 220,resizable:false,closable: true,title: ontos.locale.ExportForm,closeAction: 'hide'});
    
	viewport = new Ext.Panel({
			applyTo: 'mainpanel',
            layout:'border',
            bodyStyle: 'background: transparent',
            border: false,
            autoWidth:true,
            defaults: 'width:100%',
            monitorResize: true,
            items:[
	            {
	            	region:'north',
                    contentEl: 'north',
                    split:false,
                    height: 10,
                    minSize: 10,
                    maxSize: 10,
                    margins:'0 0 0 0',
                    border: false,
                    bodyStyle: 'background: transparent'
                }, {
                	region:'south',
                	contentEl: 'south',
                    split:false,
                    height: 10,
                    minSize: 10,
                    maxSize: 10,
                    margins:'0 0 0 0',
                    border: false,
                    bodyStyle: 'background: transparent'
				},{
	                region:'center',
	                margins:'0 0 5 5',
	                layout:'border',
	                border: false,
	                bodyStyle: 'background: transparent',
	               
	                items:[{
	                	region:'center',
	                	layout:'fit',
	                	//baseCls:'x-plain',
	                    //bodyStyle:'padding:5px 0 5px 5px',
	                    title: ontos.locale.document,
	                    border: true, 
	                    items: new ontos.AnnotatedDocumentPanel({
	                    	collapsible:false,module:_dispatcher
					    })
	                }]
	                    /*
	                    items: new ontos.AnnotatedDocumentPanel({
	                    	collapsible:false,module:_dispatcher
					    })
					    */
	            },{
                    region:'east',
                    split:true,
                    width:400,
                    margins:'0 5 5 0',
                    collapsible:true,
                    layout:'fit', 
                    //baseCls:'x-plain',
                    //bodyStyle:'padding:5px 5px 5px 5px',
                	id: "details-panel",
                	title: ontos.locale.inDetails,
                	border: true,
                	tbar: [
						new Ext.Action({id:'txtobjexport', text: ontos.locale.exportTextObjects,iconCls: 'btextexport',scope: this, disabled:true, handler: function() {
							ontos.inlineExportDialog.onexport = function() {
								var cmdparam = document.getElementById("appformparam1");
								cmdparam.name = "query"; cmdparam.value="textexport";
								
								cmdparam = document.getElementById("appformparam2");
								cmdparam.name = "type"; cmdparam.value=ontos.exportType;
								
								cmdparam = document.getElementById("appformparam3");
								cmdparam.name = "ontology"; cmdparam.value=ontos.currentOntology;
								
								cmdparam = document.getElementById("appformparam4");
								cmdparam.name = "src"; cmdparam.value=_dispatcher._subject.src;
								
								cmdparam = document.getElementById("appformparam5");
								cmdparam.name = "subjid"; cmdparam.value=_dispatcher._subject.id;
								
								var appform = document.getElementById("appform");
								appform.target="_self";
								appform.submit();
							};
							ontos.inlineExportDialog.show();
						}})],
                	items: new Ext.TabPanel({
                		id: "details-tabpanel",
                		activeTab: 0,
                        tabPosition:'top',
                        border: false,
                        bodyBorder:false,
                        items:[
                        	new ontos.DetailsFactsPanel({id:'ss',module:_dispatcher}),
                        	new ontos.DetailsDiagramPanel({module:_dispatcher})
		                ]})
                }]
              //}
				
				/*,{
	                region:'center',
	                margins:'0 0 0 0',
	                layout:'column',
	                border: false,
	                bodyStyle: 'background: transparent',
	                items:[{
	                	columnWidth:.5,
	                	layout:'fit',
	                	height: 500,
	                	baseCls:'x-plain',
	                    bodyStyle:'padding:5px 0 5px 5px',
	                    items: new ontos.AnnotatedDocumentPanel({
	                    	title: ontos.locale.document,
					        collapsible:false
					    })
	                },{
	                    columnWidth:.5,
	                    layout:'fit', 
	                    baseCls:'x-plain',
	                    bodyStyle:'padding:5px 5px 5px 5px',
	                    height: 500,
	                    items: {
	                    	id: "details-panel",
	                    	title: ontos.locale.inDetails,
	                    	layout:'fit',
	                    	border: false,
	                    	items: new Ext.TabPanel({
	                    		activeTab: 0,
	                            tabPosition:'top',
	                            border: false,
	                            bodyBorder:false,
	                            items:[
	                            	new ontos.DetailsFactsPanel({module:_dispatcher}),
	                            	new ontos.DetailsDiagramPanel({module:_dispatcher})
				                ]})
	                    }
                	}]
                }*///]
	});
	
   
	Ext.getCmp('ekbfactspanel').initialize();
    Ext.getCmp('ekbdiagrampanel').initialize();
    
    _dispatcher.on("subjectSelected", function(_subject) {
    	_dispatcher._subject = _subject;
    	Ext.getCmp('txtobjexport').setDisabled(!_subject);
    	var panel = Ext.getCmp('details-panel');
    	panel.setTitle(String.format(ontos.locale.detailsFor, _subject.label, _subject.type.replace(/_/g, " ")));
   	});
   	
   	_dispatcher.on("onSearch", function(_subject) {
   		_dispatcher._subject = _subject;
    	var panel = Ext.getCmp('details-panel');
    	Ext.getCmp('txtobjexport').setDisabled(!_subject);
    	var type = _subject.type.split("#")[1];
    	if (!type) type = _subject.type;
    	panel.setTitle(String.format(ontos.locale.detailsFor, _subject.label, type.replace(/_/g, " ")));
   	});
});
