
function registerFindFastEvents(e) {

	if($('searchField') && $('searchForm')) {
		var searchField = $('searchField');
		var searchForm = $('searchForm');
		searchForm.onsubmit = function() { return false; };
		if (searchForm.captureEvents) searchForm.captureEvents(Event.SUBMIT);

		searchField.setAttribute('autocomplete','off');
		searchField.onkeydown = startSearch;
		if (searchField.captureEvents) searchField.captureEvents(Event.KEYDOWN);
		
		searchField.onfocus = doFocus;
		if (searchField.captureEvents) searchField.captureEvents(Event.FOCUS);

		searchField.onblur = doBlurDelay;
		if (searchField.captureEvents) searchField.captureEvents(Event.BLUR);
	
	}

}

var lastSearch = "";
var lastSearchLength = 0;

function doFocus(e) { 

	var searchField = $('searchField');
	searchField.value = lastSearch; 
	var sResults = $('searchResults');
	
	if(lastSearch != "") {
		//new Effect.Appear(sResults, {duration: 0.3});
		new Effect.BlindDown(sResults, {duration: 0.3, queue: {position: 'start', scope: 'searcha'}});	
	}
}

function doBlur() { 

	var searchField = $('searchField');
	
	if(lastSearch == "") {
		searchField.value = ''; 
	} else {
		var sResults = $('searchResults');
		//new Effect.Fade(sResults, {duration: 0.3});
		new Effect.BlindUp(sResults, {duration: 0.3, queue: {position: 'start', scope: 'searcha'}});
		searchField.value = lastSearch;
	}

	
}

function doBlurDelay(e) {
	if (timeOut)
	{
		window.clearTimeout(timeOut);
	}
	
	timeOut = window.setTimeout("doBlur()", 500);
}

var searched_products = new Array();

var sT = "";
var timeOut = 0;

function startSearch(e) {

	if (timeOut)
	{
		window.clearTimeout(timeOut);
	}
	
	timeOut = window.setTimeout("doSearch()", 750);
}


function doSearch() {

	var searchText = $F('searchField');	
	var searchString = searchText; 
	
	

	var sResults = $('searchResults');
	sT = searchString;
	lastSearch = searchText;

	if (searchString.length >= 3)
	{
		var url = '/search/' + escape(searchString.replace("#","%23").replace("&","%26") + '');
		
		new Ajax.Request(url, {onSuccess: updateSearch, onFailure: errFunc, on404: errFunc});

	} else if(Element.visible(sResults)) {
		new Effect.Fade(sResults, {duration: 0.3});
		new Effect.BlindUp(sResults, {duration: 0.3, afterFinish: function() { clearElement(sResultsData); }, queue: {position: 'start', scope: 'searcha'}});
	}
	
}


