
  /**
   * @class Всплывающее окно
   * @author Ildar Salavatullin <ildar.inbox@gmail.ru>
   *
   * @param {Object} opts Начальные аттрибуты объекта
   */
  MWindow = function (opts) {
    this.animationSpeed = 300;

    this._canClose = true;
    
    this._containerElement = null;
    this._titleElement = null;
    this._closeElement = null;
    this.contentElement = null;
    
    opts = opts || {};
    for (var i in opts)
      this[i] = opts[i];
      
    this._createContainer ();
  }
  
  MWindow._htmlOverflowX = null;
  MWindow._htmlOverflowY = null;
  MWindow._bodyMarginRight = null;
  
  MWindow._showingTrobber = false; // показывается ли троббер в данный момент
  MWindow._showingWindowsCount = 0; // кол-во отображаемых окон в данный момент
  
  MWindow.dontFixScrollBars = false;
  
  /**
   * Показать индикатор загрузки ajax
   */
  MWindow.showTrobber = function () {
    MWindow._createTrobberContainer ();
    $("#MWindowTrobberOverlay, #MWindowTrobber").fadeIn (300);
    MWindow._showingTrobber = true;
    MWindow._fixPageScrollBars ();
  }
  
  /**
   * Скрыть индикатор загрузки ajax
   */
  MWindow.hideTrobber = function () {
    $("#MWindowTrobberOverlay, #MWindowTrobber").fadeOut (300, function () {
      MWindow._showingTrobber = false;
      MWindow._fixPageScrollBars ();
    });
  }
  
  /**
   * Установить/получить заголовок окна
   * @param {String} [title]
   */
  MWindow.prototype.title = function (title) {
    if (typeof title == 'undefined')
      return this._titleElement.text ();
    else {
      this._titleElement.text (title);
      return this;
    }
  }
  
  /**
   * Установить/получить содержимое окна
   * @param {String} [content]
   */
  MWindow.prototype.content = function (content) {
    if (typeof content == 'undefined')
      return this.contentElement.html ();
    else {
      this.contentElement.html (content);
      return this;
    }
  }
  
  /**
   * Показать окно
   */
  MWindow.prototype.show = function () {
    this._containerElement.fadeIn (this.animationSpeed);
    MWindow._showingWindowsCount++;
    MWindow._fixPageScrollBars ();
    return this;
  }
  
  /**
   * Скрыть окно
   */
  MWindow.prototype.hide = function () {
    var self = this;
    this._containerElement.fadeOut (this.animationSpeed, function () {
      MWindow._showingWindowsCount--;
      MWindow._fixPageScrollBars ();
    });
  }

















  
  /**
   * Создать контейнер этого окна
   */
  MWindow.prototype._createContainer = function () {
    var self = this;
    this._containerElement = $([
      '<div class="MWindowOverlay">',
        '<div class="MWindowOverlayInner">',
          '<div class="MWindowContainer">',
            '<div class="MWindowTitle">',
              '<span class="MWindowTitleText">Загрузка…</span>',
              '<span class="MWindowClose">Закрыть</span>',
            '</div>',
            '<div class="MWindowContent">',
            '</div>',
          '</div>',
        '</div>',
      '</div>',
    ].join ('')).appendTo ('body');
    this._titleElement = this._containerElement.find ('.MWindowTitleText');
    this._closeElement = this._containerElement.find ('.MWindowClose');
    this.contentElement = this._containerElement.find ('.MWindowContent');
    
    this._closeElement.click (function () {
      self.hide ();
    });
  }

  /**
   * Создать контейнер для троббера
   */
  MWindow._createTrobberContainer = function () {
    if ($('#MWindowTrobber').size () == 0) {
      $([
        '<div id="MWindowTrobberOverlay"></div>',
        '<div id="MWindowTrobber"></div>',
      ].join ('')).appendTo ('body');
    }
  }
  
  /**
   * Изменить скроллбары страницы
   */
  MWindow._touchPageScrollBars = function () {
    if (MWindow.dontFixScrollBars === false && MWindow._htmlOverflowX === null) {
      MWindow._htmlOverflowX = $('html').css ('overflow-x');
      MWindow._htmlOverflowY = $('html').css ('overflow-y');
      $('html').css ('overflow-x', 'hidden').css ('overflow-y', 'hidden');
      MWindow._bodyMarginRight = $('body').css ('margin-right');
      $('body').css ('margin-right', '0');
    }
  }
  
  /**
   * Вернуть на место скроллбары страницы
   */
  MWindow._untouchPageScrollBars = function () {
    if (MWindow.dontFixScrollBars === false) {
      $('html').css ('overflow-x', MWindow._htmlOverflowX).css ('overflow-y', MWindow._htmlOverflowY);
      $('body').css ('margin-right', MWindow._bodyMarginRight);
      MWindow._htmlOverflowX = null;
      MWindow._htmlOverflowY = null;
      MWindow._bodyMarginRight = null;
    }
  }

  /**
   * Разрулить скроллбары страницы
   */
  MWindow._fixPageScrollBars = function () {
    if (MWindow._showingWindowsCount == 0 && MWindow._showingTrobber == false)
      MWindow._untouchPageScrollBars ();
    else
      MWindow._touchPageScrollBars ();
  }

















