(function ($) {
  $.extend({
    requestWindow: function (options) {
      var window = this;

      window.dialog = $("<div></div>").dialog({
        height: 70,
        width: 300,
        closeOnEscape: false,
        resizable: false
      });

      window.dialog.parent()
        .attr("id", "request-" + options.sender_id)
        .addClass("request-window")

        .find(".ui-dialog-title")
          .html(txt_friendship_request)
        .end()

        .find(".ui-icon-closethick")
          .click(function (event) {
            event.preventDefault();
            event.stopPropagation();

            window.dialog.parent().find("*").remove();

            chatfactory_requests[options.sender_id] = undefined;
          })
        .end()

        .find(".ui-dialog-content")
          .append(options.username + " " + txt_request_text)
          .append("<div style='margin-top: 10px;'><a href='#' class='button btn-save'>accept</a><a href='#' class='button btn-cancel'>reject</a></div>")

          .find('.btn-save')
            .click(function (event) {
              event.preventDefault();

              var link = $(this);

              if (link.hasClass("loading"))
              {
                return false;
              }

              link.addClass("loading");

              $.post(
                chatfactory_root + 'index.php?option=com_chatfactory&controller=user&task=acceptfriend',
                { sender_id: options.sender_id}, function (response) {
                  window.dialog.parent().find(".ui-icon-closethick").click();
              }, "json");
            })
          .end()

          .find('.btn-cancel')
            .click(function (event) {
              event.preventDefault();

              var link = $(this);

              if (link.hasClass("loading"))
              {
                return false;
              }

              link.addClass("loading");

              $.post(
                chatfactory_root + 'index.php?option=com_chatfactory&controller=user&task=rejectfriend',
                { sender_id: options.sender_id}, function (response) {
                  window.dialog.parent().find(".ui-icon-closethick").click();
              }, "json");
            })
          .end()

        .end()

      return window;
    }
  })
})(jQueryFactory);
