TC.Dialog = function( id, config ) {
    config = YAHOO.lang.merge( {
        width: '420px',
        fixedcenter: true,
        close: false,
        draggable: false,
        zindex: 4,
        modal: true,
        visible: false
    }, config );

    TC.Dialog.superclass.constructor.call( this, id, config );
};

YAHOO.extend( TC.Dialog, YAHOO.widget.Dialog, {
    setForm: function( form ) {
        for ( var k in form ) {
            var els = YAHOO.util.Dom.getElementsBy( function( el ) {
                var t = el.tagName.toUpperCase();
                return (( t == 'INPUT' || t == 'TEXTAREA' || t == 'SELECT') &&
                        el.name == k );
            }, '*', this.form );

            if ( els.length == 1 ) {
                var el = els[ 0 ];
                var tag = el.tagName.toUpperCase();

                switch( tag ) {
                    case 'INPUT':
                        var type = el.type;
                        if ( type == 'checkbox' && el.value == form[ k ] ) {
                            el.checked = true;
                        } else if ( type != 'radio' ) {
                            el.value = form[ k ];
                        }
                        break;
                    case 'TEXTAREA':
                        el.value = form[ k ];
                        break;
                    case 'SELECT':
                        for ( var i = 0; i < el.options.length; i++ ) {
                            if ( el.options[ i ].value == form[ k ] ) {
                                el.selectedIndex = i;
                                break;
                            }
                        }
                        break;
                }
            } else if ( els.length > 1 ) {
                var match = typeof form[ k ] == 'object' ?
                    function( vMatch ) {
                        for ( var idx in form[ k ] ) {
                            if ( form[ k ][ idx ] == vMatch )
                                return 1; 
                        }
                        return 0;
                    } :
                    function( vMatch ) {
                        return form[ k ] == vMatch ? 1 : 0;
                    };

                for ( var i = 0; i < els.length; i++ ) {
                    if ( match( els[ i ].value ) ) {
                        els[ i ].checked = true;
                    }
                }
            }
        }
    }
} );

/* EditorDialog should be used for any dialog in the Compose editor.
   It maintains state about the current selection (necessary for IE),
   and has helper methods for executing commands on the current
   editor. */
TC.EditorDialog = function( id, config ) {
    TC.EditorDialog.superclass.constructor.call( this, id, config );
    this.editor = null;
    this.bookmark = null;
};

YAHOO.extend( TC.EditorDialog, TC.Dialog, {
    setEditor: function( ed ) {
        this.editor = ed;

        /* IE 8 needs focus in order to store a range with the
           current collapsed caret location. */
        ed.focus();
        this.bookmark = this.editor.selection.getBookmark( 1 );
    },

    restoreSelection: function() {
        /* This can go away once we're fully on tinyMCE. */
        if ( typeof( tinymce ) == 'undefined' )
            return;

        if ( tinymce.isIE && this.editor && this.editor.selection ) {
            this.editor.selection.moveToBookmark( this.bookmark );
        }
    },

    execCommand: function( cmd, ui, val, a ) {
        a = a || {};
        a.skip_focus = 1;

        this.restoreSelection();
        return this.editor.execCommand( cmd, ui, val, a );
    },

    /* Both of these should be converted to do direct execCommand calls,
       once we're fully on tinyMCE. */
    insertHTML: function( html ) {
        this.restoreSelection();
        atp_editor.insertHTML( html );
    },

    insertImageHTML: function( html, a ) {
        this.restoreSelection();
        atp_editor.insertImageHTML( html, a );
    }
} );

