﻿// ----------------------------------------------
// File:		ToolTip.js
// Author:		Nathan Derksen
// Description:	Tool tip component
// Example:
// ToolTip.getInstance().showToolTip("Bracelet<br>$1,200");
// ----------------------------------------------

// ----------------------------------------------
// Function:	ToolTip()
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
function ToolTip()
{
	this.pInstance = null;
	this.pHandle = null;
	this.pShowIntervalId = 0;
	this.pHideIntervalId = 0;
	this.pMaxWidth = 160;
	this.pShowDelay = 400;
	this.pHideDelay = 0;
	this.pContents = "";
	this.isVisible = false;
	this.lastX = -1;
	this.lastY = -1;
}

// ----------------------------------------------
// Function:	ToolTip.getInstance()
// Author:		Nathan Derksen
// Description:	Singleton access method
// Inputs:		<none>
// Returns:		<ToolTip> Handle to a single ToolTip instance
// ----------------------------------------------
ToolTip.getInstance = function()
{
	if (!this.pInstance)
	{
		this.pInstance = new ToolTip();
	}
	return this.pInstance;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.init = function(elementId)
{
	this.pHandle = document.getElementById(elementId);
	this.isVisible = false;
	
	if (document.all)
	{
		document.onmousemove = moveToolTipIE;
	}
	else if (document.getElementById)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = moveToolTipFF;
	}
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.showToolTip = function(contents)
{
	this.pContents = contents;
	this.isVisible = true;
	clearTimeout(this.pShowIntervalId);
	this.pShowIntervalId = setTimeout("delayedToolTipShow()", this.pShowDelay);
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.showToolTipFollowup = function()
{
	repositionToolTip(this.lastX, this.lastY);

	var contents = this.pContents;

	if (this.pHandle)
	{
		this.pHandle.innerHTML = contents;
		this.pHandle.style.visibility = "visible";
		
		if (this.pMaxWidth > 0 && this.pHandle.offsetWidth > this.pMaxWidth)
		{
			this.pHandle.style.width = this.pMaxWidth + "px";
		}
		else
		{
			this.pHandle.style.width = "";
		}
		
		// Need to make sure tool tip has been repositioned, in case mouse has not moved since the tooltip
		// was initially called		
		if (this.lastY < 0)
		{
			// do nothing
		}
		else if (document.all)
		{
			if (!document.documentElement.scrollTop)
			{
				repositionToolTip(this.lastX+document.body.scrollLeft, this.lastY+document.body.scrollTop);
			}
			else
			{
				repositionToolTip(this.lastX+document.documentElement.scrollLeft, this.lastY+document.documentElement.scrollTop);
			}
		}
		else if (document.getElementById)
		{
			repositionToolTip(this.lastX, this.lastY);
		}
	}
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.hideToolTip = function()
{
	clearTimeout(this.pShowIntervalId);
	this.hideToolTipFollowup();
	this.isVisible = false;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.hideToolTipFollowup = function()
{
	if (this.pHandle)
	{
		this.pHandle.style.visibility = "hidden";
	}
};


// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.getToolTip = function()
{
	if (this.pHandle)
	{
		return this.pHandle;
	}
	return null;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTip.prototype.setMaxWidth = function(maxWidth)
{
	this.pMaxWidth = maxWidth;
};

// ----------------------------------------------
// ----------------------------------------------
function repositionToolTip(x, y)
{
	var toolTipHandle = ToolTip.getInstance().getToolTip();
	if (toolTipHandle != null)
	{
		var xOffsetRight = 10;
		var xOffsetLeft = -11;
		var yOffsetTop = 5;
		var yOffsetBottom = 10;
		var xOffset = xOffsetRight;
		var yOffset = yOffsetBottom;
		var newX;
		var newY;
		
		var width = BrowserUtils.getBrowserWidth();
		var height = BrowserUtils.getBrowserHeight();
		var toolTipWidth = toolTipHandle.offsetWidth;
		var toolTipHeight = toolTipHandle.offsetHeight;
		
		if (x >= width/2 - 8)
		{
			xOffset = xOffsetLeft - toolTipWidth;
		}
		
		newX = x + xOffset;
		newY = y + yOffset;
		
		if (newX + toolTipWidth >= width - 18)
		{
			newX = width - toolTipWidth - 18;
		}	
		
		if (toolTipHandle)
		{
			toolTipHandle.style.left = newX + "px";
			toolTipHandle.style.top = newY + "px";
		}
	}
}

// ----------------------------------------------
// ----------------------------------------------
function delayedToolTipShow()
{
	ToolTip.getInstance().showToolTipFollowup();
}

// ----------------------------------------------
// ----------------------------------------------
function delayedToolTipHide()
{
	ToolTip.getInstance().hideToolTipFollowup();
}


// ----------------------------------------------
// ----------------------------------------------
function moveToolTipFF(e)
{
	var toolTipHnd = ToolTip.getInstance();
	toolTipHnd.lastX = e.pageX;
	toolTipHnd.lastY = e.pageY;
	
	if (ToolTip.getInstance().isVisible == true)
	{
		repositionToolTip(e.pageX, e.pageY);
	}
}

// ----------------------------------------------
// ----------------------------------------------
function moveToolTipIE() 
{
	var toolTipHnd = ToolTip.getInstance();
	toolTipHnd.lastX = event.clientX;
	toolTipHnd.lastY = event.clientY;
	
	if (ToolTip.getInstance().isVisible == true)
	{
		if (!document.documentElement.scrollTop)
		{
			repositionToolTip(event.clientX+document.body.scrollLeft, event.clientY+document.body.scrollTop);
		}
		else
		{
			repositionToolTip(event.clientX+document.documentElement.scrollLeft, event.clientY+document.documentElement.scrollTop);
		}
	}
}