$(document).ready(function () {

  //Dialog for cities near, Open on click
  $("#save_search").click(function() {
    if(!state_machine.get_is_authenticated()) {
      msg_box.set_width(300);
      msg_box.set_title("You must register.");
      msg_box.set_content("You must <a class='dialogLink' href='/accounts/register/'>Register</a> to save searches or apartments!");
      msg_box.open_message_box();
    } else {
      $('#save_search_dialog').dialog('open');
    }
    return false;
  });

  //Open and setup cities near dialog
  $("#save_search_dialog").dialog({
    autoOpen: false,
    bgiframe: true,
    width:400,
    open: function() { return; },
    buttons: {
      'Save': function() {
        $.post("/process/save_search/",
        {
          save_search_title : $("#saved_search_title").val(),
          save_search_notes : $("#saved_search_notes").val(),
          save_search_url : state_machine.get_perma_link()
        },
        function(data){
          $("#save_search").html("Saved!");
          return;
        },
          "text"
        );

        $(this).dialog('close');
      },
      Cancel: function() {
        $(this).dialog('close');
      }
    },
    close: function() {
      return;
    }
  });
});
