/**
 * Pages/People search script
 * Patrick Lewis [plewis@binghamton.edu]
 * Last Updated: 2011-03-03
 *
 * This script provides the toggle between 'Pages' and 'People' for
 * the search form included in the global header include.
 *
 * The BU Directory search requires that the 'search' variable be the
 * only one specified in the URL, or the query will fail.
 *
 * Example: http://www.telecom.binghamton.edu/directory/directory.results?search=patrick
 *
**/

$(document).ready(function() {
    // Reset the search to 'Pages' when the page loads
    $('#search-filters1').click();

    // Submit the form when the spyglass image is clicked
    $('img.searchButton').click(function() {
        $('#cse-search-box').submit();
    });

    // Configure the form to search pages when the Pages radio button is clicked
    $('#search-filters1').click(function() {
        // Restore the hidden form fields
        $('#cse-search-box').append(
            '<input type="hidden" name="cx" value="013262338783433875854:7stszgiris8" />',
            '<input type="hidden" name="cof" value="FORID:11" />',
            '<input type="hidden" name="ie" value="UTF-8" />'
            );

        // Change the name of the text input to 'q' (required by Google custom search)
        $('input.searchBox').attr('name', 'q');

        // Change the target of the form to the Google custom search page
        $('#cse-search-box').attr('action', 'http://www.binghamton.edu/results.html');
    });

    // Configure the form to search the BU Directory when the People radio button is clicked
    $('#search-filters2').click(function() {
        // Remove the hidden form fields (directory search will fail if these are present)
        $('input[name="cx"]').remove();
        $('input[name="cof"]').remove();
        $('input[name="ie"]').remove();

        // Change the name of the text input to 'search' (required by directory search)
        $('input.searchBox').attr('name', 'search');

        // Change the target of the form to the BU directory search page
        $('#cse-search-box').attr('action', 'http://www.telecom.binghamton.edu/directory/directory.results');
    });
});

