Ext.override(Ext.form.HtmlEditor, {
    /**
     * Set a readonly mask over the editor
     * @param {Boolean} readOnly - True to set the read only property, False to switch to the editor
     */
    setReadOnly: function(readOnly){
        if(readOnly){
            this.syncValue();
            var roMask = this.wrap.mask();
            roMask.dom.style.filter = "alpha(opacity=100);"; //IE
            roMask.dom.style.opacity = "1"; //Mozilla
            roMask.dom.style.background = "white";
            roMask.dom.style.overflow = "scroll";
            roMask.dom.innerHTML = this.getValue();
            this.el.dom.readOnly = true;
        } else {
            if(this.rendered){
                this.wrap.unmask();
            }
            this.el.dom.readOnly = false;
        }
    },
   // private
    onRender : function(ct, position){
        Ext.form.HtmlEditor.superclass.onRender.call(this, ct, position);
        this.el.dom.style.height = this.height+'px';
        this.el.dom.style.border = '0 none';
        this.el.dom.setAttribute('tabIndex', -1);
        this.el.addClass('x-hidden');
        if(Ext.isIE){ // fix IE 1px bogus margin
            this.el.applyStyles('margin-top:-1px;margin-bottom:-1px;')
        }
        this.wrap = this.el.wrap({
            cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
        });

        this.createToolbar(this);

        this.tb.items.each(function(item){
           if(item.itemId != 'sourceedit'){
                item.disable();
            }
        });

        var iframe = document.createElement('iframe');
        iframe.name = Ext.id();
        iframe.frameBorder = 'no';

        iframe.src=(Ext.SSL_SECURE_URL || "javascript:false");

        this.wrap.dom.appendChild(iframe);

        this.iframe = iframe;

        if(Ext.isIE){
            iframe.contentWindow.document.designMode = 'on';
            this.doc = iframe.contentWindow.document;
            this.win = iframe.contentWindow;
        } else {
            this.doc = (iframe.contentDocument || window.frames[iframe.name].document);
            this.win = window.frames[iframe.name];
            this.doc.designMode = 'on';
        }
        this.doc.open();
        this.doc.write(this.getDocMarkup())
        this.doc.close();

        var task = { // must defer to wait for browser to be ready
            run : function(){
                if(this.doc.body || this.doc.readyState == 'complete'){
                    Ext.TaskMgr.stop(task);
                    this.doc.designMode="on";
                    this.initEditor.defer(10, this);
                }
            },
            interval : 10,
            duration:10000,
            scope: this
        };
        Ext.TaskMgr.start(task);

        if(!this.width){
            this.setSize(this.el.getSize());
        }

        this.setReadOnly(this.readOnly);

        // insert image
        editor = this;
        this.getToolbar().insertButton(16, {
            iconCls: 'icon-insertimage',
            handler: function(b,e) {
                insertimagewindow = new Ext.Window({
                    title: 'Wstaw obrazek',
                    layout: "fit",
                    closable : true,
                    items: [
                        insertimageFormPanel = new Ext.form.FormPanel({
                            fileUpload: true,
                            autoHeight: true,
                            autoWidth: true,
                            labelWidth: 70,
                            frame:true,
                            bodyStyle:'padding:5px 5px 0',
                            labelAlign: 'right',
                            defaultType: 'textfield',
                            defaults: {width: 220},
                            items: [
                                {
                                    fieldLabel: 'Adres URL',
                                    name: 'image',
                                    id: 'image'
                                },{
                                    fieldLabel: 'Alt',
                                    name: 'alt',
                                    id: 'alt',
                                    value: 'image',
                                    allowBlank: false
                                },{
                                    xtype: 'combo',
                                    fieldLabel: 'Align',
                                    name: 'alignval',
                                    id: 'alignval',
                                    displayField:'alignval',
                                    valueField: 'alignval',
                                    mode: 'local',
                                    triggerAction: 'all',
                                    emptyText:'---wybierz---',
                                    value: 'none',
                                    store: new Ext.data.SimpleStore({
                                        fields: ['alignval'],
                                        data : [
                                            ['none'],
                                            ['left'],
                                            ['right']
                                        ]
                                    }),
                                    allowBlank: false
                                },{
                                    fieldLabel: 'Upload',
                                    xtype: 'fileuploadfield',
                                    id: 'imagename',
                                    emptyText: 'Wybierz plik...',
                                    name: 'imagename',
                                    buttonText: '',
                                    buttonCfg: {
                                        iconCls: 'icon-upload'
                                    }
                                }
                            ],
                            buttons: [
                                {
                                    handler: function() {
                                        if(insertimageFormPanel.getForm().isValid()) {
                                            insertimageFormPanel.getForm().submit({
                                                url: config['homedir']+'/admin/actions/uploadimage',
                                                method: 'get',
                                                params: {image: Ext.getCmp('image').getValue()},
                                                waitMsg: 'Wysyłanie danych...',
                                                success: function(fp, o){
                                                    if(o.result.imgname == 'none') {
                                                        if(!Ext.getCmp('image').getValue()) errorMsg('wpisz URL');
                                                        else {
                                                            align = (Ext.getCmp('alignval').getValue() != 'none') ? 'align="'+Ext.getCmp('alignval').getValue()+'"' : '';
                                                            editor.execCmd('InsertHTML', '<img src="'+Ext.getCmp('image').getValue()+'" alt="'+Ext.getCmp('alt').getValue()+'" '+align+' />');
                                                            insertimagewindow.close();
                                                        }
                                                    } else {
                                                        editor.execCmd('InsertHTML', '<img src="'+o.result.imgname+'" alt="'+o.result.alt+'" '+o.result.align+' />');
                                                        insertimagewindow.close();
                                                    }
                                                },
                                                failure: function(fp, o){errorMsg(o.result.ret);}
                                            });
                                        } else {
                                            errorMsg('Formularz wypełniono niepoprawnie. Popraw błędy w oznaczonych polach.'); 
                                        }
                                    },
                                    text: 'Dodaj'
                                }
                            ]
                        })
                    ],
                    closable: true,
                    width: 350,
                    modal: true,
                    autoHeight: true
                });
                insertimagewindow.show();
            }
        });
    }
});

