/**
 * @license Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.html or http://ckeditor.com/license
 */

/**
 * @fileOverview  Plugin that changes the toolbar and maximizes the editor
 *                for the big toolbar.
 *
 *                You need a custom config to define the small and big toolbars.
 *                Also the maximize plug-in is needed but not the maximize button.
 *                For this plugin you should use the 'Toolbarswitch' button instead.
 *
 *                CKEDITOR.replace('sometextcomponentname', {
 *               		customConfig: '/...custom_ckeditor_config.js'
 *               		toolbar: 'yoursmalltoolbarname', 
 *               		smallToolbar: 'yoursmalltoolbarname',
 *               		maximizedToolbar: 'yourbigtoolbarname' });
 *
 *                Requires:
 *                - Maximize plugin. But not the button that goes with it.
 *                - All toolbars used in the ckeditor instance have to use the 'Toolbarswitch' button instead.
 *                - A custom config to define the small and big toolbars.
 *                - jQuery
 */


function switchMe(editor, callback) {

    var origCustomConfig = editor.config.customConfig;
    var origContentCss = editor.config.contentsCss;
    var origExtraPlugins = editor.config.extraPlugins;

    var origToolbar = editor.config.toolbar;
    var origSmallToolbar = editor.config.smallToolbar;
    var origMaximizedToolbar = editor.config.maximizedToolbar;
    var newToolbar;
    //if (editor.commands.maximize.state == 2) {
    if (origToolbar == origSmallToolbar) {
        newToolbar = origMaximizedToolbar;
    } else {
        newToolbar = origSmallToolbar;
    }

    // Remember editing data before getting rid of the old editor
    var oldEditorData = editor.getData();
    var domElement = editor.element.$;

    // Remove old editor and the DOM elements, else you get two editors
    //var id = domElement.id;
    editor.destroy(true);

    CKEDITOR.replace(domElement, {
        customConfig: origCustomConfig,
        contentsCss: origContentCss,
        toolbar: newToolbar,
        smallToolbar: origSmallToolbar,
        maximizedToolbar: origMaximizedToolbar,
        extraPlugins: origExtraPlugins,
        on: {
            instanceReady: function (e) {
                // Restore old editing data
                if (typeof(oldEditorData) != "undefined") {
                    e.editor.setData(oldEditorData);
                }

                if (callback) {
                    callback.call(null, e);
                }
            }
        }
    });
}

CKEDITOR.plugins.add('toolbarswitch', {
    requires: ['button', 'toolbar', 'maximize'],
    lang: 'en,ru,uk', // %REMOVE_LINE_CORE%
    icons: 'toolbarswitch', // %REMOVE_LINE_CORE%
    hidpi: true, // %REMOVE_LINE_CORE%
    init: function (editor) {
        var lang = editor.lang;

        var commandFunction = {
            exec: function (editor) {
                if (editor.config.toolbar == editor.config.maximizedToolbar) {
                    // For switching to the small toolbar first minimize
                    editor.commands.maximize.exec();
                    switchMe(editor, function (e) {
                        var newEditor = e.editor;
                        newEditor.fire('triggerResize');
                    });
                } else {
                    switchMe(editor, function (e) {
                        var newEditor = e.editor;
                        newEditor.commands.maximize.exec();
                        newEditor.fire('triggerResize');
                        newEditor.commands.toolbarswitch.setState(CKEDITOR.TRISTATE_ON);
                    });
                }
            }
        };

        var command = editor.addCommand('toolbarswitch', commandFunction);
        command.modes = {wysiwyg: 1, source: 1};
        command.canUndo = false;
        command.readOnly = 1;

        editor.ui.addButton('Toolbarswitch', {
            label: lang.toolbarswitch.toolbarswitch,
            command: 'toolbarswitch',
            toolbar: 'tools,10'
        });

    }
});