/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
$(document).ready(function()
{
	// Safely inject CSS3 and give the search results a shadow
	var cssObj = {'box-shadow' : '#888 5px 10px 10px', // Added when CSS3 is standard
		'-webkit-box-shadow' : '#888 5px 10px 10px', // Safari
		'-moz-box-shadow' : '#888 5px 10px 10px'}; // Firefox 3.5+
	$("#suggestions").css(cssObj);
        $("#suggestions2").css(cssObj);
	
	// Fade out the suggestions box when not active
	 $("input").blur(function(){
	 	$('#suggestions').fadeOut();
                $('#suggestions2').fadeOut();
	 });
});

var is_searching = false;
var inputStringGlobal = '';

function checkFocus(event) {
    if ((event.keyCode == 40) || (event.keyCode == 38)) {
        event.preventDefault();
    }
    if ((event.keyCode == 13) && $('#suggestions a.focus').length) {
        event.preventDefault();
        window.location.href = $('#suggestions a.focus').attr('href');
    }
}

function lookup(language, inputString, event) {
        inputStringGlobal = inputString;
        var onfocus;
        if (event.keyCode == 40) {
            if ($('#suggestions a.focus').length) {
                onfocus = $('#suggestions a.focus');
                $('#suggestions a').removeClass('focus');
                onfocus.nextAll('a:first').addClass('focus');
            } else {
                $('#suggestions a:first').addClass('focus');
            }
        } else if (event.keyCode == 38) {
            if ($('#suggestions a.focus').length) {
                onfocus = $('#suggestions a.focus');
                $('#suggestions a').removeClass('focus');
                onfocus.prevAll('a:first').addClass('focus');
            } else {
                $('#suggestions a:last').addClass('focus');
            }
        } else if (event.keyCode == 13) {
            return;
        } else {
            if(inputStringGlobal.length == 0) {
                    $('#suggestions').fadeOut(); // Hide the suggestions box
            } else {
                if (is_searching) return;
                is_searching = true;
                setTimeout(function() {
                    if(inputStringGlobal.length == 0) {
                        $('#suggestions').fadeOut(); // Hide the suggestions box
                    } else {
                        $.post("ajax-autosuggest", {language_id: ""+language+"", queryString: ""+inputStringGlobal+""}, function(data) { // Do an AJAX call
                                $('#suggestions').fadeIn(); // Show the suggestions box
                                $('#suggestions').html(data); // Fill the suggestions box
                        });
                    }
                    is_searching = false;
                },
                250);
            }
        }
}


function checkFocus1(event) {
    if ((event.keyCode == 40) || (event.keyCode == 38)) {
        event.preventDefault();
    }
    if ((event.keyCode == 13) && $('#suggestions a.focus').length) {
        event.preventDefault();
        window.location.href = $('#suggestions a.focus').attr('href');
    }
}

function lookup1(language, inputString, event) {
        inputStringGlobal = inputString;
        var onfocus;
        if (event.keyCode == 40) {
            if ($('#suggestions2 a.focus').length) {
                onfocus = $('#suggestions a.focus');
                $('#suggestions2 a').removeClass('focus');
                onfocus.nextAll('a:first').addClass('focus');
            } else {
                $('#suggestions2 a:first').addClass('focus');
            }
        } else if (event.keyCode == 38) {
            if ($('#suggestions2 a.focus').length) {
                onfocus = $('#suggestions a.focus');
                $('#suggestions2 a').removeClass('focus');
                onfocus.prevAll('a:first').addClass('focus');
            } else {
                $('#suggestions2 a:last').addClass('focus');
            }
        } else if (event.keyCode == 13) {
            return;
        } else {
            if(inputStringGlobal.length == 0) {
                    $('#suggestions2').fadeOut(); // Hide the suggestions box
            } else {
                if (is_searching) return;
                is_searching = true;
                setTimeout(function() {
                    if(inputStringGlobal.length == 0) {
                        $('#suggestions2').fadeOut(); // Hide the suggestions box
                    } else {
                        $.post("ajax-compautosuggest", {language_id: ""+language+"", queryString: ""+inputStringGlobal+""}, function(data) { // Do an AJAX call
                                $('#suggestions2').fadeIn(); // Show the suggestions box
                                $('#suggestions2').html(data); // Fill the suggestions box
                        });
                    }
                    is_searching = false;
                },
                250);
            }
        }
}
