// Copyright Neil Ramsden 2004-2012. All rights reserved.

// Word Search scripts
// Neil Ramsden
// search.js
// 11 Dec 11 -> 20 Jan 12    NR
//  Moved to jQuery.
//  Added FreeBSD word pool.
// 5 Oct 04 -> 11 Oct 04

/*---------------------------------------------------------------------------
Configuration
*/

var max_matches = 1000; //const


/*---------------------------------------------------------------------------
Search Functions
*/

function doSearch(pool, pattern)
//->search in word pool for pattern and write to results window
{
    //ensure there's a pattern
    if (!pattern)
        return;

    //delegate according to word pool
    if ("standard" == pool) {
        doSearch_Standard(pool, pattern)
    }
    else if ("FreeBSD" == pool) {
        doSearch_BSD(pool, pattern)
    }
    else { 
        $("#pane-header").html('Search failed! - unrecognised word pool!');
    }
}


function doSearch_Standard(pool, pattern)
//->search for pattern in local (standard) pool and write to results window
{
    //prepare controls for search
    prepareSearch()

    //find matches
    var matches = [];
    var expression = RegExp(pattern, 'i');
    var word_count = wordList.length;
    for (index = 0; index < word_count; index ++)
    {
        word = wordList[index];
        if (word.search(expression) != -1)
            matches.push(word);
    }

    //write matches to results window
    printResults (pool, pattern, matches);
    
    //finalise controls after search
    finishSearch()
}


function doSearch_BSD(pool, pattern)
//->search for pattern in server (FreeBSD) pool and write to results window
{
    try {

        //prepare for search
        prepareSearch();

        //request search
        $.getJSON('../../cgi-bin/searcher/do_word_search.py',
            { 'pool': pool, 'pattern': pattern, 'max_matches': max_matches}, 
            function (data) {
                try {
                    if (data.error)
                        throw (Error(data.error));
                    else
                        printResults(data.pool, data.pattern, data.matches);
                } //try
                catch (e) {
                    $("#pane-header").html(data.pool + ' Search failed! - ' + e.message);
                }
                finally {
                    finishSearch();
                }
            }
        );
        
    } //try
    
    catch (e) {
        $("#pane-header").html('Search failed! - ' + e.message);
        finishSearch();
    }
}


function finishSearch()
//->finish search by activating disabled controls
{
    $("#go-button").removeAttr("disabled");
}


function prepareSearch()
//->prepare controls for search
{
    $("#go-button").attr("disabled", "disabled");
    if (!$("#instructions").hasClass("invisible"))
        $("#instructions").addClass("invisible");
    $("#results-list").empty();
    $("#pane-header").html('Searching...');
}


function printResults (pool, pattern, matches)
//->write matches to results window
{
    try {
    
        //write header
        var match_count = matches.length; //may be modified!
        function match_count_text () {
            switch (pool) {
                case 'FreeBSD':
                    if (max_matches < match_count) {
                        match_count = max_matches;
                        return 'first ' + max_matches + ' matches';
                    }
                case 'standard':
                default:
                    return match_count + (match_count == 1 ? ' match' : ' matches');
            }
        }        

        $("#pane-header").html((pool == 'standard' ? '' : pool + ' ') 
            + 'Search Results for "' + pattern
            + '"<br>(' + match_count_text() + ')')

        //write word list
        matches_markup = [];
        for (index = 0; index < match_count; index ++)
        {
            /*note: use <div> rather than inline+<br> as latter v slow to display;
                    need <div> and/or trailing space to avoid one (long) line result
            */
            word = matches[index];
            matches_markup.push('<div class="found-word">' + word + ' </div>');
        }
        $("#results-list").html(matches_markup.join(""));

        //write notes
        if (!match_count) {
            message_text = '<div class="no-matches-message">\
              <p>No matches found in the ' + pool + ' word pool.</p>'
            if ("standard" == pool)
                message_text += '<p>The FreeBSD list may contain some matches;\
                    but note that you may need to treat its results with extra care.</p>';
            message_text += '</div>';
            $("#results-list").append(message_text);
        }
        
    } //try
    
    catch (e) {
        $("#pane-header").html('Search failed! - ' + e.message);
    }
    
    finally {
        finishSearch()
    }
}


/*---------------------------------------------------------------------------
Reset Function
*/

function resetSearcher()
//->reset main form and clear results pane.
{
    $("#search-pattern").val("");
    $("#pane-header").html('Ready!')
    $("#instructions").removeClass("invisible");
    $("#results-list").empty();
}

