$(document).ready(function () {

  set_search_radius_select(state_machine.get_get_data()["search_radius"])

  add_close_cities_html();
  add_neighborhoods_html(); 
  add_amenities_html();


  set_sort_method_select(state_machine.get_get_data()["sort_method"]);

  set_min_rent_select(state_machine.get_get_data()["min_rent"]);
  set_max_rent_select(state_machine.get_get_data()["max_rent"]);

  set_beds_select(state_machine.get_get_data()["min_beds"],state_machine.get_get_data()["max_beds"]);

  set_bathrooms_select(state_machine.get_get_data()["bathrooms"]);

  set_min_square_feet_select(state_machine.get_get_data()["min_square_feet"]);
  set_max_square_feet_select(state_machine.get_get_data()["max_square_feet"]);

  set_checkbox("allow_cats",state_machine.get_get_data()["allow_cats"]);
  set_checkbox("allow_small_dogs",state_machine.get_get_data()["allow_small_dogs"]);
  set_checkbox("allow_large_dogs",state_machine.get_get_data()["allow_large_dogs"]);

  set_checkbox("is_house",state_machine.get_get_data()["is_house"]);
  set_checkbox("is_condo",state_machine.get_get_data()["is_condo"]);

  set_checkbox("is_furnished",state_machine.get_get_data()["is_furnished"]);
  set_checkbox("is_short_term",state_machine.get_get_data()["is_short_term"]);
  set_checkbox("is_senior",state_machine.get_get_data()["is_senior"]);
  set_checkbox("is_corporate",state_machine.get_get_data()["is_corporate"]);


  //EVENT HANDLERS

  $("#show_options_a").click(function() {
    $('#show_options').hide();
    $('#hide_options').show();
    $('#advanced').show('slide');
    return false;
  });

  $("#hide_options_a").click(function() {
    $('#hide_options').hide();
    $('#show_options').show();
    $('#advanced').hide('slide');
    return false;
  });

  $("#perma_link").click(function() {
    msg_box.set_width(650);
    msg_box.set_title("Link to this Search");
    msg_box.set_content("<p class='permaLinkDialogText'>Copy the following URL to link directly to these search results.</p><input id='perma_link_input' style='width:600px' type='text' value='" + state_machine.get_perma_link() + "'>");
    msg_box.open_message_box();
  });

  $("#rss_link").click(function() {
    window.location = state_machine.get_rss_link();
    return false;
  });


  $("#bathrooms").change(function() { 
    state_machine.set_bathrooms($(this).val()); 
    get_new_results();
  });


  $("#allow_cats").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_allow_cats("t");
    } else {
      state_machine.set_allow_cats("f");
    }
    get_new_results();
  });

  $("#allow_small_dogs").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_allow_small_dogs("t");
    } else {
      state_machine.set_allow_small_dogs("f");
    }
    get_new_results();
  });

  $("#allow_large_dogs").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_allow_large_dogs("t");
    } else {
      state_machine.set_allow_large_dogs("f");
    }
    get_new_results();
  });



  $("#is_furnished").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_furnished("t");
    } else {
      state_machine.set_is_furnished("f");
    }
    get_new_results();
  });

  $("#is_short_term").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_short_term("t");
    } else {
      state_machine.set_is_short_term("f");
    }
    get_new_results();
  });

  $("#is_senior").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_senior("t");
    } else {
      state_machine.set_is_senior("f");
    }
    get_new_results();
  });

  $("#is_corporate").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_corporate("t");
    } else {
      state_machine.set_is_corporate("f");
    }
    get_new_results();
  });

  $("#is_house").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_house("t"); 
    } else {
      state_machine.set_is_house("f");
    }
    get_new_results();
  });
  
  $("#is_condo").click(function() {
    if($(this).is(":checked")) {
      state_machine.set_is_condo("t");
    } else {
      state_machine.set_is_condo("f");
    }
    get_new_results();
  });


  $("#search_radius").change(function() {
    if($(this).val() == -2) {
      state_machine.set_search_radius($(this).val());
      var location_type = state_machine.get_base_location_type();
      if(location_type != "zip_code") {
        state_machine.add_limit_city_base();
      } else {
        state_machine.limit_to_base_zip_code();
      }
    } else if($(this).val() == -1) {
      alert("LIMIT TO MAP");
    } else {
      clear_all_limits();
      state_machine.set_search_radius($(this).val());
      set_search_radius_select(state_machine.get_get_data()["search_radius"]); 
    }
    get_new_results();
  });


  $("#sort_method").change(function() {
    state_machine.set_sort_method($(this).val());
    get_new_results();
  });


  $("#min_square_feet").change(function() {
    state_machine.set_min_square_feet($(this).val());
    if(typeof state_machine.get_get_data()['max_square_feet'] == 'undefined') {
      state_machine.set_max_square_feet($("#max_square_feet option:last").val());
    } else {
      if(parseInt(state_machine.get_get_data()['max_square_feet']) < parseInt($(this).val())) {
        state_machine.set_max_square_feet($("#max_square_feet option:last").val());
        $("#max_square_feet option:last").attr("selected", true);
      }
    }
    get_new_results();
  });

  $("#max_square_feet").change(function() {
    state_machine.set_max_square_feet($(this).val());
    if(typeof state_machine.get_get_data()['min_square_feet'] == 'undefined') {
      state_machine.set_min_square_feet($("#min_square_feet option:first").val());
    } else {
      if(parseInt(state_machine.get_get_data()['min_square_feet']) > parseInt($(this).val())) {
        state_machine.set_min_square_feet($("#min_square_feet option:first").val());
        $("#min_square_feet option:first").attr("selected", true);
      }
    }
    get_new_results();
  });


  $("#bedrooms").change(function() {
    if($(this).val() == -1) {
      state_machine.clear_bedrooms();
    } else {
      var val = $(this).val();
      var tmp = val.split("_");

      state_machine.set_min_beds(tmp[0]);
      if(tmp[1] == "+") { 
        state_machine.set_max_beds(9999);
      } else {
        state_machine.set_max_beds(tmp[0]);
      }
    }
    get_new_results();
  });

  $("#min_rent").change(function() {
    state_machine.set_min_rent($(this).val());
    if(typeof state_machine.get_get_data()['max_rent'] == 'undefined') {
      state_machine.set_max_rent($("#max_rent option:last").val());
    } else {
      if(parseInt(state_machine.get_get_data()['max_rent']) <= parseInt($(this).val())) {
        state_machine.set_max_rent($("#max_rent option:last").val());
        $("#max_rent option:last").attr("selected", true);
      }
    }
    get_new_results();
  });

  $("#max_rent").change(function() {
    state_machine.set_max_rent($(this).val());
    if(typeof state_machine.get_get_data()['min_rent'] == 'undefined') {
      state_machine.set_min_rent($("#min_rent option:first").val());
    } else {
      if(parseInt(state_machine.get_get_data()['min_rent']) >= parseInt($(this).val())) {
        state_machine.set_min_rent($("#min_rent option:first").val());
        $("#min_rent option:first").attr("selected", true);
      }
    }
    get_new_results();
  });
});

