/** Creates a new tool tip. */
  protected void init() {
    toolTipConfig = new ToolTipConfig();
    lastActive = new Date();
    monitorWindowResize = true;

    anchorEl = XElement.as(DOM.createDiv());
    appearance.applyAnchorStyle(anchorEl);
    getElement().appendChild(anchorEl);
  }
  private Point getTargetXY(int targetCounter) {
    if (toolTipConfig.getAnchor() != null) {
      targetCounter++;
      int[] offsets = getOffsets();
      Point xy =
          (toolTipConfig.isAnchorToTarget() && !toolTipConfig.isTrackMouse())
              ? getElement().getAlignToXY(target.getElement(), getAnchorAlign(), null)
              : targetXY;

      int dw = XDOM.getViewWidth(false) - 5;
      int dh = XDOM.getViewHeight(false) - 5;
      int scrollX = XDOM.getBodyScrollLeft() + 5;
      int scrollY = XDOM.getBodyScrollTop() + 5;

      int[] axy = new int[] {xy.getX() + offsets[0], xy.getY() + offsets[1]};
      Size sz = getElement().getSize();
      Region r = XElement.as(target.getElement()).getRegion();

      appearance.removeAnchorStyle(anchorEl);

      // if we are not inside valid ranges we try to switch the anchor
      if (!((toolTipConfig.getAnchor() == Side.TOP
                  && (sz.getHeight() + offsets[1] + scrollY < dh - r.getBottom()))
              || (toolTipConfig.getAnchor() == Side.RIGHT
                  && (sz.getWidth() + offsets[0] + scrollX < r.getLeft()))
              || (toolTipConfig.getAnchor() == Side.BOTTOM
                  && (sz.getHeight() + offsets[1] + scrollY < r.getTop()))
              || (toolTipConfig.getAnchor() == Side.LEFT
                  && (sz.getWidth() + offsets[0] + scrollX < dw - r.getRight())))
          && targetCounter < 4) {
        if (sz.getWidth() + offsets[0] + scrollX < dw - r.getRight()) {
          toolTipConfig.setAnchor(Side.LEFT);
          return getTargetXY(targetCounter);
        }
        if (sz.getWidth() + offsets[0] + scrollX < r.getLeft()) {
          toolTipConfig.setAnchor(Side.RIGHT);
          return getTargetXY(targetCounter);
        }
        if (sz.getHeight() + offsets[1] + scrollY < dh - r.getBottom()) {
          toolTipConfig.setAnchor(Side.TOP);
          return getTargetXY(targetCounter);
        }
        if (sz.getHeight() + offsets[1] + scrollY < r.getTop()) {
          toolTipConfig.setAnchor(Side.BOTTOM);
          return getTargetXY(targetCounter);
        }
      }

      appearance.applyAnchorDirectionStyle(anchorEl, toolTipConfig.getAnchor());

      targetCounter = 0;
      return new Point(axy[0], axy[1]);

    } else {
      int x = targetXY.getX();
      int y = targetXY.getY();

      int[] mouseOffset = toolTipConfig.getMouseOffset();
      if (mouseOffset != null) {
        x += mouseOffset[0];
        y += mouseOffset[1];
      }
      return new Point(x, y);
    }
  }