// CKEditor TeXZilla Plugin
// Copyright (C) 2014  Raniere Silva
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

CKEDITOR.dialog.add('texzillaDialog', function( editor ) {
    return {
        title: 'TeXZilla Edit Box',
        minWidth: 300,
        minHeight: 200,
        contents: [
            {
                id: 'basic',
                label: 'Basic Settings',
                elements: [
                    {
                        id: 'tex',
                        type: 'textarea',
                        label: 'TeX code',
                        setup: function(element) {
                            // TODO Look for <annotation encoding="TeX">
                            // TODO Look for <math alttext=""> as fallback
                        }
                    }
                ]
            },
            {
                id: 'advanced',
                label: 'Advanced Settings',
                elements: [
                    {
                        id: 'display',
                        type: 'checkbox',
                        label: 'Display',
                        setup: function(element) {
                            var display = element.getAttribute("display");
                            if (display === 'block') {
                                this.setValue(true);
                            }
                            else {
                                this.setValue(false);
                            }
                        }
                    }
                ]
            }
        ],
        onShow: function() {
            var selection = editor.getSelection();
            var element = selection.getStartElement();
            // Try to locate a `math` or `body` tag.
            while (element.getName() != 'math' &&
                    element.getName() != 'body') {
                element = element.getParent();
            }
            if (!element || element.getName() != 'math') {
                this.insertMode = true;
            }
            else {
                this.mathRoot = element;
            }
            if (!this.insertMode) {
                // invoke the setup functions for the element
                this.setupContent( element );
            }
        },
        onOk: function() {
          var dialog = this;

          // This is the better way to insert the MathML, although we got
          //
          //     TypeError: element.getName is not a function
          //
          // when using it.
          //
          // var mathElement = TeXZilla.toMathML(dialog.getValueOf('basic', 'tex'));

          // This is a hack found at
          // http://stackoverflow.com/a/17339275/1802726.
          var math = TeXZilla.toMathMLString(dialog.getValueOf('basic', 'tex'));
          var mathElement = CKEDITOR.dom.element.createFromHtml(math, editor.document);

          editor.insertElement(mathElement);
        }
    };
} );

@@ -1354,32 +1354,830 @@
               %7D%0A
+                    %7D,%0A                    %7B%0A                        id: 'display',%0A                        type: 'checkbox',%0A                        label: 'Display',%0A                        setup: function(element) %7B%0A                            var display = element.getAttribute(%22display%22);%0A                            if (display === 'block') %7B%0A                                this.setValue(true);%0A                            %7D%0A                            else %7B%0A                                this.setValue(false);%0A                            %7D%0A                        %7D%0A                    %7D,%0A                    %7B%0A                        id: 'preview',%0A                        type: 'html',%0A                        label: 'Preview',%0A                        html: '%3Cdiv id=%22Preview%22%3E%3C/div%3E'%0A
                 
@@ -2174,32 +2174,32 @@
               %7D%0A
-
                 
@@ -2378,37 +2378,39 @@
          id: 'di
-splay
+rection
 ',%0A             
@@ -2462,39 +2462,35 @@
         label: '
-Display
+RTL
 ',%0A             
@@ -2553,37 +2553,39 @@
           var di
-splay
+rection
  = element.getAt
@@ -2587,37 +2587,33 @@
 getAttribute(%22di
-splay
+r
 %22);%0A            
@@ -2634,32 +2634,32 @@
   if (di
-splay
+rection
  === '
-block
+rtl
 ') %7B%0A   
@@ -4173,16 +4173,55 @@
 , 'tex')
+, dialog.getValueOf('basic', 'display')
 );%0A     

Add RTL option