function updateSearch(t) {

	//alert(t.responseText);
	var queue = Effect.Queues.get('searcha');
	
	queue.each(function(e) { e.cancel() });

	eval(t.responseText);

		var sResults = $('searchResults');

		var sResultsData = $('bor');

		var sIble = Element.visible(sResults);
		
		
		
		if(products.length > 0) {
			if(sIble) {
				
				new Effect.BlindUp(sResults,{ 
					duration: 0.3, scaleMode: 'box', 
					afterFinish: function() { 
						clearElementAll(sResultsData); 
						var b = 1;
						
						products.each(function(product) {
							shortname = product['name'].substring(0, 51);
							if(shortname.length >= 51) { shortname = shortname + '...'; }

							new Insertion.Top(sResultsData,'<div class="ff' + b + '" onmouseover="this.style.backgroundColor=\'#E7EFDF\';" onmouseout="this.style.backgroundColor=\'\';"><a href="/product/' + product['id'] + '"><img src="/images/32.gif" style="background: url(' + product['findfast'] + ') #fff center no-repeat;" alt="' + product['name'] + '" /></a><a href="/product/' + product['id'] + '/' + product['cleanname'] + '" class="finda">' + shortname + '</a><br /><span><strong> &pound;' + product['cost'] + ' inc VAT </strong></span><em><a href="/products/' + product['categorylink'] +  '">' + product['categoryname'] + '</a></em><div class="clear"></div></div><div class="ddbar"></div>');  
							
							if(b == 1) { b = 0; } else { b = 1; }
						});
						 

						new Insertion.Top(sResultsData,'<div class="stext"><em>Your search for "<strong>' + $F('searchField') + '</strong>" found:</em><strong><br />Showing top / nearest matches</strong></div>');

						new Insertion.Bottom(sResultsData,'<div class="rtext"> <strong>&raquo;</strong> <a href="/fullsearch/' + $F('searchField') + '">View all ' + productcount + ' results</a> <em> - or - add more terms to refine your search</em></div> ');
						

						},
					queue: {position: 'start', scope: 'searcha'}
					}
				);
			}
			
			

			if(!sIble) {
				clearElementAll(sResultsData);
				var b = 1;
				
				products.each(function(product) {
					shortname = product['name'].substring(0, 51);
					if(shortname.length >= 51) { shortname = shortname + '...'; }

					new Insertion.Top(sResultsData,'<div class="ff' + b + '" onmouseover="this.style.backgroundColor=\'#E7EFDF\';" onmouseout="this.style.backgroundColor=\'\';"><a href="/product/' + product['id'] + '"><img src="/images/32.gif" style="background: url(' + product['findfast'] + ') #fff center no-repeat;" alt="' + product['name'] + '" /></a><a href="/product/' + product['id'] + '/' + product['cleanname'] + '" class="finda">' + shortname + '</a><br /><span><strong> &pound;' + product['cost'] + ' inc VAT </strong></span><em><a href="/products/' + product['categorylink'] +  '">' + product['categoryname'] + '</a></em><div class="clear"></div></div><div class="ddbar"></div>'); 
									
					if(b == 1) { b = 0; } else { b = 1; }
				});
				new Insertion.Top(sResultsData,'<div class="stext"><em>Your search for "<strong>' + $F('searchField') + '</strong>" found:</em><strong><br />Showing top / nearest matches</strong></div>');

				new Insertion.Bottom(sResultsData,'<div class="rtext"> <strong>&raquo;</strong> <a href="/fullsearch/' + $F('searchField') + '">View all ' + productcount + ' results</a> <em> - or - add more terms to refine your search</em></div>');
				

				//new Effect.Appear(sResults, {duration: 0.3});
				new Effect.BlindDown(sResults,{duration: 0.3, scaleMode: 'box', queue: {position: 'start', scope: 'searcha'}});
			
			} else {
				clearElementAll(sResultsData); 
				var b = 1;
				
				products.each(function(product) {
					shortname = product['name'].substring(0, 51);
					if(shortname.length >= 51) { shortname = shortname + '...'; }

					new Insertion.Top(sResultsData,'<div class="ff' + b + '" onmouseover="this.style.backgroundColor=\'#E7EFDF\';" onmouseout="this.style.backgroundColor=\'\';"><a href="/product/' + product['id'] + '"><img src="/images/32.gif" style="background: url(' + product['findfast'] + ') #fff center no-repeat;" alt="' + product['name'] + '" /></a><a href="/product/' + product['id'] + '/' + product['cleanname'] + '" class="finda">' + shortname + '</a><br /><span><strong> &pound;' + product['cost'] + ' inc VAT </strong></span><em><a href="/products/' + product['categorylink'] +  '">' + product['categoryname'] + '</a></em><div class="clear"></div></div><div class="ddbar"></div>'); 
							
					if(b == 1) { b = 0; } else { b = 1; }
				});
				new Insertion.Top(sResultsData,'<div class="stext"><em>Your search for "<strong>' + $F('searchField') + '</strong>" found:</em><strong><br />Showing top / nearest matches</strong></div>');

				new Insertion.Bottom(sResultsData,'<div class="rtext"> <strong>&raquo;</strong> <a href="/fullsearch/' + $F('searchField') + '">View all ' + productcount + ' results</a> <em> - or - add more terms to refine your search</em></div> ');
				

				new Effect.BlindDown(sResults,{ 
					duration: 0.3, scaleMode: 'box', 
					queue: {position: 'end', scope: 'searcha'}
					}
				);
			}
		} else {
			//new Effect.Fade(sResults, {duration: 0.3});

			if(sIble) {
				new Effect.BlindUp(sResults,{ 
					duration: 0.3, scaleMode: 'box', 
					afterFinish: function() { clearElementAll(sResultsData); },
					queue: {position: 'start', scope: 'searcha'}
					}
				);
			} else {
				clearElementAll(sResultsData);
			}
			

			new Insertion.Top(sResultsData,'<div class="stext"><em>Your search for "<strong>' + $F('searchField') + '</strong>" found no matches.</em><strong><br />Please search again</strong></div>');

			//new Insertion.Bottom(sResultsData,'<div class="rtext"> <strong>&raquo;</strong> <a href="/fullsearch/' + $F('searchField') + '">View all ' + productcount + ' results</a> <em> - or - add more terms to refine your search</em></div> ');
				
			new Effect.BlindDown(sResults,{duration: 0.3, scaleMode: 'box', queue: {position: 'end', scope: 'searcha'}});

		}
}

var errFunc = function(t) {
	alert('An error has been encountered. Please contact customer support');
}



onloads.push(registerFindFastEvents);
if (window.captureEvents) window.captureEvents(Event.LOAD);
