$PWT.Class.create
(
	{
		$namespace:	'SC.search',
		$name:		'Advanced'
	}
)
(
	{
		core:			null,
		fieldSet:		null,
		rowRecord:		null,
		store:			null,
		rowActions:		null,
		selModel:		null,
		grid:			null,
		field:
		{
			where:		null,
			contentType:null,
//			query:		null,
			vocals:		null,
			harmonies:	null
		},
		init:			function(core)
		{
			this.core=core;
			this.rowRecord=Ext.data.Record.create
			(
				[
					'criterion_id',
					'criterion_type',
					'criterion_operand',
					'criterion_value'
				]
			);
			this.store=new Ext.data.JsonStore
			(
				{
					root:		'data',
					idProperty:	'criterion_id',
					fields:
					[
						'criterion_id',
						'criterion_type',
						'criterion_operand',
						'criterion_value'
					],
					data:		{data:this.createInitialRecordSet()},
					autoLoad:	false
				}
			);
			this.selModel=new Ext.grid.CheckboxSelectionModel
			(
				{
					checkOnly:	true
				}
			);
			this.grid=new Ext.grid.EditorGridPanel
			(
				{
					store:			this.store,
					height:			134,
					width:			500,
					loadMask:		true,
					stripeRows:		true,
					clicksToEdit:	1,
					columns:
					[
						this.selModel,
						{
							dataIndex:	'criterion_type',
							header:		'Criteria Type',
							width:		100,
							sortable:	false
						},
						{
							dataIndex:	'criterion_operand',
							header:		'Match Type',
							width:		120,
							sortable:	false,
							editor:		new Ext.ux.form.SelectBox
							(
								{
									displayField:	'key',
									valueField:		'key',
									autoSelect:		true,
									value:			'Is similar to',
									tpl:			new Ext.XTemplate('<tpl for="."><div class="x-combo-list-item">{key}</div></tpl>'),
									store:			new Ext.data.ArrayStore
									(
										{
											fields:	['key','value'],
											data:
											[
												['Is similar to','contains'],
												['Is the same as','equals'],
												['Is not the same as','not equals'],
												['Does not contain','excludes']
											]
										}
									)
								}
							)
						},
						{
							dataIndex:	'criterion_value',
							header:		'Value (double click the empty area to add value)',
							width:		250,
							sortable:	false,
							editor:		new Ext.form.TextField()
						}
					],
					selModel:	this.selModel
				}
			);
			this.field.vocals=new Ext.form.CheckboxGroup
			(
				{
					fieldLabel:	'Has Vocal Type',
					columns:	2,
					width:		220,
					items:
					[
						{boxLabel: 'Male&nbsp;&nbsp;', name: 'version_vocals', inputValue: 'male'},
						{boxLabel: 'Female', name: 'version_vocals', inputValue: 'female'}
					]
				}
			);
//			this.field.query=new Ext.ux.form.SearchField
//			(
//				{
//					name:		'search_criterion',
//					width:		300,
//					fieldLabel:	'<b>Search Term</b>',
//					emptyText:	'Enter search term...',
//					onTrigger2Click: this.performSearch.bind(this)
//				}
//			);
			this.field.harmonies=new Ext.form.RadioGroup
			(
				{
					fieldLabel:	'Has Vocal Type',
					columns:	2,
					width:		220,
					items:
					[
						{boxLabel: 'Male&nbsp;&nbsp;', name: 'version_vocals[]', inputValue: 'male'},
						{boxLabel: 'Female', name: 'version_vocals[]', inputValue: 'female'}
					]
				}
			);
			this.field.where=new Ext.form.RadioGroup
			(
				{
					fieldLabel:	'I want to search&#8230;',
					items:
					[
						{boxLabel: 'all songs &#8230;', name: 'search_where', inputValue: 0,checked:true},
						{boxLabel: 'songs with <span class="showProf">P</span> only', name: 'search_where', inputValue: 2}
					]
				}
			);
			this.field.contentType=new Ext.form.RadioGroup
			(
				{
					fieldLabel:	'&#8230; that contain',
					columns:	2,
					items:
					[
						{boxLabel: 'Sound', name: 'search_contentType', inputValue: 'file_name'},
						{boxLabel: 'Lyrics', name: 'search_contentType', inputValue: 'file_lyrics'},
						{boxLabel: 'Sound or Lyrics', name: 'search_contentType', inputValue: 'either',checked:true},
						{boxLabel: 'Sound and Lyrics', name: 'search_contentType', inputValue: 'both'}
					]
				}
			);
			this.form=new Ext.form.FormPanel
			(
				{
					id:			'search-advanced',
					border:		false,
					width:		700,
//					height:		60,
					labelWidth:	120,
			        bodyStyle:	'padding:5px 5px 0',
					items:
					[
//						this.field.query,
						this.field.where,
						this.field.contentType,
						this.field.vocals,
						this.grid
						
					]
				}
			);
		},
		getForm: function()
		{
			return this.form;
		},
		createInitialRecordSet: function()
		{
			var	ret			=[],
				types		=
				[
					'Song Title',
					'Song Writer(s)',
					'Song Subject',
					'Song Description',
					'Song Contributor'
					
				];
			types.each
			(
				function(type)
				{
					ret.push
					(
						{
							criterion_type:		type,
							criterion_operand:	'Is the same as',
							criterion_value:	''
						}
					)
				}
			);
			return ret;
		},
		performSearch: function()
		{
			application.showMask('Collecting Search Results. Please wait...');
			application.API.search.main.advanced
			(
				this.getFormValues(),
				function(response)
				{
					application.hideMask();
					if (response.success)
					{
						this.core.addResultSet(response,response.params,name);
					}
					else
					{
						application.error(response.message || 'Sorry, no results were found.');
					}
				}.bind(this)
			);
		},
		getFormValues: function()
		{
			var values=this.form.getForm().getValues();
			values.search_query=[];
			this.grid.getSelectionModel().getSelections().each
			(
				function(record)
				{
					var	type	='',
						operand	='';
					switch (record.data.criterion_type)
					{
						case 'Song Title':			type='song_title';			break;
						case 'Song Writer(s)':		type='song_writer';			break;
						case 'Song Subject':		type='song_subject';		break;
						case 'Song Description':	type='song_description';	break;
						case 'Song Contributor':	type='v_contributors';		break;
					}
					switch (record.data.criterion_operand)
					{
						case 'Is the same as':		operand='equals';			break;
						case 'Is not the same as':	operand='not equals';		break;
						case 'Is similar to':		operand='contains';			break;
						case 'Does not contain':	operand='excludes';			break;
					}
					values.search_query.push(type+'::'+operand+'::'+record.data.criterion_value);
				}.bind(this)
			);
			if (Object.isDefined(values.version_vocals))
			{
				if (Object.isArray(values.version_vocals))
				{
					values.search_query.push('version_vocals::contains::male,female');
				}
				else
				{
					values.search_query.push('version_vocals::contains::'+values.version_vocals);
				}
			}
			console.debug(values);
			return values;
		},
		loadAndPerformSearch: function(name,values)
		{
			this.field.where		.setValue(values.search_where);
			this.field.contentType	.setValue(values.search_contentType);
			this.field.which		.setValue(values.search_which);
			this.field.query		.setValue(values.search_criterion);
			this.performSearch(name);
		}
	}
);
