
MT.App.Editor = new Class( Object, {

    version: '1.2',

    mode: "iframe",
    changed: false,
    ToolbarSet: "Custom",


    init: function() {
        this.fck = new FCKeditor( "editor-content-textarea" ) ;
        this.fck.BasePath = StaticURI + "plugins/FCKeditor/fckeditor/";
        this.fck.Height = "100%";
        this.fck.ToolbarSet = this.ToolbarSet;
        this.fck.ReplaceTextarea();
        window.FCKeditor_OnComplete = this.getBoundMethod( "setEditorInstance" );
    },


    setEditorInstance: function( instance ) {
        this.editor = instance;
        /* this form submit override is keeping the entry form from submitting */
//        this.editor.LinkedField.form.onsubmit = this.getBoundMethod( "autoSave" );

        instance.Commands.GetCommand("Image").Execute = this.getBoundMethod( "insertImage" );
        instance.Commands.GetCommand("Flash").Execute = this.getBoundMethod( "insertFile" );
        instance.Events.AttachEvent( "OnSelectionChange", this.getBoundMethod( "setChanged" ) );

        /* hack */
        this.editor.textarea = {};

        this.clearDirty();
    },


    insertImage: function() {
        var div = DOM.getElement( "editor-content-enclosure" );
        app.openDialog( "__mode=list_assets&amp;_type=asset&amp;edit_field=" + div.getAttribute( "edit-field" )
            + "&amp;blog_id=" + div.getAttribute( "mt:blog-id" ) + "&amp;dialog_view=1&amp;filter=class&amp;filter_val=image" );
    },


    insertFile: function() {
        var div = DOM.getElement( "editor-content-enclosure" );
        app.openDialog( "__mode=list_assets&amp;_type=asset&amp;edit_field=" + div.getAttribute( "edit-field" )
            + "&amp;blog_id=" + div.getAttribute( "mt:blog-id" ) + "&amp;dialog_view=1" );
    },


    /* Called to save the content when the save button is pressed */
    autoSave: function( event ) {
        app.autoSave();
        return false;
    },


    /* Set the editor mode ( textarea | iframe ) between plain and rich text */
    setMode: function( mode ) {
        if ( this.mode == mode )
            return;
        this.toggleMode();
    },


    /* Toggle the editor mode */
    toggleMode: function() {
        this.mode = ( this.mode == "iframe" ) ? "textarea" : "iframe";
        this.editor.SwitchEditMode();
    },


    /* Clear the dirty flag on the editor ( dirty == modified ) */
    clearDirty: function() {
        this.editor.ResetIsDirty();
    },


    /* Proxy for the editor */
    execCommand: function( command, userInterface, argument ) {
        this.setChanged();
//        return this.fck.execCommand( command, userInterface, argument );
    },


    /* Called to set the dirty bit on the editor and call */
    setChanged: function() {
        this.changed = true;
        app.setDirty();
    },


    /* Focus the editor, forcing the cursor into the textarea or iframe */
    focus: function() {
        this.editor.Focus();
    },


    /* Get the editor content as html/xhtml */
    getHTML: function() {
        return this.editor.GetXHTML();
    },


    /* Get the editor content as xhtml ( if possible, else return html ) */
    getXHTML: function() {
        return this.editor.GetXHTML();
    },


    /* Set the html content of the editor */
    setHTML: function() {
        return this.editor.SetHTML.apply( this.editor, arguments );
    },


    /* Insert html into the editor, the editor should insert it at the cursor */
    insertHTML: function() {
        return this.editor.InsertHtml.apply( this.editor, arguments )
    },


    /* Check the dirty status */
    isDirty: function() {
        return this.editor.IsDirty();
    }


} );


App.singletonConstructor =
MT.App = new Class( MT.App, {


    /* set up the editor instance, and get the body, and extened fields */
    initEditor: function() {
        var mode = DOM.getElement( "convert_breaks" );
        DOM.addEventListener( mode, "change", this.getIndirectEventListener( "setTextareaMode" ) );

        this.editor = new this.constructor.Editor();
        this.editorInput = {
            content: DOM.getElement( "editor-input-content" ),
            extended: DOM.getElement( "editor-input-extended" )
        };
    },


    /* Called to fix the html in the editor before a save, or an insert.
     * inserted will be defined if called to fix inserted text
     */
    fixHTML: function( inserted ) { },


    /* called to switch the editor text between 'content' and 'extended' */
    setEditor: function( name ) {
        this.saveHTML( false );
        this.currentEditor = name;
        this.editor.setHTML( this.editorInput[ this.currentEditor ].value );
    },


    /* This is called when the user leaves the page
     * if the text in the editor has changed ( isDirty ) then
     * return text that will be used in a javascript confirm()
     */
    eventBeforeUnload: function( event ) {
        if ( this.editor.isDirty() )
            this.changed = true;
        if ( this.changed ) {
            if ( this.constructor.Editor )
                return event.returnValue = Editor.strings.unsavedChanges;
            else if ( window.Editor )
                return event.returnValue = window.Editor.strings.unsavedChanges;
        }

        return undefined;
    },


    /* This clears the editor's dirty flag */
    clearDirty: function() {
        this.editor.clearDirty();
        return arguments.callee.applySuper( this, arguments );
    },


    /* Called to set the editor html and switch to rich text */
    setEditorIframeHTML: function() {
        this.editor.setHTML( this.editorInput[ app.currentEditor ].value );
        this.editor.setMode( "iframe" );
        this.editor.focus();
    },


    /* Called to set the editor to non rich text mode */
    setTextareaMode: function( event ) {
    //    this.editor.textarea.setTextMode( event.target.value );
    }


} );
