$PWT.Class.create
(
	{
		$namespace:	'SC.search',
		$name:		'Core',
		$traits:	[$PWT.trait.Configurable,$PWT.trait.Observable]
	}
)
(
	{
		config:
		{
			form:			false,
			type:			'quick',
			submitType:		'ajax'
		},
		events:
		{
		
		},
		search:
		{
			simple:			null,
			advanced:		null
		},
		version:			'2.0.0',
		form:				false,
		menu:				{},
		addCriteria:		false,
		submitCriteria:		false,
		rowCount:			0,
		activeRows:			0,
//		insertedRows:		0,
		usedCriteria:		[],
		songWriterWindow:	false,
		criterionOptions:	null,
		criterionOptionsForAdvanced:
		[
			{key:'Select a criteria&#8230;',	value:''},
			{key:'Song Title',				value:'song_title'},
			{key:'Song Subject',			value:'song_subject'},
			{key:'Song Description',		value:'song_description'},
			{key:'Song Writer',				value:'song_writer'},
			{key:'Song Contributor',		value:'v_contributors'},
			{key:'Song Genre',				value:'genre_id'},
			{key:'Song Signature',			value:'version_signature'},
			{key:'Song Harmonies',			value:'version_isharmony'},
			{key:'Song Vocals',				value:'version_vocals'}
		],
		criterionOptionsForWatch:
		[
			{key:'Select a criteria&#8230;',	value:''},
			{key:'Song Writer',				value:'song_writer'},
			{key:'Song Genre',				value:'genre_id'},
			{key:'Song Vocals',				value:'version_vocals'}
		],
		genreOptions:
		[
			{key:' Select a genre',					value:''},
			{key:'Classical',						value:1},
			{key:'Country',							value:2},
			{key:'Pop',								value:3},
			{key:'Folk',							value:4},
			{key:'Gospel',							value:5},
			{key:'Metal',							value:6},
			{key:'Rock',							value:7},
			{key:'Jazz',							value:8},
			{key:'Jingle',							value:9},
			{key:'World',							value:10},
			{key:'Urban',							value:11},
			{key:'Alternative',						value:12},
			{key:'Children',						value:13},
			{key:'Blues',							value:14},
			{key:'Big Band',						value:15},
			{key:'Library Music',					value:16},
			{key:'Vocal',							value:17},
			{key:'Cabaret',							value:18},
			{key:'Dance',							value:19},
			{key:'*Unclassified',					value:150}
		],
		subGenreOptions:
		[
			[
				1,
				[
					{key:'Symphony',				value:21},
					{key:'Modern',					value:22},
					{key:'Symphonic Rock',			value:23},
					{key:'Chamber Music',			value:24},
					{key:'Opera',					value:25},
					{key:'Alternate',				value:26},
					{key:'Folk',					value:27},
					{key:'Traditional',				value:28},
					{key:'Choral',					value:29},
					{key:'Mediaeval',				value:30}
				]
			],
			[
				2,
				[
					{key:'Traditional',				value:31},
					{key:'Modern',					value:32},
					{key:'Rock',					value:33},
					{key:'Pop',						value:145},
					{key:'Blues',					value:34},
					{key:'Bluegrass',				value:35},
					{key:'Bush',					value:36},
					{key:'Ballad',					value:37},
					{key:'Acapella',				value:38},
					{key:'Comedy',					value:39},
					{key:'Cross Over',				value:40},
					{key:'Rockabilly',				value:41},
					{key:'Cajun',					value:42},
					{key:'Spiritual',				value:151},
					{key:'Instrumental',			value:160}
				]
			],
			[
				3,
				[
					{key:'Rock',					value:43},
					{key:'Ballad',					value:44},
					{key:'Acapella',				value:45},
					{key:'Power Ballad',			value:46},
					{key:'Comedy',					value:47},
					{key:'Disco',					value:48},
					{key:'Opera',					value:49},
					{key:'Mediaeval',				value:50},
					{key:'Spiritual',				value:152},
					{key:'Instrumental',			value:161}
				]
			],
			[
				4,
				[
					{key:'Traditional',				value:51},
					{key:'Mediaeval',				value:52},
					{key:'Modern',					value:53},
					{key:'Acapella',				value:54},
					{key:'Comedy',					value:55},
					{key:'Operatic',				value:56},
					{key:'Alt-folk',				value:146},
					{key:'Spiritual',				value:153},
					{key:'Instrumental',			value:162}
				]
			],
			[
				5,
				[
					{key:'Pop',						value:57},
					{key:'Christmas',				value:58},
					{key:'Hymn Style',				value:59},
					{key:'Rock',					value:60},
					{key:'Acapella',				value:61},
					{key:'Blues',					value:62},
					{key:'Spiritual',				value:154}
				]
			],
			[
				6,
				[
					{key:'Heavy Metal',				value:63},
					{key:'Thrash',					value:64},
					{key:'Hard Rock',				value:65},
					{key:'Goth Rock',				value:66},
					{key:'Progressive Metal',		value:67},
					{key:'Instrumental',			value:163}
				]
			],
			[
				7,
				[
					{key:'Alternate Rock',			value:68},
					{key:'Rock n Roll',				value:69},
					{key:'Rhythm & Blues',			value:70},
					{key:'Soft Rock',				value:71},
					{key:'Pop',						value:72},
					{key:'Acapella',				value:73},
					{key:'Rock Ballad',				value:74},
					{key:'Power Ballad',			value:75},
					{key:'Spiritual',				value:155},
					{key:'Instrumental',			value:164}
				]
			],
			[
				8,
				[
					{key:'Funk',					value:76},
					{key:'Fusion',					value:77},
					{key:'Traditional',				value:78},
					{key:'Acapella',				value:79},
					{key:'Blues',					value:144},
					{key:'Instrumental',			value:165}
				]
			],
			[
				9,
				[
					{key:'Ringtone',				value:80},
					{key:'Catchy',					value:81},
					{key:'Evocative',				value:82},
					{key:'Cute',					value:83},
					{key:'Busy',					value:84},
					{key:'Emotive',					value:85},
					{key:'Zany',					value:86},
					{key:'Funky',					value:87}
				]
			],
			[
				10,
				[
					{key:'Indigenous Aust.',		value:88},
					{key:'Pacific Island',			value:89},
					{key:'Latino',					value:90},
					{key:'Salsa',					value:91},
					{key:'Reggae',					value:92},
					{key:'Cajun',					value:93},
					{key:'Carribean',				value:94},
					{key:'Mex style',				value:95},
					{key:'Sth American',			value:96},
					{key:'Indian',					value:97},
					{key:'Spiritual',				value:156},
					{key:'Instrumental',			value:166}
				]
			],
			[
				11,
				[
					{key:'Hip Hop',					value:98},
					{key:'Funk',					value:99},
					{key:'Dance',					value:100},
					{key:'Electronica',				value:101},
					{key:'RAP',						value:102},
					{key:'Soul',					value:103},
					{key:'Mo Town',					value:104},
					{key:'Crunk',					value:105},
					{key:'Acapella',				value:106},
					{key:'Instrumental',			value:167}
				]
			],
			[
				12,
				[
					{key:'Pop',						value:107},
					{key:'Acoustic',				value:108},
					{key:'Punk',					value:109},
					{key:'Indie',					value:110},
					{key:'Grunge',					value:111},
					{key:'Emo',						value:112},
					{key:'SKA',						value:113},
					{key:'Hardcore',				value:114},
					{key:'Instrumental',			value:168}
				]
			],
			[
				13,
				[
					{key:'Comedy',					value:115},
					{key:'Teen Age',				value:116},
					{key:'Very Young',				value:117},
					{key:'Christmas',				value:118},
					{key:'Novelty',					value:119},
					{key:'Educational',				value:120},
					{key:'Spiritual',				value:157},
					{key:'Instrumental',			value:169}
				]
			],
			[
				14,
				[
					{key:'Delta Blues',				value:121},
					{key:'Chicago Blues',			value:122},
					{key:'Rock',					value:123},
					{key:'Spiritual',				value:158},
					{key:'Instrumental',			value:170}
				]
			],
			[
				15,
				[
					{key:'Swing',					value:124},
					{key:'Dance Hall',				value:125},
					{key:'Crooner',					value:126},
					{key:'Instrumental',			value:171}
				]
			],
			[
				16,
				[
					{key:'Sound Clip',				value:127},
					{key:'Sound Bed',				value:128},
					{key:'Background',				value:129}
				]
			],
			[
				17,
				[
					{key:'Crooner',					value:130},
					{key:'Inspirational',			value:131},
					{key:'Acapella',				value:132},
					{key:'Spiritual',				value:159}
				]
			],
			[
				18,
				[
					{key:'Jazz',					value:133},
					{key:'Crooner',					value:134},
					{key:'Swing',					value:135},
					{key:'Dance',					value:136},
					{key:'Opera',					value:137},
					{key:'Instrumental',			value:172}
				]
			],
			[
				19,
				[
					{key:'RAP',						value:138},
					{key:'Hip Hop',					value:139},
					{key:'Funk',					value:140},
					{key:'Techno',					value:141},
					{key:'Electronica',				value:142},
					{key:'Garage',					value:143},
					{key:'Instrumental',			value:173}
				]
			]
		],
		template:
		{
			row:
			[
				'<tr class="row">',
					'<th class="col_1" scope="row"><div align="right">&#8230;and where<input type="hidden" id="#{id}" name="search_query[]" /></div></th>',
					'<td class="col_2"><div class="field">#{col2}&nbsp;</div><div class="value" style="display:none;"></div></td>',
					'<td class="col_3"><div class="field">#{col3}&nbsp;</div><div class="value" style="display:none;"></div></td>',
					'<td class="col_4"><div class="field">#{col4}&nbsp;</div><div class="value" style="display:none;"></div></td>',
					'<td class="col_5"><div class="field">#{col5}&nbsp;</div><div class="value" style="display:none;"></div></td>',
				'</tr>'
			].join(''),
			option:		new Ext.Template('<option value="{value}"{selected}>{key}</option>'),
			select:		new Ext.Template('<select id="{id}" name="{name}">{options}</select>'),
			check:		new Ext.Template('<br /><label><input type="checkbox" id="{id}" name="{name}" value="{value}" />{key}</label>')
		},
		searchResultSetNum:	0,
		window:				null,
		tabPanel:			null,
		mySearches:
		{
			store:			null,
			grid:			null,
			rowActions:		null
		},
		init:				function()
		{
			this.search.simple		=new SC.search.Simple(this);
			this.search.advanced	=new SC.search.Advanced(this);
			this.mySearches.store	=new Ext.data.DirectStore
			(
				{
					root:		'data',
					idProperty:	'search_id',
					fields:
					[
						'search_id',
						'search_cust_id',
						'search_type',
						'search_name',
						'search_data',
						'search_temp',
						'search_created',
						'search_status'
					],
					directFn:	application.API.search.main.listAll,
					baseParams:	{start:0,limit:100},
					autoLoad:	false,
					listeners:
					{
						load:	function(data)
						{
							for (var i=0; i<data.data.length; i++)
							{
								this.menu.mySearches.add
								(
									{
										id:			'mySearches_'+data.data.items[i].data.search_id,
										text:		data.data.items[i].data.search_name,
										handler:	this.handleMenuItemClick.bind(this)
									}
								);
							}
						}.bind(this)
					}
				}
			);
//			this.mySearches.rowActions=new Ext.ux.grid.RowActions
//			(
//				{
//					header:			'Actions',
//					actions:
//					[
//						{
//							iconCls:	'icon-bullet_go',
//							tooltip:	'Run Search',
//							handler:	function(record)
//							{
//								this.loadAndPerformSearch(record.data.search_name,record.data.search_data);
//							}.bind(this)
//						}
//					]
//				}
//			);
//			this.mySearches.grid=new Ext.grid.GridPanel
//			(
//				{
//					store:		this.mySearches.store,
//					height:		300,
//					loadMask:	true,
//					stripeRows:	true,
//					columns:
//					[
//						{
//							dataIndex:	'search_name',
//							header:		'Name',
//							width:		60,
//							sortable:	true
//						},
//						{
//							dataIndex:	'search_created',
//							header:		'Date Saved',
//							width:		80,
//							sortable:	true
//						},
//						this.mySearches.rowActions
//					],
//					plugins:	[this.mySearches.rowActions],
//					viewConfig:
//					{
//						forceFit:	true
//					}
//				}
//			);
			this.tabPanel=new Ext.TabPanel
			(
				{
					height:			280,
					enableTabScroll:true
				}
			);
			this.menu.newSearch=new Ext.menu.Menu
			(
				{
					floating:	false,
					items:
					[
						{
							text:		'Simple',
							style:		'font-weight:bold;background-color:#c0c0c0;',
							handler:	this.handleMenuItemClick.bind(this)
						},
						{
							text:		'Advanced',
							handler:	this.handleMenuItemClick.bind(this)
						}
					]
				}
			);
			this.menu.mySearches=new Ext.menu.Menu
			(
				{
//					height:	300,
					floating:	false
				}
			);
			this.searchResultsPanel=new Ext.Panel
			(
				{
					region:	'south',
					height:	300,
					hidden:	true,
					items:	this.tabPanel
				}
			);
			this.cardLayout=new Ext.Panel
			(
				{
					region:		'center',
					layout:		'card',
					activeItem:	0,
					items:
					[
						this.search.simple.getForm(),
						this.search.advanced.getForm()
					]
				}
			);
			this.layout=new Ext.Panel
			(
				{
					layout:	'border',
					width:	715,
					height:	400,
					border:	false,
					items:
					[
						{
							region:	'west',
							width:	180,
							border:	false,
							items:
							[
								{
									title:		'Search Modes',
									autoScroll:	true,
									items:		this.menu.newSearch
								},
								{
									title:		'My Searches',
									height:		262,
									autoScroll:	true,
									items:		this.menu.mySearches
								}
							]
						},
						this.cardLayout,
						this.searchResultsPanel
					]
				}
			);
			this.window=new Ext.Window
			(
				{
					title:		'Song Central Search',
					width:		715,
					height:		400,
					closeAction:'hide',
					resizable:	false,
					items:
					[
						this.layout
					],
					bbar:
					[
						{
							text:	'Close',
							iconCls:'icon-close',
							handler:function()
							{
								this.window.close();
							}.bind(this)
						},
						'->',
						{
							text:	'Perform Search',
							iconCls:'icon-magnifier',
							handler:function()
							{
								if (this.cardLayout.getLayout().activeItem.id=='search-simple')
								{
									this.search.simple.performSearch();
								}
								else
								{
									this.search.advanced.performSearch();
								}
							}.bind(this)
						}
					]
				}
			);
		},
		show: function()
		{
			this.mySearches.store.load();
			this.window.show();
		},
		hide: function()
		{
			this.window.hide();
		},
		performSearch:			function(){},
		makeForm:				function(){},
		getFormValues:			function(){},
		addResultSet: function(resultSet,params,name)
		{
			if (!Object.isString(name))name='Search Results #'+(++this.searchResultSetNum);
			var rowExpander	=new Ext.ux.grid.RowExpander({tpl:new Ext.Template('<b>Description</b><br />{song_description}<br /><br /><b>Lyrics</b><br /><p>{version_lyrics}</p>')}),
				rowActions	=new Ext.ux.grid.RowActions
				(
					{
						header:			'Actions',
						actions:
						[
							{
								iconCls:	'icon-music',
								tooltip:	'Play in Audio Center',
								handler:	function(record)
								{
									//TODO: Play in Audio Center.
									application.message('Feature Returning Soon.');
								}.bind(this)
							}
						]
					}
				),
				store=new Ext.data.JsonStore
				(
					{
						root:		'data',
						idProperty:	'version_id',
						fields:
						[
							'version_id',
							'version_length',
							'version_signature',
							'version_isharmony',
							'version_vocals',
							'version_genres',
							'version_lyrics',
							'song_title',
							'song_description',
							'v_contributors',
							'showcase_num',
							'showcase_type',
							'id3'
						],
						data:		resultSet
					}
				),
				grid=new Ext.grid.GridPanel
				(
					{
						store:		store,
						closable:	true,
						width:		700,
						height:		214,
						autoScroll:	true,
						loadMask:	true,
						stripeRows:	true,
						columns:
						[
							rowExpander,
							{
								dataIndex:	'song_title',
								header:		'Title',
								width:		100,
								sortable:	true
							},
							{
								dataIndex:	'version_length',
								header:		'Length',
								width:		60,
								sortable:	true
							},
							{
								dataIndex:	'version_signature',
								header:		'Signature',
								width:		80,
								sortable:	true
							},
							{
								dataIndex:	'version_vocals',
								header:		'Vocals',
								width:		80,
								sortable:	true
							},
							{
								dataIndex:	'version_genres',
								header:		'Genres',
								width:		100,
								sortable:	true
							},
							{
								dataIndex:	'v_contributors',
								header:		'Contributors',
								width:		80,
								sortable:	true
							},
							{
								dataIndex:	'showcase_num',
								header:		'Showcase #',
								width:		80,
								sortable:	true
							},
							{
								dataIndex:	'showcase_type',
								header:		'Showcase Type',
								width:		80,
								sortable:	true
							}
//							rowActions
						],
						plugins:	[rowExpander],//,rowActions],
						viewConfig:
						{
							forceFit:	true
						},
						tbar:
						[
							{
								text:		'Save This Search',
								iconCls:	'icon-save',
								handler:	this.saveSearch.bind(this,params)
							},
							{
								text:		'Play in Audio Centre',
								iconCls:	'icon-music',
								handler:	this.loadInAudioCenter.bind(this,resultSet)
							}
						]
//						selModel:new Ext.grid.RowSelectionModel
//						(
//							{
//								singleSelect:	true,
//								listeners:
//								{
//									rowselect:		this.onRowSelect.bind(this),
//									rowdeselect:	this.onRowDeselect.bind(this)
//								}
//							}
//						),
//						listeners:
//						{
//							render: function()
//							{
//	//							(function(){this.onRender();}.bind(this)).defer(1000);
//							}.bind(this)
//						}
					}
				),
				container=new Ext.Panel
				(
					{
						title:		name,
						border:		false,
						items:		grid
					}
				);
			this.searchResultsPanel.show();
			this.layout.setHeight(640);
			this.window.setHeight(640).center();
			this.tabPanel.add(container);
			this.tabPanel.activate(container);
		},
		saveSearch: function(params)
		{
			Ext.Msg.prompt
			(
				'Name Search',
				'Please enter a name to save your search as.',
				function(button,name)
				{
					if (button=='ok')
					{
						application.showMask('Saving Search. Please wait...');
						application.API.search.main.save
						(
							{
								search_name:name || 'Unnamed',
								search_data:params
							},
							function(response)
							{
								application.hideMask();
								if (response.success)
								{
									application.message('Search Saved.');
									this.mySearches.store.load();
								}
								else
								{
									application.error('Sorry, we are unable to save your search right now. Please try again later.');
								}
							}.bind(this)
						);
					}
					else
					{
						application.message('Search was not saved.');
					}
				}.bind(this)
			);
		},
		loadInAudioCenter: function(resultSet)
		{
			var onInit=function()
			{
				application.audiocenter.createSearchResultGrid(resultSet);
			}.bind(this)
			if (!application.audioCenterIsOpen())
			{
				application.observeOnce('playerInitiated',onInit);
				application.launchAudioCenterWindow();
			}
			else
			{
				onInit();
			}
			application.launchAudioCenterWindow();
		},
		handleMenuItemClick: function(menuItem)
		{
			this.menu.newSearch.items.each
			(
				function(item)
				{
					item.el.setStyle
					(
						{
							'font-weight':		'inherit',
							'background-color':	'inherit'
						}
					);
				}
			);
			this.menu.mySearches.items.each
			(
				function(item)
				{
					item.el.setStyle
					(
						{
							'font-weight':		'inherit',
							'background-color':	'inherit'
						}
					);
				}
			);
			menuItem.el.setStyle
			(
				{
					'font-weight':		'bold',
					'background-color':	'#c0c0c0'
				}
			);
			switch (menuItem.text)
			{
				case 'Simple':
				{
					this.cardLayout.getLayout().setActiveItem('search-simple');
					break;
				}
				case 'Advanced':
				{
					this.cardLayout.getLayout().setActiveItem('search-advanced');
					break;
				}
				//Handle all the "My Searches" menu items.
				default:
				{
					var record=this.mySearches.store.getById(menuItem.id.split('_')[1]);
					this.loadAndPerformSearch(record.data.search_type,record.data.search_name,record.data.search_data);
				}
			}
		},
		loadAndPerformSearch: function(type,name,values)
		{
			this.search[type].loadAndPerformSearch(name,values);
		}
	}
);
