/**
 * RunTrackr - Main Page Scripts.
 */

jQuery(function()
{
  // Add a tooltip to the route/location search bar.
  runtrackr.Utility.addTooltip('#location-input');

  // Event handler for searching from the front page.
  jQuery('.search form.location-jump-to').submit(submitSearch);
});

/**
 * Submits a search to the Ajax-driven search results page.
 *
 * This is NON-standard and uses bad JavaScript practices.
 */
function submitSearch(e)
{
  e.preventDefault();

  var searchValue = jQuery.trim(jQuery(this).find('#location-input').val());
  if (0 == searchValue.length)
  {
    // Do not proceed if there is no valid search value.
    return;
  }

  // Form the search query, which is contained in the URI fragment (#)
  var hash = '#' + encodeURIComponent(searchValue);
  var searchQueryUri = jQuery(this).attr('action') + hash;

  // Bring up the search results page.
  window.location = searchQueryUri;
}
