/**
 * Adds extensions to the jQuery ui dialog widget, including:
 * * Styled buttons
 * * Auto-repositioning
 */
;(function($) {
  $.extend($.ui.dialog.prototype, {
    /**
     * The original init implementation
     */
    _initWithoutAutoOptions: $.ui.dialog.prototype._init,
    
    /**
     * Overrides widget initialization to update the size configuration based
     * on the current size of the element
     * 
     * TODO: It would be nice if this wasn't necessary.  I think it wouldn't
     * be necessary if we could figure out how to do inline-blocks in ie
     * properly
     */
    _init: function() {
      var width = parseInt(this.element.css('width'));
      var height = parseInt(this.element.css('height'));
      
      // Automatically resize to the width/height of the configured element
      if (width > 0 && height > 0) {
        this.options.autoResize = false;
        this.options.width = width;
        this.options.height = height;
      }
      this._initWithoutAutoOptions();
    },
    
    /**
     * The original open implementation
     */
    openWithoutAutoReposition: $.ui.dialog.prototype.open,
    
    /**
     * Overrides the opening of dialogs in order to add an event for
     * automatically repositioning the dialog when the window size changes
     */
    open: function() {
      this.openWithoutAutoReposition();
      
      if (this.options.autoReposition) {
        var self = this;
        $(window).bind('resize.dialog', function() {
          // Re-position based on the configured value
          self._position(self.options.position);
        });
      }
    },
    
    
    
    size: $.ui.dialog.prototype._size,
    
    /**
     * 
     * 
     */
    resize: function() {
      this.element.height('');
      this._size();
      this._position('center');
    }
  });
  
  $.extend($.ui.dialog.defaults, {
    /**
     * Whether to automatically re-position the dialog when the window is
     * resized
     * 
     * @type Boolean
     * @default false
     */
    autoReposition: false
  });
})(jQuery);
