var persistedAjaxResult;

var AjaxCommands = (function () {
  var showWidgetProgress = function (elementToUpdate) {
    if (elementToUpdate == "" || elementToUpdate == "ModalContent")
      return;
    var widget = $("#" + elementToUpdate + " div");
    if (widget.length == 0)
      widget = $("#" + elementToUpdate);
    $(widget[1]).html("<div class=\"loading\"></div>");
    $(widget[1]).slideDown(UIConstants.speed);
  };

  var handleSuccess = function (elementToUpdate, result) {
    if (elementToUpdate == "" || result == "")
      return;
    var target = $("#" + elementToUpdate);
    target.hide();
    target.html($(result));
    target.show();
    UIDisplay.setWidgetContainerScroll(elementToUpdate);
    UIState.update();
  };

  var execute = function (ajaxOptions, elementToUpdate, callback) {
    persistedAjaxResult = null;
    $.ajax(
				    { async: ajaxOptions.async != undefined ? ajaxOptions.async : true,
				      cache: false,
				      type: ajaxOptions.type,
				      url: ajaxOptions.url,
				      contentType: ajaxOptions.contentType,
				      data: ajaxOptions.data,
				      dataType: ajaxOptions.dataType,
				      completed: showWidgetProgress(elementToUpdate),
				      success: function (result) {
				        if (ajaxOptions.dataType != undefined && ajaxOptions.dataType != "json")
				          handleSuccess(elementToUpdate, result);
				        if (callback) {
				          persistedAjaxResult = result;
				          callback(result);
				        }
				        $(document).trigger("session-activity");
				      },
				      error: function (xhr, ajaxRequestOptions, thrownError) {
				        $(document).trigger("session-activity");
				        handleSuccess(elementToUpdate, xhr.statusText + "<br />" + thrownError);
				      }
				    });
  };

  return {
    toggleProgress: function (show) {
      (show) ? $("#SpinnerGrayOut").show() : $("#SpinnerGrayOut").hide();
      (show) ? $("#updateProgress").show() : $("#updateProgress").hide();
    },
    getJson: function (json, controller, action, elementToUpdate, callback) {
      execute(
        { async: true,
          dataType: "json",
          type: "POST",
          url: "/" + controller + "/" + action,
          data: JSON.stringify(json),
          contentType: "application/json; charset=utf-8"
        },
        elementToUpdate,
        callback);
    },
    submitJson: function (json, controller, action, elementToUpdate, callback) {
      execute(
        { async: true,
          dataType: "html",
          type: "POST",
          url: "/" + controller + "/" + action,
          data: JSON.stringify(json),
          contentType: "application/json; charset=utf-8"
        },
        elementToUpdate,
        callback);
    },
    submitGet: function (requestedUrl, elementToUpdate, callback) {
      execute(
        { async: true,
          dataType: "html",
          type: "GET",
          url: requestedUrl,
          data: null,
          contentType: "application/x-www-form-urlencoded"
        },
        elementToUpdate,
        callback);
    },
    submitForm: function (form, elementToUpdate, callback) {
      execute(
      { async: false,
        dataType: "html",
        type: form.method,
        url: form.action,
        data: $(form).serialize(),
        contentType: "application/x-www-form-urlencoded"
      },
      elementToUpdate,
      callback);
    },
    submitHiddenForm: function (form, controller, action, elementToUpdate, callback) {
      execute(
        { async: false,
          dataType: "html",
          type: form.method,
          url: "/" + controller + "/" + action,
          data: $(form).serialize(),
          contentType: "application/x-www-form-urlencoded"
        },
        elementToUpdate,
        callback);
    }
  };
})();

