/*jslint laxbreak: true, eqeqeq: true, browser: true, undef: true */
/*extern $, $$, escape, ROOT */
/*extern MooEditable */

/* Script: mooEditableExtensions
 *
 *      Custom overrides for mooEditable defaults, including danish labels, an
 *      add video action and support for pasting from word
 */

(function () {

    if (window.MooEditable) {

        // danish labels for buttons
        MooEditable.Actions.bold.title = 'Fed';
        MooEditable.Actions.italic.title = 'Kursiv';
        MooEditable.Actions.underline.title = 'Understregning';
        MooEditable.Actions.strikethrough.title = 'Overstregning';
        MooEditable.Actions.undo.title = 'Fortryd';
        MooEditable.Actions.redo.title = 'Gentag';
        MooEditable.Actions.insertorderedlist.title = 'Opstilling med tal';
        MooEditable.Actions.insertunorderedlist.title = 'Opstilling med punkttegn';
        MooEditable.Actions.createlink.title = 'Lav et hyperlink';
        MooEditable.Actions.unlink.title = 'Fjern et hyperlink';

        MooEditable.Actions.createlink.dialogs = {
            alert: MooEditable.UI.AlertDialog.pass('Vælg den tekst du vil lave til et hyperlink.')
            ,prompt: function(editor){
                return MooEditable.UI.PromptDialog('Skriv URL', 'http://', function(url){
                    editor.execute('createlink', false, url.trim());
                });
            }
        };

        // define insert video action
        MooEditable.Actions.embedhtml = {
            title: 'Indsæt video',

            dialogs: {
                prompt: function(editor){
                    return MooEditable.UI.PromptDialog('Indsæt embed html her', 'http://', function(html){
                        editor.execute("insertimage", false, ROOT + 'images/pirattv.gif#' + escape(html.trim()));
                    });
                }
            },
            command: function(){
                this.dialogs.embedhtml.prompt.open();
            }
        };


        // custom function to clean bizarre word markup on paste 
        //      from this post in mooEditable group: http://groups.google.com/group/mooeditable/msg/57c163a9b7825897
        //      (originally from feceditor...)
        var cleanWord = function(html) {

            html = html.replace(/<o:p>\s*<\/o:p>/g, '');
            html = html.replace(/<o:p>[\s\S]*?<\/o:p>/g, '&nbsp;');

            // Remove mso-xxx styles.
            html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' );

            // Remove margin styles.
            html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, '' );
            html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" );

            html = html.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, '' );
            html = html.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" );

            html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" );

            html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" );

            html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" );

            html = html.replace( /\s*tab-stops:[^;"]*;?/gi, '' );
            html = html.replace( /\s*tab-stops:[^"]*/gi, '' );

            html = html.replace( /\s*face="[^"]*"/gi, '' );
            html = html.replace( /\s*face=[^ >]*/gi, '' );

            html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' );

            // Remove Class attributes
            html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");

            // Remove styles.
            html = html.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" );

            // Remove style, meta and link tags
            html = html.replace( /<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi, '' );
            html = html.replace( /<(?:META|LINK)[^>]*>\s*/gi, '' );

            // Remove empty styles.
            html =  html.replace( /\s*style="\s*"/gi, '' );

            html = html.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' );

            html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' );

            // Remove Lang attributes
            html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");

            html = html.replace( /<SPAN\s*>([\s\S]*?)<\/SPAN>/gi, '$1' );

            html = html.replace( /<FONT\s*>([\s\S]*?)<\/FONT>/gi, '$1' );

            // Remove XML elements and declarations
            html = html.replace(/<\\?\?xml[^>]*>/gi, '' );

            // Remove w: tags with contents.
            html = html.replace( /<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi, '' );

            // Remove Tags with XML namespace declarations: <o:p><\/o:p>
            html = html.replace(/<\/?\w+:[^>]*>/gi, '' );

            // Remove comments [SF BUG-1481861].
            html = html.replace(/<\!--[\s\S]*?-->/g, '' );

            html = html.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' );

            html = html.replace( /<H\d>\s*<\/H\d>/gi, '' );

            // Remove "display:none" tags.
            html = html.replace( /<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig, '' );

            // Remove language tags
            html = html.replace( /<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3");

            // Remove onmouseover and onmouseout events (from MS Word commentseffect)
            html = html.replace( /<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3");
            html = html.replace( /<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3");

            html = html.replace( /<H1([^>]*)>/gi, '<div$1><b><fontsize="6">' );
            html = html.replace( /<H2([^>]*)>/gi, '<div$1><b><fontsize="5">' );
            html = html.replace( /<H3([^>]*)>/gi, '<div$1><b><fontsize="4">' );
            html = html.replace( /<H4([^>]*)>/gi, '<div$1><b><fontsize="3">' );
            html = html.replace( /<H5([^>]*)>/gi, '<div$1><b><fontsize="2">' );
            html = html.replace( /<H6([^>]*)>/gi, '<div$1><b><fontsize="1">' );

            html = html.replace( /<\/H\d>/gi, '<\/font><\/b><\/div>' );

            // Transform <P> to <DIV>
            var re = new RegExp( '(<P)([^>]*>[\\s\\S]*?)(<\/P>)', 'gi' );   // Different because of a IE 5.0 error
            html = html.replace( re, '<div$2<\/div>' );

            // Remove empty tags (three times, just to be sure).
            // This also removes any empty anchor
            html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' );
            html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' );
            html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' );

            return html; 
        };

        // mix in word cleanup method in MooEditable.cleanup
        var originalCleanup = MooEditable.prototype.cleanup;

        MooEditable.implement({ cleanup: function (html) {
            html = cleanWord(html);
            html = originalCleanup.apply(this, [html]);
            return html;
        }});
    }
})();

