$(document).ready(function(){
	var search = "";
	var shown = false; 
	var start = true;
	
	$("a.searchMenu").simpletip({
   		persistent: false,
		position: 'bottom',
   		content: 'Just start typing to search. No matter what page you are on.<br>Click here if it doesn\'t work...'
	});

	$(document).bind('keypress', function(event){ 
	var key;
	if (event.keyCode) key= event.keyCode;
	else if (event.which) key= event.which;

	var targ;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	if( targ.tagName.toLowerCase() == 'textarea' || targ.tagName.toLowerCase() == 'input')
		return true;
	
	if(( key >= 48 && key <= 57) || ( key >= 65 && key <= 90) || ( key >= 97 && key <= 122) || key == 32) 	
	{
		if( shown == true)
			return true;
		var boxCode = '<div id=\"typeSearch\"><input type=\"text\" name=\"searchBox\" id=\"searchBox\"><p id="searchHint">Type your query and press enter to search...</p></div>';
	
		$.msg({ autoUnblock : false , klass : 'type-search', content: boxCode , beforeUnblock : function(){ shown = false; }, fadeIn : 0, fadeOut : 1 });
		shown = true;
		//$('#searchBox').val(String.fromCharCode(key));
		$('#searchBox').focus();
		return true;
	}





		$('#searchBox').focus();



	
	});

		
	$(document).bind('keyup', function(event){
		var key = event.which;
		if( shown == true && key == 27 )
      		{
         		$.msg( 'unblock'); 
			$('#typeSearch').remove();
			shown = false;
	      	} 
		else if( key == 13)
		{
			search = $('#searchBox').val();
			if( search.length > 0)
			{
				window.location = "http://www.exeterguild.com/search/?q=" + search;
			}
			return true;
      		}
		return true;
	});
});

