protected void encodeMarkup(FacesContext context, AccordionPanel acco) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String clientId = acco.getClientId(context);
    String widgetVar = acco.resolveWidgetVar();
    String styleClass = acco.getStyleClass();
    styleClass =
        styleClass == null
            ? AccordionPanel.CONTAINER_CLASS
            : AccordionPanel.CONTAINER_CLASS + " " + styleClass;

    if (ComponentUtils.isRTL(context, acco)) {
      styleClass = styleClass + " ui-accordion-rtl";
    }

    writer.startElement("div", null);
    writer.writeAttribute("id", clientId, null);
    writer.writeAttribute("class", styleClass, null);
    if (acco.getStyle() != null) {
      writer.writeAttribute("style", acco.getStyle(), null);
    }

    writer.writeAttribute("role", "tablist", null);

    writer.writeAttribute(HTML.WIDGET_VAR, widgetVar, null);

    encodeTabs(context, acco);

    encodeStateHolder(context, acco);

    writer.endElement("div");
  }
  @Override
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    Map<String, String> params = context.getExternalContext().getRequestParameterMap();
    AccordionPanel acco = (AccordionPanel) component;

    if (acco.isContentLoadRequest(context)) {
      String var = acco.getVar();
      String clientId = acco.getClientId(context);

      if (var == null) {
        String tabClientId = params.get(clientId + "_newTab");
        Tab tabToLoad = acco.findTab(tabClientId);
        tabToLoad.encodeAll(context);
        tabToLoad.setLoaded(true);
      } else {
        int index = Integer.parseInt(params.get(clientId + "_tabindex"));
        acco.setIndex(index);
        acco.getChildren().get(0).encodeAll(context);
        acco.setIndex(-1);
      }
    } else {
      encodeMarkup(context, acco);
      encodeScript(context, acco);
    }
  }
  protected void encodeStateHolder(FacesContext context, AccordionPanel accordionPanel)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String clientId = accordionPanel.getClientId(context);
    String stateHolderId = clientId + "_active";

    writer.startElement("input", null);
    writer.writeAttribute("type", "hidden", null);
    writer.writeAttribute("id", stateHolderId, null);
    writer.writeAttribute("name", stateHolderId, null);
    writer.writeAttribute("value", accordionPanel.getActiveIndex(), null);
    writer.writeAttribute("autocomplete", "off", null);
    writer.endElement("input");
  }
  @Override
  public void decode(FacesContext context, UIComponent component) {
    AccordionPanel acco = (AccordionPanel) component;
    Map<String, String> params = context.getExternalContext().getRequestParameterMap();
    String active = params.get(acco.getClientId(context) + "_active");

    if (active != null) {
      if (isValueBlank(active)) {
        acco.setActiveIndex(null);
      } else {
        acco.setActiveIndex(active);
      }
    }

    decodeBehaviors(context, component);
  }
  protected void encodeScript(FacesContext context, AccordionPanel acco) throws IOException {
    String clientId = acco.getClientId(context);
    boolean multiple = acco.isMultiple();

    WidgetBuilder wb = getWidgetBuilder(context);
    wb.init("AccordionPanel", acco.resolveWidgetVar(), clientId);

    if (acco.isDynamic()) {
      wb.attr("dynamic", true).attr("cache", acco.isCache());
    }

    wb.attr("multiple", multiple, false)
        .callback("onTabChange", "function(panel)", acco.getOnTabChange())
        .callback("onTabShow", "function(panel)", acco.getOnTabShow())
        .callback("onTabClose", "function(panel)", acco.getOnTabClose());

    if (acco.getTabController() != null) {
      wb.attr("controlled", true);
    }

    encodeClientBehaviors(context, acco);

    wb.finish();
  }