	//grab typed characters

var search = "";
var scrollDelayTimer;
var deleteDelayTimer;
var nodePosition = 0;
var pageTitle = "";

pageTitle = document.title;

$.expr[":"].startsWith = function(el, i, m) {
	var search = m[3]; 
	if (!search) return false;
	return eval("/^[/s]*" + search + "/i").test($(el).text());
};

$(document).keypress(function(event) {

	var tmpPosition = 0;
	var $target = $(event.target);

	//Ignore if Space Bar, Enter Key pressed or if a form element has focus.
	if (event.which == '13' | event.which == '32' | $target.is(":input")) {
		return;
	}

	search += String.fromCharCode(event.which);

	document.title = "Search for: " + search;

	try
	{
		tmpPosition = $("#a2z_list a:startsWith(" + search + ")").offset().top;
	}
	catch (Err) {
	}

	if (tmpPosition > 0) {
		nodePosition = tmpPosition;
	}

	//Delay scroll whilst the user types.
	clearTimeout(scrollDelayTimer);
	scrollDelayTimer = setTimeout(function() {

		if (nodePosition > 0) {
			$('html,body').animate({ scrollTop: nodePosition - 10 }, 'slow');
		}

	}, 500);

	//Clear search criteria
	clearTimeout(deleteDelayTimer);
	deleteDelayTimer = setTimeout(function() {

		search = "";
		nodePosition = 0;
		document.title = pageTitle;

	}, 2000);

});
