/*global Autocompleter, lookupEmployer, lookupLocation */
/**
 * Location caching store.
 */
var locationCache = new Autocompleter.Cache( lookupLocation, { choices: 30 } );
/**
 * Location response caching function.
 */
var cachedLocations = locationCache.lookup.bind( locationCache );
/**
 * Employer caching store.
 */
var employerCache = new Autocompleter.Cache( lookupEmployer, { choices: 30 } );
/**
 * Employer response caching function.
 */
var cachedEmployers = employerCache.lookup.bind( employerCache );

/**
 * @author    James Swanson <james@aiminstitute.org>
 * @param     {String} needle the employer string to search for
 * @param     {Function} suggest the auto-completion callback
 * @version   2009-12-10
 */
function lookupEmployer( needle, suggest ) {
    /*global Ajax */
    (new Ajax.Request( '/locate/update_employers', {
        parameters: {
            name: needle
          , rand: (new Date()).getTime()
        }
      , onSuccess: function( r ) {
            suggest( r.responseJSON );
        }
    } ));
} // lookupEmployer

/**
 * @author    James Swanson <james@aiminstitute.org>
 * @param     {String} needle the location string to search for
 * @param     {Function} suggest the auto-completion callback
 * @version   2009-12-10
 */
function lookupLocation( needle, suggest ) {
    /*global Ajax */
    (new Ajax.Request( '/locate/update_locations', {
        parameters: {
            name: needle
          , rand: (new Date()).getTime()
        }
      , onSuccess: function( r ) {
            suggest( r.responseJSON );
        }
    } ));
} // lookupLocation

/**
 * Sets the hidden expanded search form variable based on value provided.
 * @author    James Swanson <james@aiminstitute.org>
 * @param     {String} value a string value from a search form field
 * @version   2009-10-29
 */
function setExpanded() // ------------------------------------------ setExpanded
{
    /*global $, $F */
    $('expanded').value = ($F('employer').empty()) ? 'N' : 'Y';
} // setExpanded

/**
 * Initializes the page after load.
 * @author    James Swanson <james@aiminstitute.org>
 * @see       #setExpanded
 * @version   2009-12-11
 */
function initPage() // ------------------------------------------------ initPage
{
    /*global Ajax, Event, setExpanded */
    Event.observe( 'employer', 'keypress', setExpanded );
    Event.observe( 'keywords', 'focus', function( e ) {
        e.target.select();
    } );
    Event.observe( 'location', 'focus', function( e ) {
        e.target.select();
    } );
    Event.observe( 'employer', 'focus', function( e ) {
        e.target.select();
    } );
    (new Autocompleter.Json( 'location', 'location-menu', cachedLocations, {
        minChars: 2
      , indicator: 'locations-loading'
    } ));
    (new Autocompleter.Json( 'employer', 'employer-menu', cachedEmployers, {
        minChars: 2
      , indicator: 'employers-loading'
      , afterUpdateElement: function() {
            $('employer').value = $F('employer').replace( /\*$/, '' );
        }
    } ));
} // initPage

