
searchAreaActive = false;

Ext.onReady(function(){
		
		if(Ext.get("primary-header-area")) {		
			initQuickSearch();
		}

});


function quickSearchInit() {
	//historic stub		
}
	
function jump(menu) {
	ref=menu.choice.options[menu.choice.selectedIndex].value;
	if (ref != "") {
		window.open(ref);
	}
}
    


function initQuickSearch() {
	
	
		otherSitesLink = Ext.select("a.other-sites-nav","#header-top-nav-area");
		otherSites = Ext.select("ul.other-sites-item-area","#header-top-nav-area");

		otherSitesLink.on('mouseover',function(){
											otherSites.replaceClass("hidden","displayed");
											if(typeof t != 'undefined') {clearTimeout(t)}
									},this);
		otherSitesLink.on('mouseout',function(){t = setTimeout('otherSites.replaceClass("displayed","hidden")', 50)},this);

		otherSites.on('mouseover',function(){
												otherSites.replaceClass("hidden","displayed");
												if(typeof t != 'undefined') {clearTimeout(t)}
										},this);
		otherSites.on('mouseout',function(){t = setTimeout('otherSites.replaceClass("displayed","hidden")', 50)},this);
			

		Product = Ext.data.Record.create([
		   {name: 'isbn'},
		   {name: 'shorttitle'},
		   {name: 'longtitle'}, 
		   {name: 'author'},
		   {name: 'rrp'},
		   {name: 'pubdate'},
		   {name: 'format'}
		]);
		
		Contributor = Ext.data.Record.create([
		   {name: 'keyname'},
		   {name: 'firstname'},
		   {name: 'contribution'}, 
		   {name: 'contributorID'},
		   {name: 'webaudience'}
		]);
		
		dsProducts = new Ext.data.Store({				
			url: '/Common/services/quick_search_xml.cfm',				
			reader: new Ext.data.XmlReader({
				record: 'product'
			},Product)
		});
		
		dsContributors = new Ext.data.Store({				
			url: '/Common/services/quick_search_xml.cfm',				
			reader: new Ext.data.XmlReader({
				record: 'contributor'
			},Contributor)
		});
		
		dsSuggestions = new Ext.data.Store({				
			url: '/Common/services/searchspelling.cfm',				
			reader: new Ext.data.XmlReader({
			   record: 'suggestion'
			}, [{name: 'query'}])
		});
	
		
		var productResultTpl = new Ext.XTemplate(
			'<tpl for=".">',
			'<div class="QuickRes quickres-products">',
			 '<a href="/search/title.cfm?home=TLL&ISBN={isbn}" id="{isbn}">{shorttitle}</a> <br> <span class="details">{author} <span class="format"></span></span>',
			'</div></tpl>'
		);			
//		var contributorResultTpl = new Ext.XTemplate(
//			'<tpl for=".">',
//			'<div class="QuickRes quickres-contributors">',
//			 '<a href="/lookinside/author-profile.cfm?AuthId={contributorID}" id="{contributorID}">{firstname} {keyname}</a> <span class="details">{contribution} </span>',
//			'</div></tpl>'
//		);
	
		var panel = new Ext.Panel({
			applyTo: 'results-products',
			height: 'auto',
			autoScroll:true,		
			items: new Ext.DataView({
					tpl: productResultTpl,
					store: dsProducts,
					overClass:'results-item-over',
					itemSelector: 'div.quickres-products'
				})
		});
		
		var panel = new Ext.Panel({
			applyTo: 'results-contributors',
			height: 'auto',
			autoScroll:true,		
			items: new Ext.DataView({
					tpl: contributorResultTpl,
					store: dsContributors,
					overClass:'results-item-over',
					itemSelector: 'div.quickres-contributors'
			})
		});
	
	
	
		quickSearchBox = Ext.get("search-box");
		quickSearchBox.dom.setAttribute("autocomplete","off");
		quickSearchBox.focus();
		
		var keyFunc = function(ev) { 
			onValChange(ev); 
		}
		
		
		quickSearchBox.on('keydown', keyFunc, this, {buffer: 300});
		
		var lastSearchVal = '';

		function onValChange(e){

			var searchVal = quickSearchBox.getValue();		
	
			if(lastSearchVal != searchVal && searchVal.length > 2){			
				lastSearchVal = searchVal;
				dsProducts.load({params:{q:searchVal}, callback:displayProducts});
				dsContributors.load({params:{q:searchVal}, callback:displayContributors});
			}		   
		}

		searchResultsArea = Ext.get('quicksearch-results-area');
		
		results_not_found = Ext.get('results-notfound').setVisibilityMode(Ext.Element.DISPLAY);
		results_products_header = Ext.get('results-products-header').setVisibilityMode(Ext.Element.DISPLAY);
		results_contributors_header = Ext.get('results-contributors-header').setVisibilityMode(Ext.Element.DISPLAY);
	
}



function initSearchResultsArea() {
	
	searchResultsArea.on('mouseover',function(){
									searchResultsArea.show();
									if(typeof srTim != 'undefined') {clearTimeout(srTim);}
							},this);

	searchResultsArea.on('mouseout',function(){srTim = setTimeout("searchResultsArea.hide()", 50)},this);
	
	quickSearchBox.on('mouseover',function(){
									searchResultsArea.show();
									if(typeof srTim != 'undefined') {clearTimeout(srTim)}
							},this);
	quickSearchBox.on('mouseout',function(){srTim = setTimeout("searchResultsArea.hide()", 50)},this);
	
	searchAreaActive = true;

}


function displayProducts(recset,options,success) { 
	
	if(!searchAreaActive) {
		initSearchResultsArea();
	}
	
	searchResultsArea.show();
	
	if(recset.length > 0) {				
		results_not_found.hide();
		results_products_header.show();
		results_contributors_header.show();
	} else {
		dsSuggestions.load({params:{q:quickSearchBox.getValue()}, callback:loadSuggestions});
		results_products_header.hide();
		results_contributors_header.hide();				
	}

}



function doSuggestionClick() {
	
	suggestionText = Ext.get('search-suggestion-link').dom.innerHTML;
	Ext.getDom(quickSearchBox).value = suggestionText;
	onValChange();
	
	return false;
	
}


//function loadSuggestions(recset,options,success) {

//	resText = Ext.get('did-you-mean');
//	resText.dom.innerHTML = "";	
	
//	if(recset.length > 0 && dsContributors.getCount() <1 ) {	
//		suggestion = recset[0].get('query');
//		newLink = resText.insertHtml("beforeEnd","Did you mean: <a href='#' id='search-suggestion-link'>" + suggestion + '</a>',true);	
//		newLink.on('click',doSuggestionClick);
//	} 
	
//	results_not_found.show();			
	
//}


//function displayContributors(recset,options,success) { 
	
//	if(recset.length > 0) {	
//		results_not_found.hide();
//		results_contributors_header.show();
//	} else {
//		if(dsProducts.getCount() <1 ) {
//			results_not_found.show();
//		}
		
//		results_contributors_header.hide();
//	}
//}	