define([
    'jquery',
    '../lib/ajax',
    "../core/parser",
    '../core/logging',
    '../registry',
    'tinymce'
], function($, ajax, Parser, logging, registry) {
    var log = logging.getLogger('editTinyMCE'),
        parser = new Parser("edit-tinymce");

    parser.add_argument('theme-baseurl');

    var _ = {
        name: "editTinyMCE",
        trigger: 'form textarea.pat-edit-tinymce',
        init: function($el, opts) {
            var $form = $el.parents('form'),
                $resetbtn = $form.find('[type=reset]'),
                id = $el.attr('id');

            // make sure the textarea has an id
            if (!id) {
                var formid = $form.attr('id'),
                    name = $el.attr('name');
                if (!formid) {
                    log.error('Textarea or parent form needs an id', $el, $form);
                    return false;
                }
                if (!name) {
                    log.error('Textarea needs a name', $el);
                    return false;
                }
                id = formid + '_' + name;
                if ($('#'+id).length > 0) {
                    log.error('Textarea needs an id', $el);
                    return false;
                }
                $el.attr({id: id});
            }

            // read configuration
            var cfg = $el.data('tinymce-json');
            if (!cfg) {
                log.info('data-tinymce-json empty, using default config', $el);
                cfg = {};
            }
            cfg.elements = id;
            cfg.mode = 'exact';
            cfg.readonly = Boolean($el.attr('readonly'));
            
            // get arguments
            args = parser.parse($el, opts);

            if (args.themeBaseurl && cfg.theme &&
                !tinyMCE.ThemeManager.urls[cfg.theme]) {
                log.info('loading tinymce theme ' + cfg.theme);
                tinyMCE.ThemeManager.load(cfg.theme, args.themeBaseurl + '/' +
                    cfg.theme + '/editor_template' + tinyMCE.suffix + '.js');
            }

            // initialize editor
            var tinymce = tinyMCE.init(cfg);

            // ajaxify form
            var ajaxopts = {
                beforeSerialize: (function(id) {
                    return function() {
                        tinyMCE.editors[id].save();
                    };
                })(id)
            };

            $form.on('submit', function(ev) {
                ev.preventDefault();
                ajax($form, ajaxopts);
            });

            // XXX: we hijack the reset button, but currently only reset
            // the tiny textarea.
            $resetbtn.on('click.pat-edit-tinymce', (function(id) {
                return function(ev) {
                    ev.preventDefault();
                    tinyMCE.editors[id].load();
                };
            })(id));

            return $el;
        },
        destroy: function($el) {
            // XXX
        }
    };

    registry.register(_);
    return _;
});

// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab

@@ -112,16 +112,27 @@
 tinymce'
+,%0A    'URI'
 %0A%5D, func
@@ -309,16 +309,59 @@
 seurl');
+%0A    parser.add_argument('plugin-baseurl');
 %0A%0A    va
@@ -1689,28 +1689,16 @@
 nly'));%0A
-            
 %0A       
@@ -1731,16 +1731,20 @@
         
+var 
 args = p
@@ -1764,24 +1764,74 @@
 el, opts);%0A%0A
+            // load themes from user defined path%0A
             
@@ -1921,24 +1921,245 @@
 g.theme%5D) %7B%0A
+                args.themeBaseurl = URI(args.themeBaseurl).absoluteTo().toString();%0A                var url = args.themeBaseurl + '/' + cfg.theme,%0A                    jsFile = 'editor_template' + tinyMCE.suffix + '.js';%0A%0A
             
@@ -2263,38 +2263,117 @@
 .theme, 
-args.themeBase
+url + '/' + jsFile);%0A                if (!cfg.popup_css)%0A                    cfg.popup_css = 
 url + '/
 ' +%0A    
@@ -2364,16 +2364,22 @@
 url + '/
+skins/
 ' +%0A    
@@ -2398,37 +2398,215 @@
     
-cfg.theme +
+    (cfg.skin %7C%7C 'default') + '/dialog.css';%0A            %7D%0A%0A            // load plugins from user defined path%0A            if (args.pluginBaseurl && cfg.plugins) %7B%0A                var jsFile =
  '
-/
 editor_
-template
+plugin
 ' + 
@@ -2627,16 +2627,323 @@
  + '.js'
+;%0A                args.pluginBaseurl = URI(args.pluginBaseurl).absoluteTo().toString();%0A                cfg.plugins.split(/%5Cs*,%5Cs*/).forEach(function(plugin) %7B%0A                    tinyMCE.PluginManager.load(plugin,%0A                        args.pluginBaseurl + '/' + plugin + '/' + jsFile);%0A                %7D
 );%0A     

Enable loading of TinyMCE plugins from custom locations. #144