$(function () {
    $("#searchbox [id$='SearchText']").autocomplete({
        source: function (request, response, callback) {
            var SearchString = $("#searchbox [id$='SearchText']").val();
            $.ajax({ url: "http://ww2.medway.gov.uk/WebServices/Google_WebService/GoogleSearch.asmx/GetlgslText?prefixText=" + JSON.stringify(SearchString) + "&format=jsonp&jsoncallback=?",
                dataType: "jsonp",
                json: "jsoncallback",
                success: function (json) {
                    response($.map(json.d, function (item) {
                        return {
                            data: item,
                            value: item,
                            result: item
                        }
                    }))
                },
                error: function (xhr, msg) {
                    //alert(xhr.responseText + " " + msg);
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {

            document.location = "http://www.medway.gov.uk/search?q=" + encodeURIComponent(ui.item.value) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
        }
    });
    
    $("#searchbox [id$='SearchText']").keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
	if (keycode == '13') {
	    document.location = "http://www.medway.gov.uk/search?q=" + encodeURIComponent($(this).val()) + "&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
	    return false;
	}
    });
});


