$PWT.Class.create
(
	{
		$namespace:	'$DMSBT',
		$name:		'Search'
	}
)
(
	{
		container:			null,
		containerTop:		null,
		contentContainer:	null,
		resultsContainer:	null,
		pagination:			null,
		queryInput:			null,
		resultSet:			null,
		onebox:				null,
		init: function()
		{
			this.queryInput=$('#search-box-primary input[name=query]');
			$('#search-input').click(function() {
				if(this.lang == '') {
					this.lang = 1;
					this.value = '';
					$(this).css('font-style', 'normal');
				}
			});
			
			$('#search-go-button').click(this.onSearch.bind(this));
//			$('#search-input').bind("keydown", function(event) {
//			      var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
//			      if (keycode == 13) { 
//			    	  
//			      } else  {
//			         return true;
//			      }
//			 });
			$('#search-input').keyup(this.onSearch.bind(this));
			this.createContainer();
			this.bindEvents();
		},
		createContainer: function()
		{
			var id=$PWT.util.id();
			$(document.body).append
			(
				[
					'<div id="'+id+'" class="overlay-search" style="display:none;">',
						'<div class="inner">',
							'<div class="top">CLICK HERE TO HIDE</div>',
							'<div class="content">',
								'<div class="pageText">Result Pages:</div> <div id="'+id+'_pagination" class="pagination"></div>',
								'<div class="break"></div>',
								'<div id="'+id+'_searchResults" class="container">',
									'<table cellpadding="0" cellspacing="0" width="850">',
										'<tr class="header">',
											'<th>&nbsp;</th>',
										'</tr>',
									'</table>',
								'</div>',
							'</div>',
						'</div>',
					'</div>'
				].join('')
			);
			this.container			=$('#'+id);
			this.containerTop		=$('#'+id+' .inner .top');
			this.contentContainer	=$('#'+id+' .inner .content');
			this.resultsContainer	=$('#'+id+'_searchResults');
			this.pagination			=$('#'+id+'_pagination');
		},
		bindEvents: function()
		{
			this.containerTop.click(this.hideSearchContainer.bind(this)).mouseover
			(
				function()
				{
					this.containerTop.animate({backgroundColor:'#AAA',color:'#FFF'});
				}.bind(this)
			).mouseout
			(
				function()
				{
					this.containerTop.animate({backgroundColor:'#FFF',color:'#AAA'});
				}.bind(this)
			);
		},
		showSearchContainer: function()
		{
			application.showLightscreen();
			this.container.slideDown(1000);
			return this;
		},
		hideSearchContainer: function()
		{
			this.container.slideUp(1000);
			application.hideLightscreen();
			application.hideLoader();
			return this;
		},
		onSearch: function()
		{//$('#PWT-1').innerHeight()
			if (this.resultSet !== null)
			{
				this.resultSet.clearRows();
			}
			this.showSearchContainer();
			//application.showLoader('Collecting Search Results','Please wait a moment while your results are collected.');
			$.getJSON
			(
				'/search/'+this.queryInput.val(),
				function(response)
				{
					//console.debug(response);
					if (Object.isNull(this.resultSet))
					{
						//$DMSBT.search.Cache.seed($application.key);
						this.resultSet=new $DMSBT.search.ResultSet
						(
							Object.extend
							(
								response,
								{
									resultsContainer:		this.resultsContainer,
									paginationContainer:	this.pagination,
									template_row:
									[
										'<tr class="resultItem {CLASS}">',
											'<td>',
												'<table border="0" cellpadding="0" cellspacing="0" width="800">',
													'<tbody>',
														'<tr>',
															'<td class="description">',
																'<a href="{proxyURL}">',
																	'<span class="l" style="font-weight:bold;">{title}</span>',
																'</a>',
															'</td>',
														'</tr>',
														'<tr><td class="description">',
															'<p class="resultset">',
																'{description}<br />',
																'<a href="{proxyURL}">',
																	'<cite>{URL_display}</cite>',
																'</a>',
															'</p>',
														'</td></tr>',
													'</tbody>',
												'</table>',
											'</td>',
										'</tr>'
									].join(''),
									template_loading:
									[
										'<tr>',
											'<td colspan="7" style="text-align:center;"><img src="/sites/all/themes/dmsbt/images/loader1.gif" /><br />Fetching more results. Please wait...</td>',
										'</tr>'
									].join(''),
									ellipsisButtonAction: function(resultSet)
									{
										var id=$PWT.util.id();
										$(document.body).append('<div id="'+id+'" style="display:none;"><p style="font-weight:bold;">There are too many results. Please refine your search.</p></div>')
										jQuery('#'+id).dialog
										(
											{
												width:	400,
												height:	250,
												modal:	true,
												buttons:
												{
													Cancel: function()
													{
														jQuery('#'+id).dialog('close');
														$(id).remove();
													},
													'Next Page': function()
													{
														jQuery('#'+id).dialog('close');
														$(id).remove();
														resultSet.showNextPageSet();
													},
													Refine: function()
													{
														this.queryInput.focus().effect('highlight',500);
													}.bind(this)
												}
											}
										);
									}.bind(this)
								}
							),
							{
								onModifyPageResults: function($this,resultSet,HTML)
								{
									for (var i=0,j=resultSet.length,k=true; i<j; i++,k=!k)
									{
										resultSet[i].CLASS			='';
										resultSet[i].URL_display	=decodeURIComponent(resultSet[i].URL)
																	.replace(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b\//,'');
										if (Object.isEmpty(resultSet[i].proxyURL))resultSet[i].proxyURL=resultSet[i].URL;
										HTML.push(this.applyTemplate(this.template.row,resultSet[i]));
									}
								}
							}
						).bind(this);
					}
					else
					{
						//TODO - update state of result set with new results.
					}
					application.hideLoader();
				}.bind(this)
			);
		}
	}
);

