/* *******************************************************
#kesrequire(share/webwidgets/ui/core/Widget.js)
#kesrequire(share/webwidgets/ui/core/MChildrenHandling.js)
#kesrequire(share/webwidgets/ui/core/MWidgetPosition.js)
#kesrequire(share/webwidgets/core/PopupManager.js)
******************************************************* */

/**
 * Popup-Widget.
 */
qx.Class.define("nx4.webwidgets.ui.popup.Popup",
{
  extend : nx4.webwidgets.ui.core.Widget,
  include :
  [
    nx4.webwidgets.ui.core.MChildrenHandling,
    nx4.webwidgets.ui.core.MWidgetPosition
  ],

  construct : function()
  {
    this.base(arguments);

    //automatisch zum popupRoot hinzufuegen
    qx.core.Init.getApplication().getPopupRoot().add(this);

    //Initialize visibility
    this.initVisibility();
    this.initOutevents();

    nx4.webwidgets.core.PopupManager.getInstance().add(this);
    this.setZIndex(100);
  },

  properties :
  {
    //overridden
    visibility :
    {
      refine : true,
      init   : "excluded"
    },

    modal :
    {
      init : false,
      check : "Boolean",
      apply : "_applyModal"
    },

    blockerColor :
    {
      init : "#a0a0a0",
      apply : "_applyBlockerColor"
    },

    blockerOpacity :
    {
      init : 0.5,
      check : "Number",
      apply : "_applyBlockerOpacity"
    },

    outevents :
    {
      init : false,
      apply : "_applyOutevents"
    },

    hideDelay :
    {
      init : 2000
    },

    opener :
    {
      init : null,
      check : "nx4.webwidgets.ui.core.Widget",
      apply : "_applyOpener"
    }
  },

  members :
  {
    _createContainerElement : function()
    {
      var el = this.base(arguments);
      el.setStyle("position", "absolute");
      return el;
    },

    _applyModal : function(n, o)
    {
      if(n && !this._blocker)
      {
        this._blocker = new qx.bom.Blocker();
        this._blocker.setBlockerColor(this.getBlockerColor());
        this._blocker.setBlockerOpacity(this.getBlockerOpacity());
      }
    },

    _applyBlockerColor : function(n, o)
    {
      if(this._blocker)
        this._blocker.setBlockerColor(n);
    },

    _applyBlockerOpacity : function(n, o)
    {
      if(this._blocker)
        this._blocker.setBlockerOpacity(n);
    },

    _applyOutevents : function(n, o)
    {
      if(n)
      {
        this.addListener("mouseover", this._onMouseOver, this);
        this.addListener("mouseout",  this._onMouseOut, this);
        if(this.getOpener())
        {
          this.getOpener().addListener("mouseover", this._onMouseOver, this);
          this.getOpener().addListener("mouseout",  this._onMouseOut, this);
        }
      }
      else
      {
        this.removeListener("mouseover", this._onMouseOver, this);
        this.removeListener("mouseout",  this._onMouseOut, this);
        if(this.getOpener())
        {
          this.getOpener().removeListener("mouseover", this._onMouseOver, this);
          this.getOpener().removeListener("mouseout",  this._onMouseOut, this);
        }
        this._stopHideTimer();
      }
    },

    _applyOpener : function(n, o)
    {
      if(o)
      {
        o.removeListener("mouseover", this._onMouseOver, this);
        o.removeListener("mouseout",  this._onMouseOut, this);
      }

      if(n && this.getOutevents())
      {
        n.addListener("mouseover", this._onMouseOver, this);
        n.addListener("mouseout",  this._onMouseOut, this);
      }
    },

    _onMouseOver : function(e)
    {
      this._stopHideTimer();
    },

    _onMouseOut : function(e)
    {
      this._startHideTimer();
    },

    _startHideTimer : function()
    {
      this._hideTimer = qx.util.TimerManager.getInstance().start(this.exclude, null, this, null, this.getHideDelay());
    },

    _stopHideTimer : function()
    {
      if(this._hideTimer)
      {
        qx.util.TimerManager.getInstance().stop(this._hideTimer);
        this._hideTimer = null;
      }
    },

    _showOverlay : function()
    {
      qx.core.Init.getApplication().getPopupRoot().showBlocker(this);
    },

    _hideOverlay : function()
    {
      qx.core.Init.getApplication().getPopupRoot().hideBlocker(this);
    },

    show : function()
    {
      this.base(arguments);
      if(this.getOutevents())
        this._startHideTimer();

      if(this.getModal())
        this._showOverlay();
    },

    hide : function()
    {
      this.exclude();
    },

    exclude : function()
    {
      this.base(arguments);
      this._stopHideTimer();

      if(this.getModal())
        this._hideOverlay();
    }
  },

  destruct : function()
  {
    this.resetOpener();
    nx4.webwidgets.core.PopupManager.getInstance().remove(this);
  }
});

