
function Popup(width, height) {
  Popup.nbInstances++;
  this.classname = 'Popup';
  this.id = Popup.nbInstances;
  this.displayDelay = 500;
  this.hiddenDelay = NODELAY;
  this.autoHiddenDelay = 2000;
  this.autoHiddenEnabled = true;
  this.keepOnMouveOverEnabled = false;
  this.timer = new Timer(null, null, this);
  this.div = create('DIV');
  this.content = null;
  var tmpX, tmpY;
  this.mouseIsOver = false;
  this.xIsOut = false;
  this.yIsOut = false;
  
  this.init = function(width, height) {
    this.div.style.visibility = 'hidden';
    this.div.style.position = 'absolute';
    this.div.style.zIndex = 100;
    this.setDimension(width, height);
    this.div.onmouseover = Com_globalObjectMethod(this, 'mouseOver()');
    this.div.onmouseout = Com_globalObjectMethod(this, 'mouseOut()');
  };
  
  this.appendToElement = function(element) {
    if ((this.div.parentNode == null) && (element == null)) element = document.body;
    if ((this.div.parentNode != element) && (element != null)) element.appendChild(this.div);
  };
  
  this.getX = function(x, offset) {
    var s;
    if (x == null) x = scX;
    var width = Com_getWidth(this.div);
    if (!(parseInt(width) > 0)) width = 0;
    if (offset == null) offset = 0;
    s = (Com_getWidth(this.div.parentNode) - (x + width + offset) > 0 ? 1 : -1);
    this.outOfParent((s < 0 ? true : false), null);
    return(x - (s < 0 ? width : 0) + s * offset);
  };
  
  this.getY = function(y, offset) {
    var s;
    if (y == null) y = scY;
    var height = Com_getHeight(this.div);
    if (!(parseInt(height) > 0)) height = 0;
    if (offset == null) offset = 0;
    s = (Com_getHeight(this.div.parentNode) - (y + height + offset) > 0 ? 1 : -1);
    this.outOfParent(null, (s < 0 ? true : false));
    return(y - (s < 0 ? height : 0) + s * offset);
  };
  
  this.isVisible = function() {
    return (this.div.style.visibility == 'visible');
  };
  
  this.setDimension = function(width, height) {
    if (width != null) this.div.style.width = width;
    if (height != null) this.div.style.height = height;
  };
  
  this.setKeepOnMouveOver = function(value) {
    if (value) {
      this.tmpOnHidden = this.onHidden;
      this.onHidden = function() {
        if (this.mouseIsOver) return false;
        return this.tmpOnHidden();
      };
    } else {
      this.onHidden = this.tmpOnHidden;
    }
    this.keepOnMouveOverEnabled = value;
  };
  
  this.display = function(content, x, y, width, height) {
    this.content = content;
    tmpX = x; tmpY = y;
    this.setDimension(width, height);
    this.timer.launch('show()', this.displayDelay);
  };
  
  this.show = function() {
    this.onShow();
    this.appendToElement();
    this.div.innerHTML = this.content;
    this.position(tmpX, tmpY);
    this.div.style.visibility = 'visible';
    this.autoHidden();
  };
  
  this.hidden = function() {
    this.timer.clear();
    this.timer.launch('hiddenNow()', ((!this.keepOnMouveOverEnabled) || (this.hiddenDelay != NODELAY) ? this.hiddenDelay : 500));
  };
  
  this.autoHidden = function() {
    if (this.autoHiddenEnabled) {
      this.timer.clear();
      this.timer.launch('hiddenNow()', this.autoHiddenDelay);
    }
  };
  
  this.hiddenNow = function() {
    if (this.onHidden() != false) {
      this.div.style.visibility = 'hidden';
    }
  };
  
  this.position = function(x, y) {
    this.div.style.left = (x == null || true ? this.getX(x) : x);
    this.div.style.top = (y == null || true ? this.getY(y) : y);
  };
  
  this.move = function(x, y) {
    if (this.isVisible()) {
      this.position(x, y);
      this.autoHidden();
      return true
    }
    return false;
  };
  
  this.onShow = function() {};
  this.onHidden = function() {};
  this.tmpOnHidden = function() {};
  
  this.outOfParent = function(xIsOut, yIsOut) {
    if (xIsOut != null) this.xIsOut = xIsOut;
    if (yIsOut != null) this.yIsOut = yIsOut;
    this.onOutOfParent();
  };
  this.onOutOfParent = function() {};
  
  this.mouseOver = function() {
    this.mouseIsOver = true;
    if (this.keepOnMouveOverEnabled) this.timer.clear();
    this.onMouseOver();
  };
  this.onMouseOver = function() {};
  this.mouseOut = function() {
    this.mouseIsOver = false;
    this.timer.clear();
    this.autoHidden();
    this.onMouveOut();
  };
  this.onMouveOut = function() {};
  
  this.init(width, height);
  
}

Popup.nbInstances = 0;

function _Popup() {}
_Popup.prototype = Popup.prototype;
_Popup.prototype.base = Popup.prototype.constructor;
