
  WF.Messages = {
    init: function() {
      return WF.Router.addMultiple("dialog", {
        compose_message: this.compose_message,
        delete_message: this.delete_message,
        reply_to_message: this.reply_to_message
      });
    },
    compose_message: function(username) {
      var get_url, self, target_subject, url;
      self = this;
      url = "/messages/compose/";
      if (!WF.GLOBAL.username) {
        return new WF.Dialog({
          title: "You Must Be Signed in to Send a Message",
          content: "<p>Whoops! You're not signed in. Please sign in to send a message to the owner of this campaign.</p>",
          actions: [new WF.DialogURLButton("Sign In", WF.Core.login_current_url()), new WF.DialogURLButton("Join", WF.Core.register_current_url())]
        });
      } else {
        get_url = url;
        target_subject = false;
        if (username) {
          target_subject = true;
          get_url = url + "?username=" + username;
        }
        return new WF.Dialog({
          title: "Compose Message",
          content: get_url,
          actions: [
            new WF.DialogButton("Send Message", function() {
              var dialog, form;
              dialog = this;
              form = this.dialog_content.find("form");
              return jQuery.ajax({
                type: "POST",
                url: url,
                data: {
                  recipient: form.find("#id_recipient").val(),
                  subject: form.find("#id_subject").val(),
                  message: form.find("#id_message").val(),
                  csrfmiddlewaretoken: WF.GLOBAL.csrf
                },
                dataType: "json",
                beforeSend: function() {
                  return dialog.close();
                },
                success: function(r) {
                  if (r.response === "success") {
                    return new WF.Alert("success", r.extra.message);
                  } else {
                    return new WF.Alert("error", r.extra.message);
                  }
                },
                error: function(r) {
                  return new WF.Alert("error", 'An error occurred while sending your message. Please try again.');
                }
              });
            }), new WF.DialogButton("Cancel", WF.Dialog.CANCEL_BUTTON)
          ],
          callback: function() {
            var recipient_searcher;
            if (target_subject) {
              $("#id_subject").focus();
              $("#id_recipient").parent().parent().hide();
            } else {
              $("#id_recipient").focus();
            }
            recipient_searcher = new WF.SelectSearcher("recipient", {
              items: WF.GLOBAL.search.users,
              field: "query",
              name_attr: "username",
              desc_attr: "name"
            });
            return recipient_searcher.getSelectTarget = function() {
              self = this;
              return $("#id_recipient");
            };
          }
        });
      }
    },
    delete_message: function(url) {
      return new WF.Dialog({
        title: "Delete Message",
        content: ["<p>Are you sure that you want to delete this message?</p>"],
        actions: [new WF.DialogURLButton("Yes, Delete", url), new WF.DialogButton("No, Cancel", WF.Dialog.CANCEL_BUTTON)]
      });
    },
    reply_to_message: function(message_id) {
      var self, url;
      self = this;
      url = "/messages/" + message_id + "/reply/";
      return new WF.Dialog({
        title: "Reply to Message",
        content: url,
        actions: [
          new WF.DialogButton("Send Message", function() {
            var dialog, form;
            dialog = this;
            form = this.dialog_content.find("form");
            return jQuery.ajax({
              type: "POST",
              url: url,
              data: {
                recipient: form.find("#id_recipient").val(),
                subject: form.find("#id_subject").val(),
                message: form.find("#id_message").val(),
                csrfmiddlewaretoken: WF.GLOBAL.csrf
              },
              dataType: "json",
              complete: function() {
                return dialog.close();
              },
              success: function(r) {
                if (r.response === "success") {
                  return new WF.Alert("success", r.extra.message);
                } else {
                  return new WF.Alert("error", r.extra.message);
                }
              },
              error: function(r) {
                return console.log(r);
              }
            });
          }), new WF.DialogButton("Cancel", WF.Dialog.CANCEL_BUTTON)
        ],
        callback: function() {
          return $("#id_message").focus();
        }
      });
    }
  };

