"use strict";

function ClickTooltip(strHTMLContents, mxParentPositionReference)
{
	if(typeof strHTMLContents=="undefined")
		return;
	
	this._elParentPositionReference=(typeof mxParentPositionReference=="string")?document.getElementById(mxParentPositionReference):mxParentPositionReference;
	if(!this._elParentPositionReference)
		return;
	if(this._elParentPositionReference.tagName && this._elParentPositionReference.tagName=="A")
		return;

	if(!strHTMLContents.length)
		return;
		
		
	this._elDivTooltip=document.createElement("div");
	this._elDivTooltip.style.position="fixed";
	this._elDivTooltip.className="ClickTooltip";
	this.setHTML(strHTMLContents);
	
	
	this._elParentPositionReference.classList.add("ClickTooltip_underline");
	
	
	var _self=this;
	this._fnOnMouseMoveDocument=function(event){_self._onMouseMoveDocument(event);};
	document.addEventListener("mousemove", this._fnOnMouseMoveDocument);
	document.addEventListener("click", function(event){_self._onDocumentClick(event);});
	this._elParentPositionReference.addEventListener("click", function(event){_self._onClick(event);});
	this._elParentPositionReference.addEventListener("mouseover", function(event){_self._onMouseOver(event);});
	this._elParentPositionReference.addEventListener("mouseout", function(event){_self._onMouseOut(event);});
	this._elParentPositionReference.addEventListener("mousemove", function(event){_self._onMouseMove(event);});
}

ClickTooltip.prototype={
	
	_bHasMouseInput: false,
	
	_bMouseOver: false,
	
	_fnOnMouseMoveDocument: null,
	
	_bVisible: false,
	
	_elParentPositionReference: null,
	
	_elDivTooltip: null,
	
	_bInitiatedByMouseOver: false,
	
	setHTML: function(strHTMLContents)
	{
		this._elDivTooltip.innerHTML=strHTMLContents;
	},
	
	show: function()
	{
		if(ClickTooltip.currentClickTooltip && ClickTooltip.currentClickTooltip!==this)
			ClickTooltip.currentClickTooltip.hide();
		ClickTooltip.currentClickTooltip=this;
		
		
		this._bVisible=true;
		
		this._reposition();
		
		var _self=this;
		setTimeout(
			function(){
				document.body.appendChild(_self._elDivTooltip);
			}, 
			20
		);
	},
	
	hide: function()
	{
		this._bVisible=false;
		this._bInitiatedByMouseOver=false;
		if(this._elDivTooltip.parentNode)
			this._elDivTooltip.parentNode.removeChild(this._elDivTooltip);
		
		ClickTooltip.currentClickTooltip=null;
	},
	
	isVisible: function()
	{
		return this._bVisible;
	},


	/**
	 * @return {HTMLElement}
	 */
	getTooltipElement: function()
	{
		return this._elDivTooltip;
	},
	
	_reposition: function()
	{
		if(this._bHasMouseInput)
			return;
		
		this._elDivTooltip.style.position="static";
		
		var elParent=this._elParentPositionReference;
		
		var offsetLeft=elParent.offsetLeft;
		var offsetTop=elParent.offsetTop;
		while(elParent.offsetParent)
		{
			elParent=elParent.offsetParent;
			offsetLeft+=elParent.offsetLeft;
			offsetTop+=elParent.offsetTop;
		}
		
		elParent=this._elParentPositionReference.parentNode;
		while(elParent.parentNode)
		{
			offsetLeft-=elParent.scrollLeft;
			offsetTop-=elParent.scrollTop;
			elParent=elParent.parentNode;
			
			if(elParent && getComputedStyle(elParent).position=="fixed")
				break;
		}

		this._elDivTooltip.style.left=offsetLeft+"px";
		this._elDivTooltip.style.top=(offsetTop+this._elParentPositionReference.offsetHeight)+"px";
	},
	
	_onClick: function(event)
	{
		if(this._bInitiatedByMouseOver)
			return;
		
		if(this._elDivTooltip.parentNode)
		{
			this.hide();
			return;
		}
		
		for(var element=event.target; element; element=element.parentNode)
		{
			if(element===this._elParentPositionReference)
			{
				this.show();
				return;
			}
		}
	},
	
	_onMouseOver: function(event)
	{
		//if(!this._bMouseOver)
			//document.addEventListener("mousemove", this._fnOnMouseMoveDocument);
		this._bMouseOver=true;
		
		this._onMouseMoveDocument(event);
		
		if(event.stopPropagation)
			event.stopPropagation();
		
		this._bHasMouseInput=true;
		
		this._bInitiatedByMouseOver=true;
		this.show();
	},
	
	_onMouseMove: function(event)
	{
		if(event.stopPropagation && ClickTooltip.currentClickTooltip===this)
			event.stopPropagation();
		
		this._elDivTooltip.style.left=(event.clientX+10)+"px";
		this._elDivTooltip.style.top=(event.clientY+10)+"px";
	},
	
	_onMouseMoveDocument: function(event)
	{
		if(ClickTooltip.currentClickTooltip!==this)
			this.hide();
		
		//if(!this._bMouseOver)
			//return;
		
		this._bHasMouseInput=true;
		
		this._elDivTooltip.style.left=(event.clientX+10)+"px";
		this._elDivTooltip.style.top=(event.clientY+10)+"px";
	},
	
	_onMouseOut: function(event)
	{
		//if(this._bMouseOver)
			//document.removeEventListener("mousemove", this._fnOnMouseMoveDocument);
		this._bMouseOver=false;
		
		this._bHasMouseInput=true;
		
		this.hide();
	},
	
	_onDocumentClick: function(event)
	{
		if(!this._elDivTooltip.parentNode)
			return;
		
		if(!event)
			var event=window.event;
		var elTarget=event.target?event.target:event.srcElement;
		
		for(var element=elTarget; element; element=element.parentNode)
			if(element===this._elParentPositionReference)
				return;
		
		this.hide();
	}
};


/**
* Helps hide tooltips when mouseout events are not firing when the mouse is moving outside the parent element
* after the browser has lost focus.
*/
ClickTooltip.currentClickTooltip=null;


ClickTooltip.onLoadWindow=function(strHTMLContents, mxParentPositionReference){
	window.addEventListener("load", function(){
		new ClickTooltip(
			strHTMLContents,
			mxParentPositionReference
		);
	});
};