AdvancedSearch = {};

AdvancedSearch.SearchForm = function(config, resultContainer){

	this.handler = config.handler;
	this.catClient = config.catClient;
	this.addField = config.addField;
    this.srvKeywordsPath = config.srvKeywordsPath;
	this.resultContainer=resultContainer;

	if(config.thesaurusPath) this.thesPath = config.thesaurusPath; 
	else this.thesPath = '/projects/gemet/';
	
	var _thesaurusWindow = null;

/****************** thesaurus 1 GE ***********************/
    /*var termsStore = new Ext.data.Store({
        url: 'libs/gemetclient/proxy.php',
        baseParams: {url: ''},
        language: 'en',
        reader: new Ext.data.JsonReader({
            	root: 'results',
            	idProperty: 'term',
        		fields: [
        			{name: 'id', mapping: 'uri'}, 
        			{name: 'term', mapping: 'preferredLabel.string'},
        			{name: 'definition', mapping: 'definition'}
        		]
    	})
    }); 
    
   this.suggestionCfg = function(obj, o){
      var conceptURI =  "http://www.onegeology-europe.eu/concept/";
      var addr = "http://gemet.esdi-humboldt.cz/thesaurus/";
      if(!addr) addr = this.url;
      var q = obj.baseParams.query;
      if(!q) q = '.';
      obj.baseParams.url = addr+"getConceptsMatchingRegexByThesaurus?thesaurus_uri="+conceptURI+"&language="+HS.getLang(2)+"&regex="+q;    
    }
    
    termsStore.on('beforeload', this.suggestionCfg, this, {});
    

    
    */
	
	/* 1GE specific */
/*    var termsStore = new Ext.data.Store({
        url: 'libs/gemetclient/proxy.php',
        baseParams: {url: 'http://gemet.esdi-humboldt.cz/thesaurus/getTopmostConcepts?thesaurus_uri=http://www.onegeology-europe.eu/concept/&language='+HS.getLang(2)},
        reader: new Ext.data.JsonReader({
            	root: 'results',
            	idProperty: 'term',
        		fields: [
        			{name: 'id', mapping: 'uri'}, 
        			{name: 'term', mapping: 'preferredLabel.string'},
        			{name: 'definition', mapping: 'definition'}
        		]
    	})
    });
 */   
    /* Plan4 specific */
/*     var hierarchyLevelStore = new Ext.data.SimpleStore({
        fields: ['value'],
        data : [
        	['spatialPlan'],
        	['spatialPlan.country'],
        	['spatialPlan.state'],
        	['spatialPlan.regional'],
        	['spatialPlan.subRegional'],
        	['spatialPlan.supraLocal'],
        	['spatialPlan.local'],
        	['spatialPlan.subLocal'],
        	['spatialPlan.other']
        ]
    });
    
 */   
    
    //--- seznam organizaci do combo boxu   
    var partyStore = new Ext.data.JsonStore({
    	// TODO - do konfigurace
        url: config.mickaPath+'/util/organizace.php',
        root: 'records',
        id: 'id',
        baseParams: {lang: HS.getLang()},
        fields: ['id', 'name']
    });
    
    // meritka
    var scaleList = new Ext.data.SimpleStore({
    	id: 0,
        fields: ['label'],
        //data : [['1000','1:1000'],['5000','1:5000'],['10000','1:10 000'],['50000','1:50 000'],['100000','1:100 000'],['500000','1:500 000'],['1000000','1:1000 000']]
        data : [[1000],[5000],[10000],[50000],[100000],[500000],[1000000]]
    });
    
    // zda data nebo sluzba
    var typeList = new Ext.data.SimpleStore({
      data: [['',' '],['data',HS.i18n('data')],['service',HS.i18n('service')]],
      fields: ['name', 'label']
    });

    var orderList = new Ext.data.SimpleStore({
      data: [['title', HS.i18n('Title')], ['date', HS.i18n('Date')]],
      fields: ['kod', 'label']
    });
    
    // --- XML cteni ---  
  	var xmlRecordDef = Ext.data.Record.create([
      {name: 'name', mapping: '@name'}, 
      {name: 'label', mapping: '/'}                 
  	]);
  
  	var xmlRecordDefCode = Ext.data.Record.create([
      {name: 'name', mapping: '@code'}, 
      {name: 'label', mapping: '/'}                 
  	]);
  
    var topicsReader = new Ext.data.XmlReader({
      record: "topicCategory/value"         
    }, xmlRecordDef);  
    
   var topicsStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: topicsReader,
      autoload: true
   });

   var langReader = new Ext.data.XmlReader({
     record: "language/value" 
   }, xmlRecordDefCode);  
    
   var langStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: langReader,
      autoload: true
   });
	
   var serviceTypeReader = new Ext.data.XmlReader({
     record: "serviceType/value" 
   }, xmlRecordDef);  
 
   var serviceStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: serviceTypeReader,
      autoload: true
   });
  
   var roleReader = new Ext.data.XmlReader({
     record: "role/value" 
   	}, xmlRecordDef);  
    
   var roleStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: roleReader,
      autoload: true
   });

   var dateTypeReader = new Ext.data.XmlReader({
     record: "dateType/value" 
   }, xmlRecordDef);  
    
   var dateTypeStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: dateTypeReader,
      autoload: true
   });
  
   var aConstrReader = new Ext.data.XmlReader({
     record: "accessConstraints/value" 
   }, xmlRecordDef);  
    
   var aConstrStore = new Ext.data.Store({
      url: 'xsl/codelists_'+HS.getLang()+'.xml',
      reader: aConstrReader,
      autoload: true
   }); 

    topicsStore.load();
    langStore.load();
    serviceStore.load();
    roleStore.load();
    dateTypeStore.load();
    aConstrStore.load();
	  //termsStore.load();
	  //partyStore.load();

   var kw = new Ext.form.TriggerField({
   		fieldLabel: HS.i18n('Keywords'),
      width: 300, 
   		name:'keywords'
   });
   
   kw.onTriggerClick = function(a,b,c){
   		if(!_thesaurusWindow){
	   	  	_thesaurusWindow = new Ext.Window({
  				width:400,
  				height:500,
  				layout: 'fit',
  				closeAction:'hide',
  				items: keywordManager
  			});	
  		}
   		_thesaurusWindow.show();
   };	
  
  this.getValues= function(){
    var form = this.theForm.getForm();
    var vals = form.getValues();
  	return vals;
  }

	// Nastavi obsah formulare (napr. daty zvenku)
	this.setValues= function(v){
    	if (v.hlevel && v.hlevel.length>0) {
			Ext.getCmp('hlevel-'+v.hlevel).toggle();
		};
    	this.theForm.form.setValues(v);
    	this.onChangeResource();
	}
    
  this.onChangeResource = function(combo, record, index){
    var values = this.getValues();
    var s = Ext.get('service-select');
    var d = Ext.get('topic-select');
    var s = s.findParent('div').parentNode.parentNode;
    var d = d.findParent('div').parentNode.parentNode;
    s.style.display = 'none';
	d.style.display = 'none';
  	if(values.hlevel=='service') s.style.display = 'block'; 
  	else if(values.hlevel=='data') d.style.display = 'block';
  }
  
  this.onEnter = function(f,e){
    if(e.getKey() == e.ENTER){
        this.search();
    } 
  }
  
  this.fillKeywords = function(result){
    this.theForm.form.setValues({'keywords': result.terms[HS.getLang(2)]});
  	_thesaurusWindow.hide();
  };
  
  /*var thesaurusReader = new ThesaurusReader({
      lang: HS.getLang(2),
      appPath: this.thesPath,
      outputLangs: [HS.getLang(2)], 
      title: 'GEMET Thesaurus',
      separator: ' > ',
      returnPath: false,
      returnInspire: true,
      handler: fillKeywords
  });           */

    var thesaurusReader = new ThesaurusReader({
      lang: HS.getLang(2), 
      outputLangs: [HS.getLang(2)],
      separator: '/',
      appPath: this.thesPath,
      returnPath: false,
      returnInspire: true,
      handler: this.fillKeywords,
      scope: this,
      defaultThesaurus: 'GEMET'/*,
      thesaurus: {
        '1GE': {
          url:        "http://gemet.esdi-humboldt.cz/thesaurus/",
          concept:    "http://www.onegeology-europe.eu/concept/",
          theme:      "http://www.onegeology-europe.eu/theme/",        
          group:   	  null,
          supergroup: null
        }    
     }   */   
  });

  
  var services = new InspireServiceReader({
  	lang: HS.getLang(2),
  	outputLangs: [HS.getLang(2)], 
  	handler: this.fillKeywords,
  	scope: this,
  	serviceUrl: this.srvKeywordsPath 
  }); 

  var keywordManager = new Ext.TabPanel({
    activeTab: 0,
    items: [
      {id:'gemet', title: 'GEMET', items: [thesaurusReader], layout: 'fit'},
      {id:'services', title: HS.i18n('InspireServices'), items: [services], layout: 'fit', disabled: false}
    ]
  }); 
  
  /*var b = keywordManager.getItem('gemet');
  b.doLayout(); */

  this.boundingBoxField = new Ext.form.TextField({
                    name: 'bbox',
                    width: 200, 
                    fieldLabel: HS.i18n('Bounding box')
    });
  
   this.setTypes = function(obj){
   		var items = obj.ownerCt.items.items;
   		for(var i=0; i<items.length; i++){  			
   			if(items[i] != obj) { 
					items[i].toggle(false)
				} else {
					var menuNo = i+1;
				};
   			}

			if(obj.pressed) {
				this.theForm.getForm().setValues({hlevel: obj.fieldLabel});
				//SetTypeValue(obj.fieldLabel,'menu'+menuNo); -- spis vyhodi udalost v onChangeResource
			} else {
				this.theForm.getForm().setValues({hlevel: ''});
			}

   		this.onChangeResource();
   }      
  
    this.theForm = new Ext.FormPanel({
		//frame:true,
		//border:false,
		labelWidth: 120,
		//layout: 'fit',
		buttonAlign: 'center',
        //defaults: {width: 300},
        defaultType: 'textfield',
        autoScroll: true,
		items:[
				{
						fieldLabel: HS.i18n('Resource Type'),
						xtype: 'panel',
						layout: 'table',
						defaultType: 'button',
						id: 'resourceType',
						defaults: {width:80, ennableToggle: true},
						items: [
							{
								fieldLabel: 'data',
								id: 'hlevel-data',
								enableToggle: true,
								scale: 'medium',
								icon: config.catClientPath+"/style/img/lyr.gif",
								listeners: {'click': this.setTypes, scope: this},
								text: HS.i18n('Data')
							},
							{
								fieldLabel: 'service',
								id: 'hlevel-service',
								enableToggle: true,
								scale: 'medium',
								icon: config.catClientPath+"/style/img/serv.gif",
								listeners: {'click': this.setTypes, scope: this},
								text: HS.i18n('Service')
							},
							{
								fieldLabel: 'map',
								id: 'hlevel-map',
								enableToggle: true,
								scale: 'medium',
								icon: config.catClientPath+"/style/img/sw.gif",
								listeners: {'click': this.setTypes, scope: this},
								text: HS.i18n('Map')
							}
						]
				},
				{
						name: 'hlevel',
						xtype: 'hidden'
				},
				{
						name: 'anytext',
						width: 330,
						selectOnFocus:true,
						fieldLabel: HS.i18n('Text'),
						listeners: {specialkey: this.onEnter, scope: this},
						minChars: 3
				},
				{
            			xtype: 'radiogroup',
            			fieldLabel: '',
            			autoHeight: true,
			            items: [
			                {boxLabel: HS.i18n('Fulltext'), name: 'ttype', inputValue: 'anytext', checked: true},
			                {boxLabel: HS.i18n('Title'), name: 'ttype', inputValue: 'title'},
			                {boxLabel: HS.i18n('Abstract'), name: 'ttype', inputValue: 'abstract'},
			                {boxLabel: HS.i18n('Lineage'), name: 'ttype', inputValue: 'lineage'}
			            ]
        		},
					
/* experimental 1GE implementation 


				{
						name: 'onege',
						xtype: 'combo',  
						fieldLabel: HS.i18n('1-GE'),
						displayField:'term',
						valueField: 'term',
						forceSelection: true,
						typeAhead: true,
						store: termsStore,
						minChars: 3,
						//mode: 'local',
						triggerAction: 'all'
				},
*/
//--------------------------------------------------------------------------

/* Plan4all specific */


/*				{
						name: 'hierarchyLevelName',
						xtype: 'combo',  
						fieldLabel: HS.i18n('Type'),
						displayField: 'value',
						valueField: 'value',
						forceSelection: true,
						typeAhead: true,
						store: hierarchyLevelStore,
						minChars: 3,
						mode: 'local',
						triggerAction: 'all'
				},
*/
//--------------------------------------------------------------------------
				{		
						name: 'party',
						xtype: 'combo',
            			width: 300,  
						fieldLabel: HS.i18n('Organisation'),
						store: partyStore,
						displayField:'name',
						valueField:'name',
						typeAhead: true,
						triggerAction: 'all',
						minChars: 3
				},

				/*{
						name: 'party',
						//xtype: 'text',  
						fieldLabel: HS.i18n('Organisation'),
						listeners: {specialkey: this.onEnter, scope: this},
						minChars: 1
				}, */
				/*{
						hiddenName: 'role',
						xtype: 'combo',  
						fieldLabel: HS.i18n('Party Role'),
						displayField: 'label',
						valueField: 'name',						
						store: roleStore,
						triggerAction: 'all',
						typeAhead: true,
						mode:'local'
				},    */
				/*{
						xtype: 'combo',
						hiddenName: 'hlevel',
						fieldLabel: HS.i18n('Resource type'),
						valueField: 'name',
						displayField: 'label',						
						store: typeList,
						triggerAction: 'all',
						typeAhead: true,
						width: 75,
						mode:'local',
						listeners: {'select': this.onChangeResource, scope: this}
				},*/
				{
						xtype: 'combo',  
						fieldLabel: HS.i18n('Service Type'),
						id: 'service-select',
						hideParent: true, 
						displayField:'label',
						valueField: 'name',
						hiddenName: 'serviceType',
						typeAhead: true,
						store: serviceStore,
						triggerAction: 'all',
						editable: false,
						mode:'local'
				},
				{
						xtype: 'combo',  
						fieldLabel:HS.i18n('Topic category'),
						id: 'topic-select',
						displayField:'label',
						valueField: 'name',
						forceSelection: true,
						hiddenName: 'topic',
						typeAhead: true,
						store: topicsStore,
						triggerAction: 'all',
						mode:'local'
				},
						kw,
						
				//--- bounding box		
				{
						xtype: 'panel',
						layout: 'column',
						items:[{
								width: 330,
								layout: 'form',
								items:[this.boundingBoxField]
							},
							{
								labelWidth: 1,
								layout: 'form',
								items:[
									{
										xtype: 'checkbox',
										boxLabel: HS.i18n('Inside only'),
										name: 'inside'
									}
								]	
							}
						]	
        		},
				{
						xtype: 'panel',
						layout: 'column',
						width: '100%',
						items:[{
								width: 230,
								layout: 'form',
								items:[
									{
										xtype: 'combo',
										store: scaleList,
										//valueField: 'denom',
										displayField: 'label',
										lazyRender: true,
										//align: 'right',
										fieldLabel: HS.i18n('Scale')+":  1",
										width: 95,
										name: 'scale1',
										triggerAction: 'all',
										mode:'local'
									},
									{
										xtype: 'datefield',
										format: 'd.m.Y',
										fieldLabel: HS.i18n('Temporal extent'),
										name: 'tempFrom'
									}
								]
							},
							{
									labelWidth: 1,
									layout: 'form',
									items:[{
											xtype: 'combo',
											store: scaleList,
											displayField: 'label',
											//valueField: 'denom',
											//fieldLabel: HS.i18n('to'),
											width: 95,
											name: 'scale2',
											triggerAction: 'all',
											mode:'local'
										},
										{
											//fieldLabel: HS.i18n('to'),
											xtype: 'datefield',
											format: 'd.m.Y',
											name: 'tempTo'
										}
									]
							}]
					},

				{
						xtype: 'panel',
						layout: 'column',
						width: '100%',
						items:[{
								width: 230,
								layout: 'form',
								items:[
									{
										xtype: 'datefield',
										format: 'd.m.Y',
										fieldLabel: HS.i18n('Date'),
										name: 'dateFrom'
									},
									{
										xtype: 'datefield',
										format: 'd.m.Y',
										fieldLabel: HS.i18n('Date')+ "-" +HS.i18n('Metadata'),
										name: 'modifiedFrom'
									}									
								]
							},
							{
								labelWidth: 1,
								width: 110,
                  				layout: 'form',
								items:[
									{
										//fieldLabel: HS.i18n('to'),
										xtype: 'datefield',
										format: 'd.m.Y',
										name: 'dateTo'
									},
									{
										xtype: 'datefield',
										format: 'd.m.Y',
										//fieldLabel: HS.i18n('to'),
										name: 'modifiedTo'
									}
									
								]
							},
							{
								labelWidth: 35,
								layout: 'form',
								items:[
									{
										fieldLabel: HS.i18n('Type'),
										xtype: 'combo',
										width: 80,
						        		displayField:'label',
						        		valueField: 'name',
										store: dateTypeStore,
										mode: 'local',
										triggerAction: 'all',
										editable: false,
										hiddenName: 'dateType'
									}
								]
							}   
            			]
					},    


					{
						xtype: 'combo',  
						fieldLabel: HS.i18n('Language'),
						width: 150,
						hiddenName: 'language',
						displayField: 'label',
						valueField: 'name',
						forceSelection: true,
						typeAhead: true,
						store: langStore,
						triggerAction: 'all',
						editable: false,
						mode:'local'
					},	
					/*{
						xtype: 'fieldset',
            			title: 'INSPIRE',
            			width: '96%',
            			defaultType: 'textfield',
            			autoHeight: true,
            			collapsible: true,
            			collapsed: true,
            			defaults: {width: 300},
						items: [
							{
								xtype: 'combo',
								name: 'useLimitation',
								store: [['no conditions apply',HS.i18n('no conditions apply')], ['conditions unknown',HS.i18n('conditions unknown')]],
								fieldLabel: HS.i18n('Use limitation'),
								mode:'local'
							},
							
							{
								xtype: 'combo',
								hiddenName: 'accessConstraints',
								store: aConstrStore,
								fieldLabel: HS.i18n('Limitation on access'),
								mode:'local',
								displayField: 'label',
								valueField: 'name'
							},
							{
								name: 'specification',
								fieldLabel: HS.i18n('Specification')
							},
							{
								xtype: 'combo',
								name: 'degree',
								store: [['compliant',HS.i18n('compliant')], ['not evaluated',HS.i18n('not evaluated')]],
								fieldLabel: HS.i18n('Degree'),
								mode:'local',
								displayField:'label',
								valueField: 'name'
							}	
						]
					}*/

					{
						xtype: 'combo',
						hiddenName: 'sortBy',
						width: 150,
						store: orderList,
						fieldLabel: HS.i18n('Order by'),
						mode:'local',
						displayField:'label',
						editable: false,
						forceSelection: true,
						typeAhead: true,
						triggerAction: 'all',
						valueField: 'kod'
					} 
			]
	});

	this.clearForm = function(){
		this.theForm.getForm().reset();
		var resourceTypeItems = Ext.getCmp('resourceType').items.items;
		var s = Ext.get('service-select');
		var d = Ext.get('topic-select');
		var s = s.findParent('div').parentNode.parentNode;
		var d = d.findParent('div').parentNode.parentNode;
		s.style.display = 'none';
		d.style.display = 'none';

		for (var item=0; item < resourceTypeItems.length; item++){
			resourceTypeItems[item].toggle(false);
		}

		this.fireEvent("formcleared",this.theForm);
			if(this.addField) this.addField.value = '';
			//this.catClient.reset();
	}

  /**
   * Process form values to search handler
   * @param activate {Boolean} to activate TabPanel with CSW
   */
  this.search = function(activate){
	var formValues = this.getValues();
	this.handler(formValues, {sortBy:formValues.sortBy,activate: activate});
  }

  //changes the form items according to resource type
  /*this.changeType = function(type){
    var s = Ext.get('service-select');
    var d = Ext.get('topic-select');
    var s = s.findParent('div').parentNode.parentNode;
    var d = d.findParent('div').parentNode.parentNode;
    s.style.display = 'none';
	d.style.display = 'none';
  	if(type){
  	  if(type=='service') s.style.display = 'block'; 
  	  else if(type=='dataset') d.style.display = 'block';
  	}    
  }*/
  
  config.items=[this.theForm];
  AdvancedSearch.SearchForm.superclass.constructor.call(this,config);   
  
  this.addEvents("formcleared");
  
};    

Ext.extend(AdvancedSearch.SearchForm, Ext.Panel, {
        
    boundingBoxField: undefined,
    theForm: undefined
});

AdvancedSearch.SearchForm.date2iso  = function (d){
  	if(d.indexOf('.')>-1){
  		var s = d.split('.');
  		if(s[2]) d = s[2]+'-'+s[1]+'-'+s[0];
  		else d = s[1]+'-'+s[0];
  	}
  	return d;
};


