﻿$(document).ready(function () {
    $("span.suggestion + *:input:not(:radio,:submit)").each(function () { $(this).watermark($(this).prev().text()); });

    $("input[type='text']").
        blur(function () { $(this).val($(this).val().replace(/^\s+/g, '').replace(/\s+$/g, '')); });

    $("input[name='DishA'], input[name='DishB']")
        .autocomplete(dishAutoCompleteUrl, { width: '10.25em', multiple: true, multipleSeparator: ' ', scroll: true })
        .live('keydown', CloseAutoComplete);

    $("input[name='SearchTerms']")
        .autocomplete(categoryAutoCompleteUrl, { width: '224px', multiple: false, scroll: true, delay: 0 })
        .live('keydown', CloseAutoComplete);

    $("input[name='CategoryKeywords']")
        .autocomplete(categoryAutoCompleteUrl, { width: '224px', multiple: false, scroll: true, delay: 0 })
        .live('keydown', CloseAutoComplete);

    $("input[name='Location']")
        .autocomplete(locationAutoCompleteUrl, { width: '224px', multiple: false, scroll: true, minChars: 3, max: 2000, delay: 0 })
        .live('keydown', CloseAutoComplete);

    $("ul.short-list")
        .hideMaxListItems({ 'max': 4, 'speed': 500, 'moreHTML': '<a class="maxlist-more" href="#">(show more...)</a>' });

    $("#kgbpeople").submit(function () {
        return ($('#input-namekgbplp').val().trim()) != "";
    });

    $("input[name='SearchType']").change(function () {
        var inputBox = $("input[name='SearchTerms']");
        if ($(this).val() == 1) {
            inputBox.watermark(inputBox.prev().text());
            inputBox.autocomplete(categoryAutoCompleteUrl, { width: '224px', multiple: false, scroll: true, delay: 0 }).live('keydown', CloseAutoComplete);
        }
        else {
            $.watermark.hide("input[name='SearchTerms']");
            inputBox.unbind(".autocomplete");
            inputBox.watermark("e.g. Pizza Hut");
        }
    });
});

function CloseAutoComplete(event) {
    if (event.keyCode == 27) {
        $(this).focus();
        return false;
    }
}
