I changed the weird JS hack in the encodebegin.
I added an HtmlComponentControl to the button, and changed the encodeBegin to the following:
HtmlComponentControl control = (HtmlComponentControl)this.getButton().getChildren().get(0);
 control.setAttachTo(this.getButton().getClientId(context));
 control.setFor(this.getCalendar().getClientId(context));
 control.setEvent("onclick");
 control.setOperation("switchState");