CKEDITOR.plugins.add('bookmarktemplate',
    {
        requires: ['richcombo'], //, 'styles' ],
        init: function (editor) {
            var config = editor.config,
                lang = editor.lang.format;

            // Gets the list of tags from the settings.
            var options = []; //new Array();
            var delimiter = '|^~^|';
            var buildListHasRunOnce = 0;
            var buildList = function () {
                if (buildListHasRunOnce) {
                    // Remove the old unordered list from the dom.
                    // This is just to cleanup the old list within the iframe
                    $(this._.panel._.iframe.$).contents().find("ul").remove();
                    // reset list
                    this._.items = {};
                    this._.list._.items = {};
                }
                for (var i in editor.lstGroupedTmpls) {
                    if (i != "clean")
                    {
                        var item = editor.lstGroupedTmpls[i];
                        // do your add calls
                        this.add(item.BookmarkName);
                    }
                  
                }
                if (buildListHasRunOnce) {
                    // Force CKEditor to commit the html it generates through this.add
                    this._.committed = 0; // We have to set to false in order to trigger a complete commit()
                    this.commit();
                }
                buildListHasRunOnce = 1;
            };
            //this.add('value', 'drop_text', 'drop_label');
            //var tags = [];
            //tags[0] = ["[contact_name]", "Name", "Name"];
            //tags[1] = ["[contact_email]", "email", "email"];
            //tags[2] = ["[contact_user_name]", "User name", "User name"];

            // Create style objects for all defined styles.

            //var fnData = editor.config.fnData;

            //if (!fnData || typeof (fnData) != 'function')
            //    throw "You must provide a function to retrieve the list data.";

            editor.ui.addRichCombo('bookmarktemplate',
                {
                    label: "Choose bookmark template",
                    title: "Choose bookmark template",
                    voiceLabel: "Choose bookmark template",
                    toolbar: 'others',
                    // className: 'cke_format',
                    multiSelect: false,

                    panel:
                    {
                        css: [CKEDITOR.skin.getPath('editor')].concat(config.contentsCss),
                        //css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css')],
                        voiceLabel: lang.panelVoiceLabel
                    },

                    init: function () {
                       // var self = this;

                        var rebuildList = CKEDITOR.tools.bind(buildList, this);
                        rebuildList();
                        $(editor).bind('rebuildList', rebuildList);

                    },

                    onClick: function (value) {   //value is refer bookmark value

                        editor.focus();
                        editor.fire('saveSnapshot');
                        var arr = value.split(delimiter);

                        editor.selTemplateID = arr[0];

                        editor.selBookmarkTemplate = this._.items[value];
                        //value = arr[1];
                        //editor.selSignature = '<br>' + arr[1];
                        //editor.selBookmarkTemplate = arr[1];
                        if (!$('#TemplateHTML').val()) {
                            var input = $('#TemplateHTML');
                            input.val(editor.selBookmarkTemplate);
                            input.trigger('input'); // Use for Chrome/Firefox/Edge
                            input.trigger('change'); // Use for Chrome/Firefox/Edge + IE11
                            return;
                        };

                        editor.fire('saveSnapshot');

                        //editor.fire('saveSnapshot');

                    },
                    onRender: function () {

                        editor.on("selectionChange", function (b) {

                            this.setValue(editor.selTemplateHTML, editor.selTemplateID);
                            
                            return
                        }, this)
                    }
                });
        }


    });