﻿/********************************************************
jQuery based function to auto suggest links based on
search results
********************************************************/
function quickSearch(inputString, homePageId) {
    if (inputString.length < 4) {
        $('#suggestions').slideUp();
    } else {
        $('#searchtext').addClass('load');
        $.ajax({
            type: "POST",
            url: "/webservices/SiteSearch.asmx/Search",
            contentType: "application/json; charset=utf-8",
            data: "{HomeNodeId:'" + homePageId + "', Terms:'" + inputString + "', ResultsLimit:'5'}",
            dataType: "json",
            success: function(response) {
                $('#searchtext').removeClass('load');
                if ($('#searchtext').val().length > 3 && response.d.length > 0) {
                    $('#suggestionsList').html(response.d);
                    $('#suggestions').slideDown();
                } else {
                    $('#suggestions').slideUp();
                }
            },
            error: function(msg) {
                $('#searchtext').removeClass('load');
                alert('fail: '+msg.status + ', ' + msg.statusText +', ' + msg.responseText);   
            }
        });
    }
}

function clearQuickSearch() {
    setTimeout(function() {
        $('#suggestions').slideUp();
    }, 100); // delay is for the blur event which causes this to execute. It executes before the click event on the LI does, so it's delayed by 100ms to compensate
}
