var __EXPORT__ = function(self) {
  /** Registers a hint for the form-element */
  self.registerHint = hinter.registerHint;
}
hinter = {
  CONTAINER_ID: "hint",
  activeElement: null,
  position: "over",
  _container: null,
  _timeout: null
};
hinter.getContainer = function() {
  if (!this._container) {
    this._container = $(hinter.CONTAINER_ID);
    if (this._container == null) {
      throw new Error("Can't fint container");
    }
    connect(this._container, "onmouseover", function() {
      hinter.switchPosition();
    });
  }
  return this._container;
}
hinter.show = function(target, message, overClassName) {
  if (this._timeout) {
    clearTimeout(this._timeout);
    this._timeout = null;
  }
  var container = this.getContainer();
  container.getElementsByTagName("P").item(0).innerHTML = message.replace(/\n/g, "<br/>");
  if (this.position == "under") {
    this.displayUnder(target, overClassName);
  } else {
    this.displayOver(target, overClassName);
  }
  container.style.display = "block";
}
hinter.hide = function() {
  if (this._timeout) {
    clearTimeout(this._timeout);
    this._timeout = null;
  }
  this.activeElement = null;
  var container = this.getContainer();
  this._timeout = setTimeout(
    function() {
      container.style.display = "none";
    }, 10);
}
  hinter.displayOver = function(target, overClassName) {
  var container = this.getContainer();
  var pos = minikit.getElementPosition(target);
  var newPos = {
    x: pos.x,
    y: pos.y - container.offsetHeight
  };
  minikit.setElementPosition(container, newPos, "px");
  this.position = "over";
  if (typeof(overClassName) != "undefined") {
    container.className = overClassName;
  }
  this.activeElement = target;
}
    hinter.displayUnder = function(target, overClassName) {
  var container = this.getContainer();
  var pos = minikit.getElementPosition(target);
  var newPos = {
    x: pos.x,
    y: pos.y + target.offsetHeight
  };
  minikit.setElementPosition(container, newPos, "px");
  this.position = "under";
  if (typeof(overClassName) != "undefined") {
    container.className = overClassName;
  }
  this.activeElement = target;
}
hinter.switchPosition = function() {
  if (!this.activeElement) {
    return;
  }
  if (this.position == "under") {
    this.displayOver(this.activeElement);
  } else {
    this.displayUnder(this.activeElement);
  }
}
hinter.registerHint = function(field, message, target, eventType, overClassName) {
  if ($(hinter.CONTAINER_ID) == null) {
    callLater(.1, function() {
      var container = createDOM("div", {"id": hinter.CONTAINER_ID});
      container.appendChild(createDOM("p"));
      document.body.appendChild(container);
    });
  }
  var el = $(field);
  if (typeof(target) == "undefined" || target == null) {
    var target = $(field);
  }
  if (typeof(eventType) == "undefined" || eventType == null) {
    var eventType = "mouseover";
  }
  if (eventType == "focus") {
    connect(el, "onfocus", function() {
        hinter.show(target, message, overClassName);
    });
    connect(el, "onblur", function() {
      hinter.hide();
    });
  } else if (eventType == "mouseover") {
    connect(el, "onmouseover", function() {
        hinter.show(target, message, overClassName);
    });
    connect(el, "onmouseout", function() {
      hinter.hide();
    });
  } else {
    throw new Error("Event type not supported");
  }
}
__EXPORT__(window);