$(document).ready(function() {

	var timer;

// add onchange for search
	$('#search_keyword').keyup(function() {
		// skip short phrases
		if($('#search_keyword').val().length >= 3) {

			var keyword = $('#search_keyword').val();

			$.ajax({
				url: "search_autocomplete.php",
				cache: false,
				data: { searchquery : keyword },
				success: function(results){
					$("#search_autocomplete").html(results);
					$("#search_autocomplete").show();
					if(timer) {
					    clearTimeout(timer);
					    timer = null
					   }

					$('.search_suggestion').each(function(i) {
						$(this).click(function() {
							$('#search_keyword').val($(this).html());
							$("#search_autocomplete").hide();
							return false;
						});
					});
				}
			});
		} else {
			$("#search_autocomplete").hide();
		}

	});

     $("#search_autocomplete").mouseover(function() {
        if(timer) {
            clearTimeout(timer);
            timer = null
           }
//        timer = setTimeout(function() {
//            if(!$("#search_autocomplete").is(':hidden')){
//		$("#search_autocomplete").hide();
//            }
//
//        }, 1000);
    });

     $("#search_autocomplete").mouseleave(function() {
            if(timer) {
                clearTimeout(timer);
                timer = null
               }
            timer = setTimeout(function() {
                $("#search_autocomplete").hide();

            }, 500);
    });


});

