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

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

      window.dialog.parent()
        .attr("id", "settings-window")

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

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

            window.dialog.parent().find("*").remove();
          })
        .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 settings
      this.updateWindow = function (settings) {
        window.dialog
          .find("table")
            .remove()
          .end()

          .append(settings)

          .find(".menu a")
            .click(function (event) {
              event.preventDefault();

              var panel = $(this).attr("rel");
              $(".panel li").each(function () {
                if ($(this).hasClass(panel)) {
                  $(this).css("top", 6);
                } else {
                  $(this).css("top", -9999);
                }
              })

              $(".menu a").each(function () {
                if (panel == $(this).attr("rel")) {
                  $(this).addClass("active");
                } else {
                  $(this).removeClass("active");
                }
              });
            })
          .end()

          .find("a:first")
            .click()
          .end()

          // Remove blocked users
          .find("#remove-blocked")
            .click(function () {
              $("#blocked :selected").each(function(i, selected) {

	              window.dialog.find(".blocked-users-cell")
	               .append("<input type='hidden' name='remove[]' value=" + $(selected).val() + " />");


	              $(selected).remove();
              });
            })
          .end()

          // Cancel button
          .find(".btn-cancel")
            .click(function (event) {
              event.preventDefault();

              window.dialog.parent().find(".ui-icon-closethick").click();
            })
          .end()

          // Save button
          .find(".btn-save")
            .click(function (event) {
              event.preventDefault();

              var button = $(this);
              if (button.hasClass("loading"))
              {
                return false;
              }

              button.addClass("loading");

              // Change show_offline
              chatfactory_show_offline = $("#show_offline").attr("checked") ? 1 : 0;

              var serialized = window.dialog.find("#options-form").serialize();
              $.ajax({
                type: "POST",
                url:  chatfactory_root + "index.php?option=com_chatfactory&controller=user&task=saveSettings",
                data: serialized,
                success: function (response) {
                  button.removeClass("loading");
                }
              });
            })
          .end()

          .find("#progressbar")
            .progressbar({ value: 50 })
          .end()

          .find('#swfupload-control')
            .swfupload({
              upload_url:             chatfactory_root + "index.php?option=com_chatfactory&controller=user&task=uploadavatar",
              file_size_limit:        "10240000",
              file_types:             "*.jpg;*.gif;*.png;",
              file_types_description: "Images",
              file_upload_limit:      "0",
              file_queue_limit:       "0",
              file_post_name:         "file",
              flash_url:              chatfactory_root + "components/com_chatfactory/assets/swfs/swfupload.swf",
              button_width:           100,
              button_height:          20,
              button_text:            '<span class="font">' + txt_upload + '</span>',
              button_text_style:      ".font { font-size: 12px; font-family: Helvetica,Arial,sans-serif; font-weight: normal; background-color: #ececec; }",
              button_placeholder:     $('#button')[0],
              debug:                  false,
              button_window_mode:     SWFUpload.WINDOW_MODE.TRANSPARENT,
              button_cursor:          SWFUpload.CURSOR.HAND,
              post_params:            { session: chatfactory_session }
            })
            .bind('fileQueued', function(event, file) {
              $("#upload-response").fadeOut();
              $(this).swfupload('startUpload');
            })
            .bind('uploadStart', function(event, file){
              $('#progressbar').progressbar("option", "value", 0).fadeIn(100);
            })
            .bind('uploadProgress', function(event, file, bytesLoaded){
              $('#progressbar').progressbar("option", "value", Math.floor(bytesLoaded * 100 / file.size));
            })
            .bind('uploadSuccess', function(event, file, serverData){
              try
              {
                var response = eval('(' + serverData + ')');
              }
              catch (e)
              {
                $("#upload-response").html(txt_error_occurred).fadeIn();
                return false;
              }

              if (0 == response.status)
              {
                $("#upload-response").html(response.error).fadeIn();
                return false;
              }

              $("#current-avatar").attr("src", response.src);
            })
            .bind('uploadComplete', function(event, file){
              $("#progressbar").fadeOut(100);
            })
          .end()
      }

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