/*
 *  Placeholder
 *
 *  source: https://stackoverflow.com/questions/27237876/how-do-i-add-placeholder-text-to-tinymce
 */
(function () {
    var areaPlaceholder = (function () {
        'use strict';

        var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
        var pluginname = 'areaPlaceholder';

        global.add(pluginname, function ( editor, url ) {
            
            var Label = function () {
                var placeholder_text = editor.getElement().getAttribute("placeholder") || editor.settings.placeholder;
                var placeholder_attrs = editor.settings.placeholder_attrs || { class: "tinymce_placeholder" };
                var contentAreaContainer = editor.getContentAreaContainer();
                
                tinymce.DOM.setStyle(contentAreaContainer, 'position', 'relative');
                this.el = tinymce.DOM.add(contentAreaContainer, "label", placeholder_attrs, placeholder_text);
            }
            Label.prototype.hide = function () {
                tinymce.DOM.setStyle(this.el, 'display', 'none');
            }
            Label.prototype.show = function () {
                tinymce.DOM.setStyle(this.el, 'display', '');
            }
            
            editor.on('init', function () {
                var label = new Label;
                
                // toggle
                function togglePH() {
                    if (editor.getContent() == '') label.show();
                    else label.hide();
                }
                togglePH();
                editor.on('keyup keydown blur change setContent', togglePH);
                
                // on focus
                function onFocus() {
                    editor.execCommand('mceFocus', false);
                }
                tinymce.DOM.bind(label.el, 'click', onFocus);
            });
            
        });

        // return
        function Plugin () {
        }

        return Plugin;

    }());
})();