AdfRichUIPeer.createPeerClass(AdfRichUIPeer, "AdfDhtmlToolboxPeer", false);
AdfDhtmlToolboxPeer.InitSubclass = function() {
  AdfObject.ensureClassInitialization(AdfToolbarUtils);
};
AdfDhtmlToolboxPeer.prototype.InitDomElement = function(component, domElement) {
  AdfDhtmlToolboxPeer.superclass.InitDomElement.call(this, component, domElement);
  AdfPage.PAGE.__registerDescendantResizeNotifyComponent(component);
};
AdfDhtmlToolboxPeer.prototype.ComponentRemoved = function(component) {
  delete this._rows;
  AdfPage.PAGE.__unregisterDescendantResizeNotifyComponent(component);
  AdfDhtmlToolboxPeer.superclass.ComponentRemoved.call(this, component);
};
AdfDhtmlToolboxPeer.prototype.needsResizeNotify = function(component) {
  return true;
};
AdfDhtmlToolboxPeer.prototype.ResizeNotify = function(oldWidth, oldHeight, newWidth, newHeight) {
  if (!this._areRowsValid()) {
    AdfLogger.LOGGER.fine("toolbox#ResizeNotify, toolbox row size stale or not yet defined, recalculate row sizes");
    this._initRows();
  }
  var agent = AdfAgent.AGENT;
  var toolboxElement = this.getDomElement();
  newWidth -= agent.getOuterWidth(toolboxElement, 0);
  var toolboxBodyElement = AdfDomUtils.getFirstChildElement(toolboxElement);
  newWidth -= agent.getOuterWidth(toolboxBodyElement, 0);
  for (var i = 0;i < this._rows.length;i++) {
    var row = this._rows[i];
    var newRowWidth = newWidth - row.childPeers.length;
    var bonusSpace = newRowWidth - row.totalWidth;
    if (bonusSpace < 0) {
      AdfLogger.LOGGER.fine("toolbox#ResizeNotify, resizing toolbar row " + i + ", available width " + newRowWidth + ", is smaller than row width " + row.totalWidth + ", attempt to overflow. ");
      this._overflowChildren(row, newRowWidth, -bonusSpace);
    } else {
      AdfLogger.LOGGER.fine("toolbox#ResizeNotify, resizing toolbar row " + i + ", available width " + newRowWidth + ", is not smaller than row width " + row.totalWidth + ", attempt to stretch.");
      this._stretchChildren(row, newRowWidth, bonusSpace);
    }
  }
};
AdfDhtmlToolboxPeer.prototype.__preferredSizeChangeNotify = function(component) {
  this.bind(component);
  this._rows = null;
};
AdfDhtmlToolboxPeer.prototype._overflowChildren = function(row, newRowWidth, spaceToRemove) {
  var childPeersLength = row.childPeers.length;
  var lastChildPeerIndex = childPeersLength - 1;
  for (var i = lastChildPeerIndex;i >= 0;--i) {
    var peer = row.childPeers[i];
    if (peer == null) {
      AdfLogger.LOGGER.severe("Unexpected null peer for toolbox child at index: ", i);
      break;
    }
    var childElement = peer.getDomElement();
    if (childElement == null) {
      AdfLogger.LOGGER.warning("Unexpected child [" + i + "] found in toolbox component.");
      break;
    }
    childElement.style.width = "";
    var childPadding = this._getPadding(childElement);
    if (peer.GetPreferredWidth) {
      var preferredChildWidth = peer.GetPreferredWidth();
      var preferredContainerWidth = preferredChildWidth + childPadding;
      var newChildWidth;
      var newContainerWidth;
      var minChildWidth = peer.GetMinimumWidth();
      var minContainerWidth = minChildWidth + childPadding;
      newContainerWidth = Math.max(preferredContainerWidth - spaceToRemove, minContainerWidth);
      newChildWidth = newContainerWidth - childPadding;
      childElement.parentNode.style.width = newContainerWidth + 1 + "px";
      childElement.style.width = newChildWidth + "px";
    }
    spaceToRemove -= preferredContainerWidth - newContainerWidth;
    if (spaceToRemove <= 0) {
      spaceToRemoved = 0;
    } else {
      childElement.parentNode.style.width = childElement.offsetWidth + 1 + "px";
    }
  }
};
AdfDhtmlToolboxPeer.prototype._stretchChildren = function(row, newRowWidth, bonusSpace) {
  var childPeersLength = row.childPeers.length;
  var lastChildPeerIndex = childPeersLength - 1;
  for (var i = 0;i < childPeersLength;i++) {
    var peer = row.childPeers[i];
    if (peer == null) {
      AdfLogger.LOGGER.severe("Unexpected null peer for toolbox child at index: ", i);
      break;
    }
    var childElement = peer.getDomElement();
    if (childElement == null) {
      AdfLogger.LOGGER.warning("Unexpected child [" + i + "] found in toolbox component.");
      break;
    }
    childElement.style.width = "";
    var childPadding = this._getPadding(childElement);
    if (peer.GetPreferredWidth) {
      var preferredChildWidth = peer.GetPreferredWidth();
      var preferredContainerWidth = preferredChildWidth + childPadding;
      var component = peer.getComponent();
      var flex = component.getFlex ? component.getFlex() || 0 : 0;
      if (flex > 0) {
        var percentContainerWidth = newRowWidth * (flex / row.totalFlex);
        childElement.parentNode.style.width = percentContainerWidth + "px";
      } else {
        if (i == lastChildPeerIndex && row.totalFlex == 0) {
          childElement.parentNode.style.width = newRowWidth + "px";
        } else {
          childElement.parentNode.style.width = preferredContainerWidth + 1 + "px";
          childElement.style.width = preferredChildWidth + "px";
        }
      }
    } else {
      if (i == lastChildPeerIndex && row.totalFlex == 0) {
        childElement.parentNode.style.width = newRowWidth + "px";
      } else {
        childElement.parentNode.style.width = childElement.offsetWidth + 1 + "px";
      }
    }
  }
};
AdfDhtmlToolboxPeer.prototype._initRows = function() {
  this._rows = [];
  var rootElement = this.getDomElement();
  var rows = rootElement.firstChild.childNodes;
  var rowsLength = rows.length;
  for (var i = 0;i < rowsLength;i++) {
    this._rows[i] = this._getRowInfo(rows[i]);
  }
};
AdfDhtmlToolboxPeer.prototype._getRowInfo = function(rowElement) {
  var agent = AdfAgent.AGENT;
  var childPeers = [];
  var totalWidth = 0;
  var fixedWidth = 0;
  var totalFlex = 0;
  var children = rowElement.getElementsByTagName("tr")[0].childNodes;
  var childrenLength = children.length;
  for (var i = 0;i < childrenLength;i++) {
    var child = children[i];
    var childElement = AdfDomUtils.getFirstChildElement(child);
    if (childElement && childElement.id) {
      var id = childElement.id;
      child.style.width = "";
      var component = AdfPage.PAGE.findComponent(id);
      if (component) {
        var peer = component.getPeer();
        childPeers.push(peer);
        if (peer.GetPreferredWidth) {
          var preferredOuterWidth = peer.GetPreferredWidth() + this._getPadding(childElement);
          totalWidth += preferredOuterWidth;
          var hasFlex = component.getFlex != null && component.getFlex() != null;
          var flex = hasFlex ? component.getFlex() : 0;
          totalFlex += flex;
        } else {
          var outerWidth = agent.getOuterWidth(childElement, childElement.offsetWidth);
          totalWidth += outerWidth;
          fixedWidth += outerWidth;
        }
      }
    }
  }
  return {childPeers:childPeers, totalWidth:totalWidth, fixedWidth:fixedWidth, totalFlex:totalFlex};
};
AdfDhtmlToolboxPeer.prototype._getPadding = function(element) {
  var currentStyle = AdfAgent.AGENT.getComputedStyle(element);
  var cssLengthAsInt = AdfAgent.getCSSLengthAsInt;
  var extraPadding = cssLengthAsInt(currentStyle.paddingLeft) + cssLengthAsInt(currentStyle.paddingRight) + cssLengthAsInt(currentStyle.marginLeft) + cssLengthAsInt(currentStyle.marginRight);
  return extraPadding;
};
AdfDhtmlToolboxPeer.prototype._areRowsValid = function() {
  if (!this._rows) {
    return false;
  }
  for (var i = 0;i < this._rows.length;i++) {
    var row = this._rows[i];
    for (var j = 0;j < row.childPeers.length;j++) {
      var peer = row.childPeers[j];
      if (peer == null) {
        AdfLogger.LOGGER.fine("Toolbox cached rows are not valid, due to unexpected null peer for toolbox child at row: " + i + ", index: " + j);
        return false;
      }
      var childElement = peer.getDomElement();
      if (childElement == null) {
        AdfLogger.LOGGER.fine("Toolbox cached rows are not valid, due to toolbar peer without component (perhaps due to ppr) at row: " + i + ", index: " + j);
        return false;
      }
    }
  }
  return true;
};
