$(document).ready(function () {

  set_advanced_filter(state_machine.get_get_data()["show_advanced"])

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

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

  set_property_type_select(state_machine.get_get_data()["property_type"]);

  set_min_price_select(state_machine.get_get_data()["min_price"]);
  set_max_price_select(state_machine.get_get_data()["max_price"]);

  set_beds_select(state_machine.get_get_data()["min_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_min_lot_size_select(state_machine.get_get_data()["min_lot_size"]);
  set_max_lot_size_select(state_machine.get_get_data()["max_lot_size"]);

  set_min_year_built_select(state_machine.get_get_data()["min_year_built"]);
  set_max_year_built_select(state_machine.get_get_data()["max_year_built"]);

  set_checkbox('water_front',state_machine.get_get_data()["water_front"]);
  set_checkbox('water_view',state_machine.get_get_data()["water_view"]);
  set_checkbox('water_access',state_machine.get_get_data()["water_access"]);

  //EVENT HANDLERS
  $("#water_front").click(function() {
    if ($(this).attr('checked') == true) { 
      state_machine.set_water_front();
    } else {
      state_machine.clear_water_front();
    }
    get_new_results();
  });

  $("#water_view").click(function() {
    if ($(this).attr('checked') == true) {
      state_machine.set_water_view();
    } else {
      state_machine.clear_water_view();
    }
    get_new_results();
  });

  $("#water_access").click(function() {
    if ($(this).attr('checked') == true) {
      state_machine.set_water_access();
    } else {
      state_machine.clear_water_access();
    }
    get_new_results();
  });

  $("#hide_options_a").click(function() {
    state_machine.set_show_advanced(false);

    $(this).addClass('hidden');
    $("#show_options_a").removeClass('hidden');
    $('#advanced_options').removeClass('advanced');
    $('#advanced_options').addClass('hidden');
    return false;
  });

  $("#show_options_a").click(function() {
    state_machine.set_show_advanced(true);

    $(this).addClass('hidden');
    $("#hide_options_a").removeClass('hidden');
    $('#advanced_options').removeClass('hidden');
    $('#advanced_options').addClass('advanced');
    return false;
  });

  $("#city_select").change(function() {
    $("#search_form").submit();
    return false;
  });

  $("#property_type").change(function() {
    if($(this).val() == "-1") {
      state_machine.clear_property_type();
    } else {
      state_machine.set_property_type($(this).val());
    }
    get_new_results();
  });

  $("#bathrooms").change(function() { 
    if($(this).val() == "-1") {
      state_machine.clear_bathrooms();
    } else {
      state_machine.set_bathrooms($(this).val()); 
    }
    get_new_results();
  });

  $("#search_radius").change(function() {
    if($(this).val() == -1) {
      state_machine.clear_search_radius();
    } else {
      state_machine.clear_limit_cities();
      state_machine.set_search_radius($(this).val());
    }
    get_new_results();
  });


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


  $("#min_square_feet").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_min_square_feet();
    } else {
      state_machine.set_min_square_feet($(this).val());
    }
    get_new_results();
  });

  $("#max_square_feet").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_max_square_feet();
    } else {
      state_machine.set_max_square_feet($(this).val());
    }
    get_new_results();
  });

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

  $("#min_price").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_min_price();
    } else {
      state_machine.set_min_price($(this).val());
    }
    get_new_results();
  });

  $("#max_price").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_max_price();
    } else {
      state_machine.set_max_price($(this).val());
    }
    get_new_results();
  });


  $("#min_lot_size").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_min_lot_size();
    } else {
      state_machine.set_min_lot_size($(this).val());
    }
    get_new_results();
  });

  $("#max_lot_size").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_max_lot_size();
    } else {
      state_machine.set_max_lot_size($(this).val());
    }
    get_new_results();
  });

  $("#min_year_built").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_min_year_built();
    } else {
      state_machine.set_min_year_built($(this).val());
    }
    get_new_results();
  });

  $("#max_year_built").change(function() {
    if(parseInt($(this).val()) == -1) {
      state_machine.clear_max_year_built();
    } else {
      state_machine.set_max_year_built($(this).val());
    }
    get_new_results();
  });


});

function Filter() {
  this.set_search_radius_select = set_search_radius_select;
  this.set_advanced_filter = set_advanced_filter;
  this.set_property_type_select = set_property_type_select;
  this.set_min_price_select = set_min_price_select;
  this.set_max_price_select = set_max_price_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_min_lot_size_select = set_min_lot_size_select;
  this.set_max_lot_size_select = set_max_lot_size_select;
  this.set_min_year_built_select = set_min_year_built_select;
  this.set_max_year_built_select = set_max_year_built_select;

  this.set_checkbox = set_checkbox;
  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_property_type_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#property_type option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#property_type option:first").attr("selected", true); }
}

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_lot_size_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#min_lot_size option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#min_lot_size option:first").attr("selected", true); }
}

function set_max_lot_size_select(_val) {
  var found = false;
  if(typeof _val != 'undefined') {
    $("#max_lot_size option").each(function() {
      if($(this).val() == _val) {
        found = true;
        $(this).attr("selected", true);
      }
    });
  };
  if(found == false) { $("#max_lot_size 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:first").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) {
  var found = false;
  if(typeof _min != 'undefined') {

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

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

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


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

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


function set_advanced_filter(_val) {
  if(typeof _val != 'undefined') {
    if(_val == "t") {
      $(this).addClass('hidden');
      $("#hide_options_a").removeClass('hidden');
      $("#show_options_a").addClass('hidden');
      $('#advanced_options').removeClass('hidden');
      $('#advanced_options').addClass('advanced');
    }
  }
}

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