function Filter() {
  this.add_close_cities_html = add_close_cities_html;
  this.add_neighborhoods_html = add_neighborhoods_html;
  this.remove_city = remove_city;
  this.remove_neighborhood = remove_neighborhood;
  this.remove_amenity = remove_amenity;
  this.set_search_radius_select = set_search_radius_select;
  this.set_min_rent_select = set_min_rent_select;
  this.set_max_rent_select = set_max_rent_select;
  this.set_beds_select = set_beds_select;
  this.set_bathrooms_select = set_bathrooms_select;
  this.set_min_square_feet_select = set_min_square_feet_select;
  this.set_max_square_feet_select = set_max_square_feet_select;
  this.set_checkbox = set_checkbox;
  this.add_amenities_html = add_amenities_html;
  this.set_sort_method_select = set_sort_method_select;
  this.reset = reset;
}

function reset() {
  var url = state_machine.get_default_url();
  window.location = url;
}

function get_new_results() {
  state_machine.set_page_num(1);
  list_properties_ajax.get_properties_list(); 
}

function set_checkbox(_element_id,_val) {
  if(typeof _val != 'undefined') {
    if(_val == "t") { 
      $("#" + _element_id).attr("checked", true);
    } else {
      $("#" + _element_id).attr("checked", false);
    }
  } else {
    $("#" + _element_id).attr("checked", false);
  }
}

function set_sort_method_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#sort_method option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#sort_method option:first").attr("selected", true); }
}

function set_min_square_feet_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#min_square_feet option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#min_square_feet option:first").attr("selected", true); }
}

function set_max_square_feet_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#max_square_feet option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#max_square_feet option:last").attr("selected", true); }
}



function set_bathrooms_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#bathrooms option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#bathrooms option:first").attr("selected", true); }
}

function set_beds_select(_min,_max) {
  var found = false;
  if(typeof _min != 'undefined' && typeof _max != 'undefined') {

    if(_min == _max) { _val = _min + "_" + _max; }
    else { _val = _min + "_+"; }

    $("#bedrooms option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#min_beds option:first").attr("selected", true); }
}

