// ---- ToolTipManager V 1.0 ---------------------
// ToolTip manager for Onidji Framework
// © 2003-2009 Onidji®. All rights reserved.
var CLASS_TOOLTIP = 'onidji-tooltip';

function ToolTipManager(id)
{
	this.toolTipNode = null;
	if(id==null)
		this.id = '1';
	else	
		this.id = id;
	this.getNode = ToolTipManager_getNode;
	this.getId = ToolTipManager_getId;
	this.show = ToolTipManager_show;
	this.hide = ToolTipManager_hide;
	this.mdx = 25; // minimal distance X with cursor
	this.mdy = 25; // minimal distance Y with cursor
	this.defineImg = ToolTipManager_defineImg;
	this.getDimensions = ToolTipManager_getDimensions;
	this.getWindowWidth = ToolTipManager_getWindowWidth;
	this.getWindowHeight = ToolTipManager_getWindowHeight;
}

function ToolTipManager_defineImg(src)
{
	this.hide();
	deleteAllChild(this.getNode());
	var b = new NodeBuilder('img');
	b.addAttribute('src',src);
	this.getNode().appendChild(b.node);
}

function ToolTipManager_getNode()
{
	if(this.toolTipNode == null)
	{
		var n = getNode(window.document,'body',null,null);
		var b = new NodeBuilder('div');
		b.addAttribute('id',this.getId());
		b.addAttribute('class',CLASS_TOOLTIP);
		this.toolTipNode = b.node;
		this.toolTipNode.style.display = '';
		this.toolTipNode.style.position = 'absolute';
		n.appendChild(this.toolTipNode);
	}	
	return this.toolTipNode;
}

function ToolTipManager_getId()
{
	return 'onidji_tooltip_'+this.id;
}

function ToolTipManager_getDimensions(element)
{
	  element = element;
    var display = element.style.display;
    if (display != 'none' && display != null) // BUG SAFARI
      return {width: element.offsetWidth, height: element.offsetHeight};
    var els = element.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    var originalDisplay = els.display;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = 'block';
    var originalWidth = element.clientWidth;
    var originalHeight = element.clientHeight;
    els.display = originalDisplay;
    els.position = originalPosition;
    els.visibility = originalVisibility;
    return {width: originalWidth, height: originalHeight};
}

function ToolTipManager_getWindowWidth()
{
        var r;
        if (typeof(window.innerWidth) == 'number') {
            r = window.innerWidth;
        } else if(document.documentElement && document.documentElement.clientWidth) {
            r = document.documentElement.clientWidth;
        } else if(document.body && document.body.clientWidth){
            r = document.body.clientWidth;
        }
        return innerWidth;
}

function ToolTipManager_getWindowHeight()
{
        var r;
        if (typeof(window.innerHeight) == 'number') {
            r = window.innerHeight;
        } else if(document.documentElement && document.documentElement.clientHeight) {
            r = document.documentElement.clientHeight;
        } else if(document.body && document.body.clientHeight){
            r = document.body.clientHeight;
        }
        return r;
}

function ToolTipManager_show(event)
{
	var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	var mdx = this.mdx;
	var mdy = this.mdy;
	
	var scroll = 0;
  if (typeof(window.pageYOffset) == 'number') {
      scroll = window.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
      scroll = document.documentElement.scrollTop;
  } else if (document.body && document.body.scrollTop) {
       scroll = document.body.scrollTop;
  }
	var dim = this.getDimensions(this.getNode());
	var width = dim.width;
	var height = dim.height;
  if ((width + x) >= (this.getWindowWidth() - mdx - 20))
	{
      x = x - width;
      x = x - mdx;
  } else {
      x = x + mdx;
  }
  
  if ((height + y) >= (scroll + this.getWindowHeight() - mdy)
      && (y - scroll - height + mdy > 0)) {
      y = Math.min(y - height + mdy, scroll + y - (height - (this.getWindowHeight() - y)) - mdy);
  } else if ((height + y) >= (scroll + this.getWindowHeight() - mdy)
      && (scroll + y - height + mdy > 0)) {
      y = Math.min(scroll + y - height + mdy, scroll + y - (height - (this.getWindowHeight() - y)) - mdy);
  } else if ((height + y) >= (scroll + this.getWindowHeight() - mdy)){
      y = scroll + y - (height - (this.getWindowHeight() - y)) - mdy;
  } else if ((y < height) && (y + mdy > scroll + this.getWindowHeight() - mdy)) {
      y = y + (height - (height + y)) + mdy + y;
  } else {
      y = y + mdy;
  }
	this.getNode().style.top = y+'px';
	this.getNode().style.left = x+'px';
	this.getNode().style.display = '';
}

function ToolTipManager_hide()
{
	this.getNode().style.display = 'none';
}

