(function($) {
    // Fix dialog auto with (for IE)
    var _setData = $.ui.dialog.prototype._setData;
    var open = $.ui.dialog.prototype.open;
    $.extend($.ui.dialog.prototype, {
        _fixWidth: function() {
            var hasAutoWidthBug = ((this.options.width == 'auto') && (this.element.outerWidth() != this.uiDialog.width()));
            if (hasAutoWidthBug) {
                this._setData('width', this.element.outerWidth());
                this._setData('position', this._getData('position'));
            }
        },
        _setData: function(key, value) {
            _setData.apply(this, arguments);
            if ((key == 'width') && (value == 'auto')) this._fixWidth();
        },
        open: function() {
            open.apply(this, arguments);
            this._fixWidth();
        }
    });

    // Add Fade effect and apply it to all dialogs when they open
    $.extend($.effects, {
        fade: function(o) { 
            return this.queue(function() { 
                var el = $(this);
                $.extend(o, { mode: 'show' });
                
                // Hack for jagged text in IE 6/7/8
                var callback = function() {
                    if ($.browser.msie) $(this).get(0).style.removeAttribute('filter');
                };
                
                (o.mode == 'show') ? el.fadeIn(o.duration, callback) : el.fadeOut(o.duration, callback);
                el.queue('fx', function() { el.dequeue(); }); 
                el.dequeue(); 
            });
        }
    });
    $.extend($.ui.dialog.defaults, {
        show: 'fade'
    });
    
})(jQuery);
