var ONESEARCH_URL = 'http://onesearch.svc.primedia.com';
var APPLICATION = 'AG';
var channel = "apartments";
var current_channel = '';
var valid_key = false;
var default_records_per_page = 20;

$(document).ready(function() {
  setup_default_text();
  
  $("input#user_search").oneautocomplete({ highlight: highlightItem });
  $("form#search_form").onesearch();

  $("body").bind("onesearch-success", seoPathSuccess);
  $("body").bind("onesearch-error-noresults", seoPathError);
  $("body").bind("onesearch-error-invalid", invalidSearchTerm);
  
  // bind the search button to the submit event
  $("form#search_form a.global_btn").click(function() {
    $(this).parents("form").submit();
    return false;
  });
  
  
  INVALID_QUERIES = ["City, State or Zip",""];
  
  // clears autocomplete text
  $('#osClear').click( function() {
    $('#user_search').val('');
  });
  
  current_channel = $('#current_channel').html();
  
});

function setup_default_text() {
  default_text = 'Enter a City, State or Zip';
  last_searched_term = $.cookie("last_searched_term");
  if (last_searched_term) {
    $('#user_search').val(last_searched_term);
  } else if ($('#user_search').val() == ''){
    $('#user_search').val(default_text);
  }
  $('#user_search').click(function(){
    if(this.value.match(default_text)) this.value = "";
  }).blur(function(){
    if(this.value == "") this.value = default_text;
  });
}

function highlightItem(value, term) {
  return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
}

function seoPathSuccess(event) {

  //Capture the double enter key and squash it
  document.body.onkeypress = function (event) {
    event = event || window.event;
    if (event.keycode = 13) { return false; }
    return true;
  }

  var search_val = $("#user_search").val();
  
  $.cookie("last_searched_term",search_val, { path: '/', expires: 90 });

  var get_url = $("#search_form").attr("action") + event.seoPath;
  get_url = get_url.replace("apartments/","");

  //handle presence of a forward slash
  if( !( get_url.match(/^\//) ) ){
    get_url = '/'+get_url;
  }

  if(current_channel != null  && current_channel.length > 0){
    get_url = "/"+current_channel+get_url;
  }

  window.location = get_url;
}

//Rails/Application should repond with HTML status 422 upon error to ensure the error callback is executed.
function seoPathError(event) {
  searchErrorMessage(event.message);
}

function invalidSearchTerm(event) {
  searchErrorMessage("You must enter a city, state or a zip code before this search can be performed.");
}

function searchErrorMessage(message){
  $("#search_errors").text(message).show();
  if ($.browser.msie) { 
    $("#searchCitiesStates, #searchOptions").hide();
  }
  $("body").bind('click.search_errors', function(){
    $("#search_errors").hide();
    if ($.browser.msie) { 
      $("#searchCitiesStates, #searchOptions").show();
    }
    $("body").unbind('click.search_errors');
  });
}
