(function ($) {
  $.extend({
    archiveWindow: function () {
      var window = this;
      var request = null;

      window.dialog = $("<div></div>").dialog({
        resizable: true,
        height: 400,
        width: 600,
        resizable: false
      });

      window.dialog.parent()
        .attr("id", "cf-archive-window")

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

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

            window.dialog.parent().find("*").remove();
            chatfactory_mainWindow.closeArchiveWindow();
          })
        .end()

        .find(".ui-dialog-content")
          .append("<table style='height: 100%; width: 100%;'><tr><td style='text-align: center;'>" + txt_loading_please_wait + "<br /><!--<img src='loading-animation.gif' />--></td></tr></table>")
        .end()

      // Updates the window and loads the archive
      this.updateWindow = function (archive) {
        window.dialog
          // Remove loading message
          .find("table")
            .remove()
          .end()

          // Append archive layout
          .append(archive)

          // Users list
          .find(".cf-users-list a").click(function(event) {
            event.preventDefault();

            window.dialog.find(".cf-users-list a.cf-active").removeClass("cf-active");
            $(this).addClass("cf-active");

            if (null != request)
            {
              request.abort();
            }

            var dates = $(".cf-dates-list");
            dates.html("");

            request = $.post(
              chatfactory_root + "index.php?option=com_chatfactory&controller=archive&task=getdates",
              { user_id: $(this).attr("rel") },
              function (response) {
                for (var i = 0, count = response.dates.length; i < count; i++)
                {
                  var date = response.dates[i];
                  dates.append("<li><a href='#' rel='" + date.date + "'>" + date.date + "</a></li>");

                }

                window.bindDateClick();
              }, "json");
          })
          .end();
      }

      this.bindDateClick = function () {
        window.dialog
          .find(".cf-dates-list a").click(function (event) {
            event.preventDefault();

            window.dialog.find(".cf-dates-list a.cf-active").removeClass("cf-active");
            $(this).addClass("cf-active");

            if (null != request)
            {
              request.abort();
            }

            var user_id      = window.dialog.find(".cf-users-list a.cf-active").attr("rel");
            var date         = $(this).attr("rel");
            var conversation = $(".cf-conversation");

            conversation = conversation.html("<table></table>").find("table");

            request = $.post(
              chatfactory_root + "index.php?option=com_chatfactory&controller=archive&task=getconversation",
              { user_id: user_id, date: date },
              function (response) {
                for (var i = 0, count = response.conversation.length; i < count; i++)
                {
                  var message = response.conversation[i];

                  conversation.append("<tr><td class='" + ("1" == message.highlight ? "cf-highlight" : "") + "'><b>" + message.sender_username + ":</b></td><td>" + message.text + "</td></tr>");
                }
              }, "json");
          });
      }

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