var term_len = 0;
var query_term = '';
var query_cache = '';
var query_ready = true;
var query_invalid = '';
var hint_id_select = -1;
var hint_count = 0;

function keyIsAlphaNum(e)
{
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys : 8 = backspace; 9 = tab; 13 = entrée; 46 = suppr;
	if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 46)
	   return true;
	// alphanumeriques
	else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
	   return true;
	else
	   return false;
}

function hide_hints()
{
	$('#search_hints').html('');
	hint_id_select = -1;

	if($(this).attr('value') == '')
		$(this).val('Rechercher sur le site');
}

$(document).ready(function()
{
	$('.tooltip').tooltip({showURL: false });

	$('#search_term').click(function()
	{
		if($(this).attr('value') == 'Rechercher sur le site')
			$(this).val('');
	});

	$('#search_term').blur(function()
	{
		setTimeout('hide_hints()', 250);
	});

	$('#search_term').keyup(function(e)
	{
		// 27 = escape
		if (e.keyCode == 27)
		{
			$('#search_hints').html('');
			hint_id_select = -1;
		}
		// 38 = flèche haut
		else if (e.keyCode == 38 && hint_id_select > 0)
		{
			$('#hint_s_'+hint_id_select).removeClass('li_hint_sel');
			hint_id_select--;
			li_sel = $('#hint_s_'+hint_id_select);
			li_sel.addClass('li_hint_sel');
			$(this).val(li_sel.children().html());
		}
		// 40 = flèche bas
		else if (e.keyCode == 40 && hint_id_select < hint_count - 1)
		{
			$('#hint_s_'+hint_id_select).removeClass('li_hint_sel');
			hint_id_select++;
			li_sel = $('#hint_s_'+hint_id_select);
			li_sel.addClass('li_hint_sel');
			$(this).val(li_sel.children().html());
		}

		if (keyIsAlphaNum(e) == false)
			return;

		query_term = $(this).attr('value');
		term_len = query_term.length;

		if (term_len >= 3)
		{
			if (query_ready && (query_invalid == '' || query_term.indexOf(query_invalid) == -1))
			{
				post_data = 'search_term='+query_term;
				query_ready = false;

				$.ajax
				({
					type: 'POST',
					url: 'inc_x_recherche.php',
					data: post_data,
					success: function(response)
					{
						$('#search_hints').html(response);
						hint_id_select = -1;

						if (response == '')
						{
							query_invalid = query_term;
							query_ready = true;
							query_cache = '';
						}
						else
							query_invalid = '';

						if (query_cache != '' && query_invalid != '')
						{
							post_data = 'search_term='+query_cache;

							$.ajax({
								type: 'post',
								url: 'inc_x_recherche.php',
								data: post_data,
								success: function (response)
								{
									$('#search_hints').html(response);
									hint_id_select = -1;
									query_ready = true;
									query_cache = '';

									if (response == '')
										query_invalid = query_term;
									else
										query_invalid = '';
								}
							});
						}
						else
							query_ready = true;
					}
				});
			}
			else
			{
				query_cache = query_term;
			}
		}
		else
		{
			$('#search_hints').html('');
			hint_id_select = -1;
		}

	});
});