AdfRichUIPeer.createPeerClass(AdfRichUIPeer, "AdfDhtmlCommandMenuItemPeer");
AdfDhtmlCommandMenuItemPeer.InitSubclass = function() {
  AdfObject.ensureClassInitialization(AdfRichCommandMenuItem);
  AdfObject.ensureClassInitialization(AdfMenuUtils);
  AdfRichUIPeer.addComponentEventHandlers(this, AdfUIInputEvent.MOUSE_DOWN_EVENT_TYPE, AdfUIInputEvent.MOUSE_UP_EVENT_TYPE, AdfUIInputEvent.CLICK_EVENT_TYPE, AdfUIInputEvent.FOCUS_EVENT_TYPE, AdfUIInputEvent.BLUR_EVENT_TYPE, AdfUIInputEvent.KEY_UP_EVENT_TYPE, AdfUIInputEvent.KEY_DOWN_EVENT_TYPE);
  if (!AdfAgent.AGENT.isTouchFirstDevice() || AdfAgent.guessOS() == "Windows") {
    AdfRichUIPeer.addComponentEventHandlers(this, AdfUIInputEvent.MOUSE_IN_EVENT_TYPE, AdfUIInputEvent.MOUSE_OUT_EVENT_TYPE);
  }
  AdfRichUIPeer.addComponentPropertyChanges(this, AdfRichCommandMenuItem.DISABLED, AdfRichCommandMenuItem.SELECTED, AdfRichCommandMenuItem.VISIBLE);
  AdfRichUIPeer.addComponentPropertyGetters(this, AdfRichCommandMenuItem.TEXT);
  this._AFR_GROUPID = "_afrGrp";
  this._BUSY = "_busy";
};
AdfDhtmlCommandMenuItemPeer.prototype.Init = function(component) {
  component.getParent();
  AdfDhtmlCommandMenuItemPeer.superclass.Init.call(this, component);
  this._enterKeyDown = false;
  this._spaceKeyDown = false;
};
AdfDhtmlCommandMenuItemPeer.prototype.InitDomElement = function(component, domElement) {
  AdfDhtmlCommandMenuItemPeer.superclass.InitDomElement.call(this, component, domElement);
  var keystroke = component.getAccelerator();
  if (keystroke != null) {
    AdfRichUIPeer.registerKeyStroke(component, keystroke, this._execute);
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.ComponentRemoved = function(component) {
  var keystroke = component.getAccelerator();
  if (keystroke != null) {
    AdfRichUIPeer.unregisterKeyStroke(component, keystroke);
  }
  AdfDhtmlCommandMenuItemPeer.superclass.ComponentRemoved.call(this, component);
};
AdfDhtmlCommandMenuItemPeer.prototype.GetAccessibleName = function() {
  var component = this.getComponent();
  return component.getText();
};
AdfDhtmlCommandMenuItemPeer.prototype.GetComponentText = function(component, domElement) {
  var parent = component.getParent();
  var textElement;
  if (window["AdfRichMenu"] && parent instanceof AdfRichMenu) {
    textElement = AdfDomUtils.getChildElements(domElement)[1];
  } else {
    textElement = AdfDomUtils.getFirstDescendentElement(domElement, "a");
  }
  if (textElement) {
    if (component.getType() == "antonym") {
      textElement = AdfDomUtils.getFirstChildElement(textElement);
    }
    if (textElement) {
      var text = AdfAgent.AGENT.getTextContent(textElement);
      if (text == "") {
        return null;
      } else {
        return text;
      }
    }
  }
  AdfAssert.assert(false, "Unable to find the element containing the text");
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentKeyUp = function(componentEvent) {
  var evt = componentEvent.getNativeEvent();
  if (evt.ctrlKey) {
    return;
  }
  var agent = AdfAgent.AGENT;
  var keyCode = agent.getKeyCode(evt);
  var component = this.getComponent();
  var isBusy = component.getProperty(AdfDhtmlCommandMenuItemPeer._BUSY);
  var cancelEvent;
  switch(keyCode) {
    case AdfKeyStroke.ENTER_KEY:
      if (this._enterKeyDown && !isBusy) {
        this._execute();
      }
      this._enterKeyDown = false;
      cancelEvent = true;
      break;
    case AdfKeyStroke.SPACE_KEY:
      if (this._spaceKeyDown && !isBusy) {
        this._execute();
      }
      this._spaceKeyDown = false;
      cancelEvent = true;
      break;
    default:
      return;
  }
  if (cancelEvent) {
    componentEvent.cancel();
  }
  var parent = component.getParent();
  if (parent && parent instanceof AdfRichMenu) {
    parent.getPeer().updateScrollIconsState();
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentKeyDown = function(componentEvent) {
  if (componentEvent.isCanceled() || this._isDisabled()) {
    return;
  }
  var keyCode = componentEvent.getKeyCode();
  if (keyCode == AdfKeyStroke.ENTER_KEY) {
    this._enterKeyDown = true;
    componentEvent.cancel();
    componentEvent.stopBubbling();
    return;
  } else {
    if (keyCode == AdfKeyStroke.SPACE_KEY) {
      this._spaceKeyDown = true;
      componentEvent.cancel();
      componentEvent.stopBubbling();
      return;
    }
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentFocus = function(componentEvent) {
  if (this._isDisabled()) {
    return;
  }
  componentEvent.cancel();
  var rootElement = this.getDomElement();
  AdfDhtmlMenuPeer.cancelShowing();
  var component = this.getComponent();
  if (AdfMenuUtils.isInMenu(component)) {
    AdfDomUtils.addCSSClassName(rootElement, AdfMenuUtils.HIGHLIGHTED_STYLE_CLASS);
  } else {
    if (AdfMenuUtils.isMenuBarAutoOpenMode()) {
      AdfMenuUtils.closeAllMenus(false);
    }
    AdfMenuUtils.highlightRootMenuItem(rootElement);
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentMouseDown = function(componentEvent) {
  if (this._isDisabled() || !componentEvent.isLeftButtonPressed()) {
    return;
  }
  var rootElement = this.getDomElement();
  AdfDomUtils.addCSSClassName(rootElement, AdfMenuUtils.DEPRESSED_STYLE_CLASS);
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentMouseUp = function(componentEvent) {
  if (this._isDisabled() || !componentEvent.isLeftButtonPressed()) {
    return;
  }
  var rootElement = this.getDomElement();
  AdfDomUtils.removeCSSClassName(rootElement, AdfMenuUtils.DEPRESSED_STYLE_CLASS);
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentMouseOver = function(componentEvent) {
  var component = this.getComponent();
  if (this._isDisabled()) {
    if (AdfMenuUtils.isMenuBarAutoOpenMode() && AdfMenuUtils.isOnMenuBar(component)) {
      AdfMenuUtils.closeAllMenus(false);
    }
    return;
  }
  componentEvent.cancel();
  var rootElement = this.getDomElement();
  var alreadyHaveFocus = AdfFocusUtils.containsFocus(rootElement);
  if (AdfMenuUtils.isInMenu(component)) {
    AdfDomUtils.addCSSClassName(rootElement, AdfMenuUtils.HIGHLIGHTED_STYLE_CLASS);
    AdfMenuUtils.dismiss(rootElement, false);
    if (!alreadyHaveFocus) {
      this.focus(component);
    }
  } else {
    if (AdfMenuUtils.isMenuBarAutoOpenMode() && !alreadyHaveFocus) {
      this.focus(component);
    } else {
      AdfMenuUtils.highlightRootMenuItem(rootElement);
    }
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentMouseOut = function(componentEvent) {
  componentEvent.cancel();
  AdfMenuUtils.unhighlightRootMenuItem();
  if (this._isDisabled()) {
    return;
  }
  var component = this.getComponent();
  if (AdfMenuUtils.isInMenu(component)) {
    this._deselectElement();
  }
  var rootElement = this.getDomElement();
  AdfDomUtils.removeCSSClassName(rootElement, AdfMenuUtils.DEPRESSED_STYLE_CLASS);
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentBlur = function(componentEvent) {
  this._deselectElement();
};
AdfDhtmlCommandMenuItemPeer.prototype.HandleComponentClick = function(componentEvent) {
  var component = this.getComponent();
  var isBusy = component.getProperty(AdfDhtmlCommandMenuItemPeer._BUSY);
  if (componentEvent.isLeftButtonPressed() && !this._isDisabled() && !isBusy) {
    this._execute();
  }
  componentEvent.cancel();
};
AdfDhtmlCommandMenuItemPeer.prototype.ComponentDisabledChanged = function(component, domElement, newValue, oldValue) {
  AdfMenuUtils.menuItemDisabledChanged(component, domElement, newValue);
};
AdfDhtmlCommandMenuItemPeer.prototype.ComponentSelectedChanged = function(component, domElement, newValue, oldValue) {
  var type = component.getType();
  if (type == "check" || type == "radio") {
    var ariaElement = AdfMenuUtils.isInMenu(component) ? domElement : AdfDomUtils.getFirstChildElement(domElement);
    var selected = newValue ? "true" : "false";
    ariaElement.setAttribute("aria-checked", selected);
  }
  if (newValue) {
    AdfDomUtils.addCSSClassName(domElement, AdfMenuUtils.SELECTED_STYLE_CLASS);
    if (type == "radio") {
      this._toggleRadioGroupItems();
    }
  } else {
    AdfDomUtils.removeCSSClassName(domElement, AdfMenuUtils.SELECTED_STYLE_CLASS);
  }
};
AdfDhtmlCommandMenuItemPeer.prototype.ComponentVisibleChanged = function(component, domElement, newValue, oldValue) {
  AdfDhtmlCommandMenuItemPeer.superclass.ComponentVisibleChanged.call(this, component, domElement, newValue, oldValue);
  AdfMenuUtils.menuItemVisibleChanged(component, domElement, newValue);
};
AdfDhtmlCommandMenuItemPeer.prototype.SetBusy = function(isBusy) {
  var component = this.getComponent();
  var domElement = this.getDomElement();
  var actionForm = AdfDomUtils.getFormElement(domElement);
  if (!actionForm) {
    return;
  }
  var isMultipart = AdfDomUtils.isMultiPartForm(actionForm);
  if (component.getDisabled() || isMultipart || AdfPage.PAGE.isAttachmentMode()) {
    return;
  }
  AdfDomUtils.addOrRemoveCSSClassName(isBusy, domElement, AdfRichUIPeer.DISABLED_STYLECLASS);
  component.setProperty(AdfDhtmlCommandMenuItemPeer._BUSY, isBusy, false, AdfUIComponent.PROPAGATE_NEVER);
};
AdfDhtmlCommandMenuItemPeer.prototype.__preferredSizeChangeNotify = function(component) {
  this.bind(component);
  var parent = this.getComponentParent(component);
  var parentPeer = parent != null ? parent.getPeer() : null;
  if (parentPeer && parentPeer.__preferredSizeChangeNotify) {
    parentPeer.__preferredSizeChangeNotify(parent);
  }
};
AdfDhtmlCommandMenuItemPeer.prototype._execute = function() {
  if (this._isDisabled()) {
    return;
  }
  AdfLogger.LOGGER.finest("menuBar auto open mode, set to OFF due to commandMenuItem execute");
  AdfMenuUtils.setMenuBarAutoOpenMode(false);
  var component = this.getComponent();
  var parent = component.getParent();
  while (parent instanceof AdfRichMenu) {
    parent = parent.getParent();
  }
  if (parent instanceof AdfRichPopup) {
    AdfMenuUtils.closeAllMenus(false);
    parent.getPeer().hide(parent);
  } else {
    var inMenu = AdfMenuUtils.isInMenu(component);
    AdfMenuUtils.closeAllMenus(inMenu && !AdfMenuUtils.isInDetachedMenu(component));
  }
  var targetAttrs = AdfMenuUtils.getContextMenuItemTargetAttrs(component);
  if (targetAttrs != null) {
    AdfMenuUtils.setContextMenuItemTargetAttrs(component, targetAttrs);
  }
  var actionEvent = new AdfActionEvent(component);
  actionEvent.queue(component.getPartialSubmit());
  if (!actionEvent.isCanceled()) {
    var type = component.getType();
    if (type == "check" || type == "antonym") {
      component.setSelected(!component.getSelected());
    } else {
      if (type == "radio") {
        this._toggleRadioGroupItems();
      }
    }
  }
};
AdfDhtmlCommandMenuItemPeer.prototype._toggleRadioGroupItems = function() {
  var component = this.getComponent();
  var domElement = this.getDomElement();
  var currentGroupId = this._getGroupId(domElement);
  if (currentGroupId == null) {
    AdfLogger.LOGGER.warning("Unexpected to have commandMenuItem without a group Id.");
    component.setSelected(!component.getSelected());
    return;
  }
  if (AdfMenuUtils.isInMenu(component)) {
    this._toggleMenuRadioGroupItems(currentGroupId);
  } else {
    if (AdfMenuUtils.isOnMenuBar(component)) {
      this._toggleMenuBarRadioGroupItems(currentGroupId);
    }
  }
  component.setSelected(true);
};
AdfDhtmlCommandMenuItemPeer.prototype._toggleMenuRadioGroupItems = function(currentGroupId) {
  var component = this.getComponent();
  var domElement = this.getDomElement();
  var menuItems = domElement.parentNode.childNodes;
  for (var i = 0;i < menuItems.length;i++) {
    var menuItem = menuItems[i];
    var groupId = this._getGroupId(menuItem);
    if (currentGroupId == groupId) {
      var id = menuItem.id;
      if (id) {
        var sibling = AdfPage.PAGE.findComponent(id);
        if (sibling != component && sibling.getType && sibling.getType() == "radio") {
          sibling.setSelected(false);
        }
      }
    }
  }
};
AdfDhtmlCommandMenuItemPeer.prototype._toggleMenuBarRadioGroupItems = function(currentGroupId) {
  var component = this.getComponent();
  var menuBar = component.getParent();
  var menuBarItems = menuBar.getPeer().getDescendantComponents(menuBar);
  for (var i = 0;i < menuBarItems.length;i++) {
    var menuBarItem = menuBarItems[i];
    var menuItemPeer = menuBarItem.getPeer();
    if (menuItemPeer == null) {
      continue;
    }
    var menuItemElement = menuItemPeer.getDomElement();
    var groupId = this._getGroupId(menuItemElement);
    if (currentGroupId == groupId) {
      if (menuBarItem != component && menuBarItem.getType && menuBarItem.getType() == "radio") {
        menuBarItem.setSelected(false);
      }
    }
  }
};
AdfDhtmlCommandMenuItemPeer.prototype._getGroupId = function(domElement) {
  if (domElement.getAttribute) {
    return domElement.getAttribute(AdfDhtmlCommandMenuItemPeer._AFR_GROUPID);
  } else {
    return null;
  }
};
AdfDhtmlCommandMenuItemPeer.prototype._deselectElement = function() {
  var rootElement = this.getDomElement();
  AdfDomUtils.removeCSSClassName(rootElement, AdfMenuUtils.HIGHLIGHTED_STYLE_CLASS);
};
AdfDhtmlCommandMenuItemPeer.prototype._isDisabled = function() {
  var rootElement = this.getDomElement();
  return rootElement.disabled || AdfDomUtils.containsCSSClassName(rootElement, AdfRichUIPeer.DISABLED_STYLECLASS);
};
