﻿

// This is always called in the document ready block for pages that use CDEditor
function setUpPageForCKEditor(saveButtonClientID) {

    // First add the div that will be the jquery dialog box
    // that will contain the list of uploaded images and files.
    jQuery("body").append('<div id="divInsertFilePopUpId" style="display: none;" ></div>')

    // Setup the div to be a jquery dialog box
    jQuery(function () {
        jQuery('#' + divInsertFilePopUpId).dialog({
            autoOpen: false,
            width: 750,
            height: 625,
            zIndex: 10001
        });
    });

    // Convert all textareas on page to a CKEditor window.
    CKEDITOR.replaceAll(function (textarea, config) {
        // Custom code to evaluate the replace, returning false
        // if it must not be done.
        // It also passes the "config" parameter, so the
        // developer can customize the instance.
        config.removePlugins = 'scayt'; // remove ckeditor spell checker.
    });

    // Set CKEditor windows to set the ckInsertFile js variable when focus on editor is invoked.
    for (var i in CKEDITOR.instances) {
        CKEDITOR.instances[i].on('focus', function (e) { setCkInsertFile(e.editor.name); });
    }


    // Catch Save Button in CKEditor window.
    // It's tied to the button on the child page to save the form.    
    (function () {
        var saveCmd = {
            modes: { wysiwyg: 1, source: 1 },
            exec: function (editor) {
                jQuery('#' + saveButtonClientID).click();
                // jQuery($form = editor.element.$.form).submit();
            }
        };

        var pluginName = 'safesave';

        // Register a plugin named "save".
        CKEDITOR.plugins.add(pluginName, {
            init: function (editor) {
                var command = editor.addCommand(pluginName, saveCmd);
                command.modes = { wysiwyg: !!(editor.element.$.form) };

                editor.ui.addButton('SafeSave', {
                    label: editor.lang.save,
                    command: pluginName,
                    className: 'cke_button_save'
                });
            }
        });
    })();



}


// This is the hard coded id of the popup dialog div on all pages with a ckeditor window.
var divInsertFilePopUpId = "divInsertFilePopUpId";
// This is the var that holds the specific CKEditor id. It is set on focus of the CKEditor windows.
var ckInsertFile = ""; /* Set on focus of ckeditor object */

function setCkInsertFile(value) {
    ckInsertFile = value;
}

function insertFile() {

    jQuery('#' + divInsertFilePopUpId).dialog('open');

    jQuery.ajax({
        url: '../../WebMethodsUploadedFiles.aspx/GetListOfUploadedFiles',
        type: 'POST',
        data: "{ }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $('#' + divInsertFilePopUpId).html(msg.d);
        }
    });

}


function closeInsertImageDialog(value) {
    var ckEditor = CKEDITOR.instances[ckInsertFile];
    ckEditor.insertHtml(value);

    jQuery('#' + divInsertFilePopUpId).dialog('close');
}


