Ext.namespace('SilverMapper');

SilverMapper.Filter = function(config) {

	Ext.apply(this, config, {
			title: Lang.Get('Core','general','filter_title')
		,	labelWidth: 100
		,	url:'api/silvermapper.php'
		,	autoScroll: true
		,	bodyStyle:'padding:5px 5px 0'
		,	id: 'sm-filter'
		,	border: false
		,	maxSize: 250
		,	minSize: 250
		,	defaults: {width: 155}
		, hideMode: 'offsets'
		,	bbar: [{
					text: Lang.Get('Core','general','filter_reset')
				, iconCls: 'x-tbar-loading'
				,	scope: this
				, handler: function() {
						this.getForm().reset();
						Ext.getCmp('sm-map').getMarkers( 1 ); 
					}
			}]
		,	items: [{			
					xtype: 'xtwincombo'
				, fieldLabel: 'Collection Types'
	      , store: new Ext.data.SimpleStore({
		        	fields: [ 'collection_types' ]
	        	,	data: collection_types
    			})
        ,	displayField: 'collection_types'
        ,	typeAhead: true
        ,	mode: 'local'
        ,	forceSelection: true
        ,	triggerAction: 'all'
        ,	selectOnFocus: true
				,	name: 'kind'
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
	    },	{
					xtype: 'xtwincombo'
				, fieldLabel: 'Country'
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "location_country_name" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
        ,	displayField: 'name'
        ,	typeAhead: true
//        ,	mode: 'local'
        ,	forceSelection: true
        ,	triggerAction: 'all'
        ,	selectOnFocus: true
				,	name: 'location_country_name'
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
	    },	{
					xtype: 'xtwincombo'
				, fieldLabel: 'Collection Size'
	      , store: new Ext.data.SimpleStore({
		        	fields: [ 'collection_sizes', 'collection_size' ]
	        	,	data: collection_sizes
    			})
        ,	displayField: 'collection_sizes'
				,	valueField: 'collection_size'
				,	hiddenName: 'collection_size'
        ,	typeAhead: true
        ,	mode: 'local'
        ,	forceSelection: true
        ,	triggerAction: 'all'
        ,	selectOnFocus: true
				,	name: 'collection_size'
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}

/*					
	    },	{
				
					xtype: 'xtwincombo'
				, fieldLabel: 'Name'
				, name: 'name'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "name" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
			}, {
					xtype: 'xtwincombo'
				, fieldLabel: 'Code'
				, name: 'code'
				,	queryParam: 'filter'		
				,	store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy( { url: 'api/silvermapper.php' } )
						,	baseParams: { task: "list", type: "code" }
						, reader: new Ext.data.JsonReader({root: 'nodes',	id: 'name'}, [ {name: 'name', mapping: 'name'} ])
					})
				,	displayField: 'name'
				,	typeAhead: false
				,	hideTrigger2: true
				,	minChars: 2
				,	value:''
				, listeners: {
							'select': this.getMarkers2
						,	'clear': this.getMarkers
					}
*/					
			}, {				
					xtype: 'component'
				, autoEl: { 
							tag: 'div'
						, cls: 'form-note'
						, html: 'Note: Not having any filter set will show all collections.'
					}
			}]		
		,	listeners: {
				'show': function() {
					if ( this.fix ) {
						this.fix = false;
					}
				}
			}
	});

	SilverMapper.Filter.superclass.constructor.apply(this, arguments);

};

Ext.extend( SilverMapper.Filter, Ext.FormPanel, {

		getMarkers: function() {
			Ext.getCmp('sm-map').getMarkers( 1 ); 
		}

	,	getMarkers2: function( f_item ) {
		
		var form = Ext.getCmp('sm-filter').getForm();

		switch( f_item.name ) {
			case 'Invasive_Species':
				form.findField('Noxious_Weeds').reset();
				form.findField('Family').reset();
				form.findField('Genus').reset();
				form.findField('SpeciesEpithet').reset();
				form.findField('TrinomialEpithet').reset();
				form.findField('CommonNames').reset();
				break;

			case 'Noxious_Weeds':
				form.findField('Invasive_Species').reset();
				form.findField('Family').reset();
				form.findField('Genus').reset();
				form.findField('SpeciesEpithet').reset();
				form.findField('TrinomialEpithet').reset();
				form.findField('CommonNames').reset();
				break;

			case 'Family':
			case 'Genus':
			case 'SpecificEpithet':
			case 'SpeciesEpithet':
			case 'TrinomialEpithet':
			case 'CommonNames':
				form.findField('Noxious_Weeds').reset();
				form.findField('Invasive_Species').reset();
				break;
				
		}
		
		Ext.getCmp('sm-map').getMarkers( 1 ); 
	}

});

Ext.reg( 'sm-filter', SilverMapper.Filter ); 

