$(function () {
    var analyzeTimeoutId = null;

    function analyzeQuestion() {
        window.clearTimeout(analyzeTimeoutId);
        analyzeTimeoutId = window.setTimeout(function () {
            var searchText = $('#question').val();
            if (searchText) {
                $.post('/questions/analyze', { QuestionText: searchText }, function (response) {
                    $('#specialties').html('');
                    $(response.Specialties).each(function () {
                        $('#specialties').append('<li>' + this.Name + '</li>');
                    });
                });
            } else {
                $('#specialties').html('');
            }
        }, 250);
    };

    $('#question')
        .keyup(analyzeQuestion)
        .change(analyzeQuestion);

    window.setTimeout(function () { $('#question').focus(); }, 250);
});
