$(document).ready(function() {
	// Turn all buttons with ID of Submit or Preview into jqueryui styled buttons
	$('#Submit').button();
	$('#Preview').button();
	
	// Search autocomplete
	$("#key").autocomplete({
		source: function(request, response) {
    		$.getJSON("/js/ajax.php", { searchTerm: request.term }, response);
  		},
		minLength: 3,
		delay: 1000,
		// On select, redirect to the URL, return false to prevent the selected term appearing in the form field search box
		select: function(event, ui) { 
            window.location.href = ui.item.url;
			return false;
        },
		// Stop the highlighted term appearing in the form field search box when being focused on, i.e when the keyboard is being used to go up and down in the search results
		focus: function(event, ui) {
			return false;
		}}).data("autocomplete")._renderItem = function(ul, item) {
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>"+ item.label + "</a>")
                .appendTo(ul);
        };
});


