function AdfDhtmlZOrderManager() {
  this.Init();
}
AdfObject.createSubclass(AdfDhtmlZOrderManager);
AdfDhtmlZOrderManager.InitClass = function() {
  var LAYERIDS = ["__af_Z_maskingframe", "__af_Z_window", "__af_Z_tooltip"];
  this.LAYERCONTAINER = "DhtmlZOrderManagerLayerContainer";
  this.FLOATINGTYPE_WINDOW = LAYERIDS[1];
  this.FLOATINGTYPE_TOOLTIP = LAYERIDS[2];
  this.FLOATINGTYPE_MASKINGFRAME = LAYERIDS[0];
  this.LAYERIDS = LAYERIDS;
  this._LAYER_CONTAINER_STYLECLASS = "AFZOrderLayerContainer";
  this.LAYER_STYLECLASS = "AFZOrderLayer";
  this._MAXZ = Math.pow(2, 31) - 2;
  this._SHADOWDECORATORZINDEX = 3;
  this._BLANK_PAGE_RESOURCE = "about:blank";
  this._FORM_ID_EXPANDO = "data-afr-fid";
  this._INUSE_EXPANDO = "data-afr-inuse";
};
AdfDhtmlZOrderManager.prototype.Init = function() {
  AdfDhtmlZOrderManager.superclass.Init.call(this);
  this._layerHash = {};
  this._isGecko = AdfAgent.AGENT.getPlatform() == AdfAgent.GECKO_PLATFORM;
};
AdfDhtmlZOrderManager.getMaxZIndex = function() {
  return AdfDhtmlZOrderManager._MAXZ;
};
AdfDhtmlZOrderManager.prototype.dispose = function() {
  this._layerHash = null;
};
AdfDhtmlZOrderManager.prototype.addElementToLayer = function(element, layerId, priorElement, formId) {
  this._assertLayers(formId);
  var id = formId ? AdfRichUIPeer.createSubId(formId, layerId) : layerId;
  var layer = this._getLayer(id);
  layer.addElement(element, priorElement);
};
AdfDhtmlZOrderManager.prototype.raiseToFront = function(element) {
  var layerDom = element.parentNode;
  var containerDom = layerDom.parentNode;
  var formId = containerDom.getAttribute(AdfDhtmlZOrderManager._FORM_ID_EXPANDO);
  this._assertLayers(formId);
  var layerId = layerDom.getAttribute("id");
  AdfAssert.assertNonEmptyString(layerId);
  var layer = this._getLayer(layerId);
  if (layer) {
    layer.raiseToFront(element);
  }
  AdfPopupScopingUtils.scope(element);
};
AdfDhtmlZOrderManager._createMapSafeKey = function(layerId) {
  AdfAssert.assertString(layerId);
  var tokens = layerId.split(":");
  return tokens.join("$") + "$key";
};
AdfDhtmlZOrderManager.prototype._getLayer = function(id) {
  var key = AdfDhtmlZOrderManager._createMapSafeKey(id);
  var layer = this._layerHash[key];
  if (layer) {
    var containerElement = layer.getContainerElement();
    if (!containerElement) {
      delete this._layerHash[key];
      return null;
    }
  }
  return layer;
};
AdfDhtmlZOrderManager.prototype._setLayer = function(id, layer) {
  var key = AdfDhtmlZOrderManager._createMapSafeKey(id);
  this._layerHash[key] = layer;
};
AdfDhtmlZOrderManager.prototype._createLayers = function(formId) {
  var constLayerIds = AdfDhtmlZOrderManager.LAYERIDS;
  var constLayerContainer = AdfDhtmlZOrderManager.LAYERCONTAINER;
  var layerContainerId = formId ? AdfRichUIPeer.createSubId(formId, constLayerContainer) : constLayerContainer;
  var layerContainer = this._createLayerElement(layerContainerId);
  AdfDomUtils.addCSSClassName(layerContainer, AdfDhtmlZOrderManager._LAYER_CONTAINER_STYLECLASS);
  if (formId) {
    layerContainer.setAttribute(AdfDhtmlZOrderManager._FORM_ID_EXPANDO, formId);
  }
  var constLayerStyleClass = AdfDhtmlZOrderManager.LAYER_STYLECLASS;
  for (var i = 0;i < constLayerIds.length;i++) {
    var id = formId ? AdfRichUIPeer.createSubId(formId, constLayerIds[i]) : constLayerIds[i];
    var layerElement = this._createLayerElement(id);
    var layer = new AdfDhtmlZOrderLayer(id);
    this._addLayer(layerContainer, layer, layerElement);
    AdfDomUtils.addCSSClassName(layerElement, constLayerStyleClass);
  }
  this._positionLayerContainer(layerContainer);
  return layerContainer;
};
AdfDhtmlZOrderManager.prototype._createLayerElement = function(id) {
  var existingLayer = this._getLayer(id);
  if (existingLayer) {
    return existingLayer.getContainerElement();
  }
  var domDocument = AdfPage.PAGE.getDomDocument();
  var layer = domDocument.createElement("div");
  layer.setAttribute("id", id);
  layer.style.zIndex = AdfDhtmlZOrderManager._MAXZ;
  return layer;
};
AdfDhtmlZOrderManager.prototype._addLayer = function(layerContainer, layer, layerElement) {
  var id = layer.getId();
  this._setLayer(id, layer);
  layerContainer.appendChild(layerElement);
  AdfAgent.AGENT.elementsAdded(layerContainer);
};
AdfDhtmlZOrderManager.prototype._assertLayers = function(formId) {
  var layerContainer = this._getLayerContainer(formId);
  if (!layerContainer) {
    layerContainer = this._createLayers(formId);
  }
  if (AdfDhtmlZOrderManager._getFormOrBody(formId).lastChild != layerContainer) {
    this._repairLayers(layerContainer);
  }
};
AdfDhtmlZOrderManager.prototype._repairLayers = function(layerContainer) {
  var formId = layerContainer.getAttribute(AdfDhtmlZOrderManager._FORM_ID_EXPANDO);
  var documentForm = AdfDhtmlZOrderManager._getFormOrBody(formId);
  if (layerContainer.parentNode != documentForm) {
    this._positionLayerContainer(layerContainer);
    return;
  }
  documentForm.removeChild(layerContainer);
  documentForm.appendChild(layerContainer);
};
AdfDhtmlZOrderManager.prototype._positionLayerContainer = function(layerContainer) {
  var formId = layerContainer.getAttribute(AdfDhtmlZOrderManager._FORM_ID_EXPANDO);
  var documentForm = AdfDhtmlZOrderManager._getFormOrBody(formId);
  if (layerContainer.parentNode != documentForm) {
    documentForm.appendChild(layerContainer);
    AdfAgent.AGENT.elementsAdded(layerContainer);
  }
};
AdfDhtmlZOrderManager.prototype.containsElement = function(element) {
  if (element == null) {
    return false;
  }
  var constLayerContainer = AdfDhtmlZOrderManager.LAYERCONTAINER;
  for (element = element.parentNode;element;element = element.parentNode) {
    if (element.nodeType == 1) {
      var id = element.getAttribute("id");
      if (id) {
        var tokens = id.split(":");
        var tokensLen = tokens.length;
        if (tokensLen > 1 && constLayerContainer == tokens[tokensLen - 1]) {
          return true;
        }
      }
    }
  }
  return false;
};
AdfDhtmlZOrderManager.prototype.getElementCount = function() {
  var count = 0;
  var layerHash = this._layerHash;
  for (id in layerHash) {
    var layer = layerHash[id];
    if (layer && layer.getContainerElement()) {
      count += layer.getContainerElementCount();
    }
  }
  return count;
};
AdfDhtmlZOrderManager.prototype.removeElement = function(element) {
  if (element.parentNode) {
    AdfPopupScopingUtils.unscope(element);
    var layerContainer = element.parentNode;
    var layerId = layerContainer.getAttribute("id");
    AdfAssert.assertNonEmptyString(layerId);
    var layer = this._getLayer(layerId);
    AdfAssert.assertObject(layer);
    layer.removeElement(element);
  }
};
AdfDhtmlZOrderManager.prototype._getLayerContainer = function(formId) {
  var constLayerContainer = AdfDhtmlZOrderManager.LAYERCONTAINER;
  var layerContainerId = formId ? AdfRichUIPeer.createSubId(formId, constLayerContainer) : constLayerContainer;
  return AdfAgent.AGENT.getElementById(layerContainerId);
};
AdfDhtmlZOrderManager._getFormOrBody = function(formId) {
  var formElement = AdfAgent.AGENT.getElementById(formId);
  if (formElement) {
    return formElement;
  }
  var domDocument = AdfPage.PAGE.getDomDocument();
  return domDocument.body;
};
AdfDhtmlZOrderManager.prototype.prepareMaskingFrame = function(element) {
  var agent = AdfAgent.AGENT;
  var formId;
  if (element) {
    var constLayerContainer = AdfDhtmlZOrderManager.LAYERCONTAINER;
    for (element = element.parentNode;element;element = element.parentNode) {
      if (element.nodeType == 1) {
        var eid = element.getAttribute("id");
        if (eid) {
          var tokens = eid.split(":");
          var tokensLen = tokens.length;
          if (tokensLen > 1 && tokens[tokensLen - 1] == constLayerContainer) {
            formId = element.getAttribute(AdfDhtmlZOrderManager._FORM_ID_EXPANDO);
            break;
          }
        }
      }
    }
  }
  this._assertLayers(formId);
  var constFloatingTypeMaskingFrame = AdfDhtmlZOrderManager.FLOATINGTYPE_MASKINGFRAME;
  var id = formId ? AdfRichUIPeer.createSubId(formId, constFloatingTypeMaskingFrame) : constFloatingTypeMaskingFrame;
  var layer = this._getLayer(id);
  delete layer._unusedMaskingFrame;
  layer.visitElements(AdfDhtmlZOrderManager._findMaskingFramesInUse);
  var maskFrame = layer._unusedMaskingFrame;
  delete layer._unusedMaskingFrame;
  if (!maskFrame) {
    var maskNodeType = this._isGecko ? "DIV" : "IFRAME";
    maskFrame = layer.getContainerElement().ownerDocument.createElement(maskNodeType);
    this.removeMaskingFrame(maskFrame);
    if (maskFrame.tagName == "IFRAME") {
      maskFrame.src = AdfDhtmlZOrderManager._BLANK_PAGE_RESOURCE;
      maskFrame.setAttribute("aria-hidden", true);
    }
    maskFrame.setAttribute(AdfDhtmlZOrderManager._INUSE_EXPANDO, false);
    this.addElementToLayer(maskFrame, constFloatingTypeMaskingFrame, null, formId);
  }
  return maskFrame;
};
AdfDhtmlZOrderManager._findMaskingFramesInUse = function(layer, element) {
  var value = element.getAttribute(AdfDhtmlZOrderManager._INUSE_EXPANDO);
  if ("false" == value) {
    layer._unusedMaskingFrame = element;
    return true;
  }
  return false;
};
AdfDhtmlZOrderManager.prototype.createMaskingFrame = function(element) {
  var maskFrame = this.prepareMaskingFrame(element);
  maskFrame.setAttribute(AdfDhtmlZOrderManager._INUSE_EXPANDO, true);
  var maskFrameStyle = maskFrame.style;
  maskFrameStyle.zIndex = 1;
  return maskFrame;
};
AdfDhtmlZOrderManager.prototype.removeMaskingFrame = function(maskFrame) {
  maskFrame.frameBorder = "0";
  maskFrame.className = "AFMaskingFrame";
  var maskFrameStyle = maskFrame.style;
  maskFrameStyle.visibility = "hidden";
  maskFrameStyle.width = "0px";
  maskFrameStyle.height = "0px";
  maskFrameStyle.left = "0px";
  maskFrameStyle.top = "0px";
  maskFrameStyle.zIndex = 0;
  maskFrame.setAttribute(AdfDhtmlZOrderManager._INUSE_EXPANDO, false);
};
AdfObject.ensureClassInitialization(AdfDhtmlZOrderManager);
