(function() {
  function Dialog (overlay) {
    this.overlay = $(overlay);

    var that = this;
    $('.close').live('click', function () { that.hide(); return false; });
    this.overlay.click(function () { that.hide(); });
    $(window).resize(function() { that.resize(); });
  };

  Dialog.prototype.display = function (html, option) {
    if (this.el) {
      this.el.show();
      return this;
    }

    this.r = "dlg";
    if ('#' === html[0])
      html = $(html).html();

    this.el = $('<div id="dialog" class=' + this.r + '>' + html + '</div>');
    this.el.appendTo('body');
    return this;
  };

  Dialog.prototype.show = function () {
    this.el.show(); 
    this.overlay.removeClass('hide');
    this.resize();
  };

  Dialog.prototype.hide = function () {
    $('.' + this.r).hide();
    this.overlay.addClass('hide');
  };

  Dialog.prototype.resize = function () {
    var el = this.el;

    if (el) {
      el.css({
        top:  (window.innerHeight / 2) - el.height() / 2,
        left: (window.innerWidth / 2) - el.width() / 2
      });
    }
  };

  window['Dialog'] = function (overlay) {
    return new Dialog(overlay);
  };

}());

