'use strict';

(function () {
  var EVENT_NAMES = {
    ALL_IDS_COMPLETE: 'allIdsComplete',
    ID_ADDED: 'idAdded'
  };

  CKEDITOR.plugins.add('autoid', {
    init: function (editor) {
      var self = this;

      // This configures the Advanced Content Filter to allow all headings
      // to have id attributes
      editor.addFeature({
        allowedContent: 'h1 h2 h3 h4 h5 h6 *[id]'
      });

      if (editor && !editor.config.autoid) {
        editor.config.autoid = {};
      }
      self.settings = editor.config.autoid;
      if (!self.settings) {
        self.settings = {};
      }

      editor.addCommand('autoid', {
        exec: function (editor) {
          if (CKEDITOR.TRISTATE_OFF) {
            start();
          } else {
            stop();
          }
        },
        editorFocus: true
      });

      editor.on("instanceReady", function () {
        if (self.settings.autostart !== false) {
          start()
        }
      });

      editor.on('selectionChange', addIdIfNewHeading);

      function start() {
        editor.getCommand('autoid').setState(CKEDITOR.TRISTATE_ON);

        addAllIds();
      }

      function stop() {
        editor.getCommand('autoid').setState(CKEDITOR.TRISTATE_OFF);
      }

      function addAllIds() {
        var headings = findAllHeadings(),
        i, heading;

        for (i = 0; i < headings.count(); i++) {
          heading = headings.getItem(i);
          if (heading.hasAttribute('id')) {
            continue;
          }
          addId(heading);
        }
        editor.fire(EVENT_NAMES.ALL_IDS_COMPLETE);
      }

      function findAllHeadings() {
        return editor.document.find('h1, h2, h3, h4, h5, h6');
      }

      function addId(heading) {
        var uuid = CKEDITOR.tools.getUniqueId();
        heading.setAttributes({ id: uuid });
        editor.fire(EVENT_NAMES.ID_ADDED);
      }

      function addIdIfNewHeading() {
        var element = editor.getSelection().getStartElement();
        if (isHeading(element) && !element.hasAttribute('id')) {
          addId(element);
        }
      }

      function isHeading(element) {
        return element.is('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
      }

    }
  });
})();

@@ -660,24 +660,77 @@
  (editor) %7B%0A
+          editor.getCommand('autoid').toggleState();%0A
           if
@@ -754,13 +754,10 @@
 TE_O
-FF) %7B
+N)
 %0A   
@@ -769,67 +769,20 @@
     
-start();%0A          %7D else %7B%0A            stop();%0A          %7D
+addAllIds();
 %0A   
@@ -928,126 +928,8 @@
 %7B%0A  
-        start()%0A        %7D%0A      %7D);%0A%0A      editor.on('selectionChange', addIdIfNewHeading);%0A%0A      function start() %7B%0A
     
@@ -992,17 +992,18 @@
 TE_ON);%0A
-%0A
+  
         
@@ -1025,111 +1025,77 @@
     
+  
 %7D
-%0A
 %0A      
-function stop() %7B%0A        editor.getCommand('autoid').setState(CKEDITOR.TRISTATE_OFF);%0A      %7D
+%7D);%0A%0A      editor.on('selectionChange', addIdIfNewHeading);
 %0A%0A  

Refactor to use state and remove start and stop functions