// CONSTRUCTOR for the CalendarPopup Object

function ADM_Tooltip() {
	this.followMouse= true;
	this.offX= 8;
	this.offY= 12;
	this.tipID= "ADM_Tooltip";
	this.showDelay= 100;
	this.hideDelay= 200;
	this.ready=false;
	this.timer=null;
	this.tip=null;
	
	this.init = ADM_Tooltip_Fn_init;
	this.show = ADM_Tooltip_Fn_show ;
	this.writeTip = ADM_Tooltip_Fn_writeTip;
	this.positionTip = ADM_Tooltip_Fn_positionTip;
	this.hide = ADM_Tooltip_Fn_hide;
	this.toggleVis = ADM_Tooltip_Fn_toggleVis;
	this.trackMouse = ADM_Tooltip_Fn_trackMouse;

	return this;
}

function ADM_Tooltip_Fn_init(){
	if(document.createElement&&document.body&&typeof document.body.appendChild!="undefined"){
		if(!document.getElementById(this.tipID)){
			/*var el=document.createElement("DIV");
			el.id=this.tipID;
			document.body.appendChild(el);*/
		}
		this.ready=true;
	}
	
}
	
function ADM_Tooltip_Fn_show(e,msg){
	if(this.timer){
		clearTimeout(this.timer);
		this.timer=0;
	}
	//if(!this.ttready)
	//	return;
	this.tip=document.getElementById(this.tipID);
	if(this.followMouse)
		ADM_Event.add(document,"mousemove",this.trackMouse,true);
	this.writeTip("");
	this.writeTip(msg);
	ADM_Viewport.getAll();
	this.positionTip(e);
	this.timer=setTimeout("ADM_Tooltip.toggleVis('"+this.tipID+"', 'visible')", this.showDelay);
}
	
function ADM_Tooltip_Fn_writeTip(msg){
	if(this.tip&&typeof this.tip.innerHTML!="undefined")
		this.tip.innerHTML=msg;
}

function ADM_Tooltip_Fn_positionTip(e){
	if(this.tip&&this.tip.style){
		var x=e.pageX?e.pageX:e.clientX+ADM_Viewport.scrollX;
		var y=e.pageY?e.pageY:e.clientY+ADM_Viewport.scrollY;
		if(x+this.tip.offsetWidth+this.offX>ADM_Viewport.width+ADM_Viewport.scrollX){
			x=x-this.tip.offsetWidth-this.offX;
			if(x<0)x=0;
		}else 
			x=x+this.offX;
		if(y+this.tip.offsetHeight+this.offY>ADM_Viewport.height+ADM_Viewport.scrollY){
			y=y-this.tip.offsetHeight-this.offY;
			if(y<ADM_Viewport.scrollY)
				y=ADM_Viewport.height+ADM_Viewport.scrollY-this.tip.offsetHeight;
		}else 
			y=y+this.offY;
		this.tip.style.left=x+"px";
		this.tip.style.top=y+"px";
	}
}
	
function ADM_Tooltip_Fn_hide(){
	if(this.timer){
		clearTimeout(this.timer);
		this.timer=0;
	}
	this.timer=setTimeout("ADM_Tooltip.toggleVis('"+this.tipID+"', 'hidden')",this.hideDelay);
	if(this.followMouse)
		ADM_Event.remove(document,"mousemove",this.trackMouse,true);
	this.tip=null;
}
	
function ADM_Tooltip_Fn_toggleVis(id,vis){
	var el=document.getElementById(id);
	if(el)el.style.visibility=vis;
}
	
function ADM_Tooltip_Fn_trackMouse(e){
	e=ADM_Event.DOMit(e);
	ADM_Tooltip.positionTip(e);
}

var ADM_Tooltip = new ADM_Tooltip();
ADM_Tooltip.init();

//ADM_Event.add(window, "load", ADM_Tooltip.init, true);

//Tooltip
function ADM_Tooltip_Configure() {
	ADM_Tooltip.offX = 5;  
	ADM_Tooltip.offY = 5;
	ADM_Tooltip.followMouse = true;  
}

function ADM_Show_Tooltip(msg, e) {
  ADM_Tooltip_Configure();
  e=ADM_Event.DOMit(e);
  
  if ( typeof ADM_Tooltip == "undefined" || !ADM_Tooltip.ready ) return;
  ADM_Tooltip.show(e, msg);
}

function ADM_Hide_Tooltip() {
  if ( typeof ADM_Tooltip == "undefined" || !ADM_Tooltip.ready ) return;
  ADM_Tooltip.hide();
}

//HoverTip
function ADM_HoverTip_Configure() {
	ADM_Tooltip.offX = 4;  
	ADM_Tooltip.offY = 4;
	ADM_Tooltip.followMouse = false;  // must be turned off for hover-tip
	ADM_Event.add(window, "unload", ADM_Tooltip.HoverTip_unHookHover, true);
}

function ADM_Show_HoverTip(msg) {
  ADM_Show_HoverTip(msg, event);
}
function ADM_Show_HoverTip(msg, e) {
  ADM_HoverTip_Configure();
  
  if ( typeof ADM_Tooltip == "undefined" || !ADM_Tooltip.ready ) return;
  ADM_Tooltip.clearTimer();
  var tip = document.getElementById? document.getElementById(ADM_Tooltip.tipID): null;
  if ( tip && tip.onmouseout == null ) {
      tip.onmouseout = ADM_Tooltip.HoverTip_OutCheck;
      tip.onmouseover = ADM_Tooltip.HoverTip_clearTimer;
  }
  ADM_Tooltip.show(e, msg);
}

function ADM_Hide_HoverTip() {
  if ( typeof ADM_Tooltip == "undefined" || !ADM_Tooltip.ready ) return;
  ADM_Tooltip.timerId = setTimeout("ADM_Tooltip.hide()", 300);
}

ADM_Tooltip.HoverTip_OutCheck = function(e) {
  e = ADM_Event.DOMit(e);
  // is element moused into contained by tooltip?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if ( this != toEl && !contained(toEl, this) ) ADM_Tooltip.hide();
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}

//ADM_Tooltip.timerId = 0;
ADM_Tooltip.HoverTip_clearTimer = function() {
  if (ADM_Tooltip.timerId) { clearTimeout(ADM_Tooltip.timerId); ADM_Tooltip.timerId = 0; }
}

ADM_Tooltip.HoverTip_unHookHover = function () {
    var tip = document.getElementById? document.getElementById(ADM_Tooltip.tipID): null;
    if (tip) {
        tip.onmouseover = null; 
        tip.onmouseout = null;
        tip = null;
    }
}