function set_min_rent_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#min_rent option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#min_rent option:first").attr("selected", true); }
}

function set_max_rent_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#max_rent option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };  
  if(found == false) { $("#max_rent option:last").attr("selected", true); }
}

function add_amenities_html() {
  var amenities_data = state_machine.get_amenities_data();
  var amenities_html = "";

  for(i=0;i<amenities_data.length;i++) {
    amenities_html = amenities_html + "<div id='amenity_descriptor_" + amenities_data[i]['id'] + "' style='margin-top:3px;'><div style='float:left;'>" + amenities_data[i]['text'] + "</div><div style='float:left;margin-top:3px;margin-left:4px;'><a href='#' onclick='remove_single_amenity(" + amenities_data[i]['id'] + "); return false;'><img style='display:block;' src='" + state_machine.get_static_url() + "images/filter_close.png'></a></div><div style='clear:both;'></div></div>";
  }
  $("#amenities_descriptor").html(amenities_html);
}


function add_neighborhoods_html() {
  var neighborhoods_data = state_machine.get_limit_neighborhoods_data();
  var neighborhoods_html = "";

  for(i=0;i<neighborhoods_data.length;i++) {
    neighborhoods_html = neighborhoods_html + "<div id='neighborhood_descriptor_" + neighborhoods_data[i]['id'] + "' style='margin-top:3px;'><div style='float:left;'>" + neighborhoods_data[i]['text'] + "</div><div style='float:left;margin-top:3px;margin-left:4px;'><a href='#' onclick='remove_neighborhood(" + neighborhoods_data[i]['id'] + "); return false;'><img style='display:block;' src='" + state_machine.get_static_url() + "images/filter_close.png'></a></div><div style='clear:both;'></div></div>";
  }
  $("#neighborhoods_descriptor").html(neighborhoods_html);
}

function add_close_cities_html() {
  var cities_data = state_machine.get_limit_cities_data();
  var cities_html = "";

  for(i=0;i<cities_data.length;i++) {
    cities_html = cities_html + "<div id='city_descriptor_" + cities_data[i]['id'] + "' style='margin-top:3px;'><div style='float:left;'>" + cities_data[i]['text'] + "</div><div style='float:left;margin-top:3px;margin-left:4px;'><a href='#' onclick='remove_city(" + cities_data[i]['id'] + "); return false;'><img style='display:block;' src='" + state_machine.get_static_url() + "images/filter_close.png'></a></div><div style='clear:both;'></div></div>";
  }
  $("#close_cities_descriptor").html(cities_html);
}

function remove_single_amenity(amenity_id) {
  state_machine.remove_amenity(amenity_id);
  $("#amenity_descriptor_" + amenity_id).html("");
  get_new_results();
}

function remove_city(city_id) {
  state_machine.remove_limit_city(city_id);
  $("#city_descriptor_" + city_id).html(""); 
  set_search_radius_select(state_machine.get_get_data()["search_radius"]);
  get_new_results();
}

function remove_neighborhood(neighborhood_id) {
  state_machine.remove_limit_neighborhood(neighborhood_id);
  $("#neighborhood_descriptor_" + neighborhood_id).html("");
  set_search_radius_select(state_machine.get_get_data()["search_radius"]);
  get_new_results();
}

function set_search_radius_value(_val) {
  $("#search_radius option").each(function() {
    if($(this).val() == _val) {
     $(this).attr("selected", true);
    }
  });
}

function set_search_radius_location_limits() {
  $("#search_radius option").each(function() {
    if($(this).val() == "-2") {
     $(this).attr("selected", true);
    }
  });
}

function clear_all_limits() {
  state_machine.clear_limit_zip_code();
  state_machine.clear_limit_cities();
  state_machine.clear_limit_neighborhoods();

  $('#neighborhoods_descriptor').html('');
  $('#close_cities_descriptor').html('');
}


function set_search_radius_select(_val) {

  var limit_cities_length = state_machine.get_limit_cities().length;
  var limit_neighborhoods_length = state_machine.get_limit_neighborhoods().length;

  if(state_machine.get_base_location_type() != "zip_code") {
    if(limit_cities_length == 0 && limit_neighborhoods_length == 0) {
      set_search_radius_value(_val);
    } else {
      set_search_radius_location_limits();
    }
  } else {
    if(typeof state_machine.get_get_data()["limit_zip_codes"] == 'undefined') {
      set_search_radius_value(_val); 
    } else {
      set_search_radius_location_limits();
    }
  }
}
