var links,lastquery='',matches,resultsdiv;

$(document).ready(function() {
	showquickaccess();
	matches = links = $('a.quick').each(function() {
		$.data(this,'keywords',(this.innerHTML).toLowerCase().replace(/[,-\/]/g,' ').replace(/[^a-zA-Z 0-9]+/g,''));
	}); // grab links, attach data to match against
	resultsdiv = $('#quickresults');
	$('#qa_search').attr('autocomplete','off').keyup(function() { // on each keypress, filter the links
		var query = $.trim($(this).val().toLowerCase().replace(/[,-\/]/g,' ').replace(/[^a-zA-Z 0-9]+/g,''));
		if(query==lastquery) return; // do nothing if the query is unchanged
		resultsdiv.html(''); // wipe results div
		$('p').removeClass("highlight");
		if(query.length==0) return; // return no results if there is no query
		if(query.indexOf(lastquery)!=0) matches = links; // if this query is not a subset of the last query, reinitialize the matches
		lastquery = query;
		$.each(query.split(' '),function() { // filter the result for each word in the query
			var search = this;
			matches = matches.filter(function() { return (' ' + $.data(this,'keywords')).indexOf(' ' + search)>=0; });
		});
		if(matches.length<=10) matches.each(function() { resultsdiv.append($(this).clone()); $(this).parent().addClass("highlight");});
		else if(matches.length) return;
		else resultsdiv.html('(No results.)');
	});
	$('#qa_form').submit(function() { resultsdiv.find('a').eq(0).each(function() { window.location=this.href; }); return false; });
});

function showquickaccess()
{
	identity=document.getElementById('qa_container');
	identity.className='qa_show';
}
