﻿// ----------------------------------------------
// File:		ToolTipAdvanced.js
// Author:		Nathan Derksen
// Description:	Tool tip component
// Example:
// ToolTipAdvanced.getInstance().showToolTip("Bracelet<br>$1,200");
// ----------------------------------------------

// ----------------------------------------------
// Function:	ToolTipAdvanced()
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
function ToolTipAdvanced()
{
	this.pInstance = null;
	this.pHandle = null;
	this.pShowIntervalId = 0;
	this.pHideIntervalId = 0;
	this.pMaxWidth = 300;
	this.pShowDelay = 600;
	this.pHideDelay = 0;
	this.pContents = "";
	this.pId = "";
	this.moveWithCursor = true;
	this.isVisible = false;
	this.lastX = -1;
	this.lastY = -1;
}

// ----------------------------------------------
// Function:	ToolTipAdvanced.getInstance()
// Author:		Nathan Derksen
// Description:	Singleton access method
// Inputs:		<none>
// Returns:		<ToolTipAdvanced> Handle to a single ToolTipAdvanced instance
// ----------------------------------------------
ToolTipAdvanced.getInstance = function()
{
	if (!this.pInstance)
	{
		this.pInstance = new ToolTipAdvanced();
	}
	return this.pInstance;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.init = function(elementId)
{
	this.pHandle = document.getElementById(elementId);
	this.isVisible = false;
	
/*	if (document.all)
	{
		document.onmousemove = moveToolTipAdvancedIE;
	}
	else if (document.getElementById)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = moveToolTipAdvancedFF;
	}*/
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.showToolTip = function(contents, x, y, alignment, id, isBig)
{
	this.pContents = contents;
	this.pId = id;
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
	this.pShowIntervalId = setTimeout("delayedToolTipAdvancedShow()", this.pShowDelay);

	this.lastX = x;
	this.lastY = y;
	this.pHandle.innerHTML = contents;
	repositionToolTipAdvanced(x, y, false);
	var baseStyle = (isBig) ? "toolTipAdvancedBig":"toolTipAdvanced";
	
	if (typeof(alignment) != "undefined" && alignment == "right")
	{
		this.pHandle.className = baseStyle+" toolTipRight";
	}
	else
	{
		this.pHandle.className = baseStyle;
	}
//	this.showToolTipFollowup();
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.showToolTipFollowup = function()
{
	var contents = this.pContents;

	if (this.pHandle)
	{
		this.pHandle.style.visibility = "visible";
		this.isVisible = true;
		
		// 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
		{
			repositionToolTipAdvanced(this.lastX, this.lastY);
		}
	}
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.hideToolTip = function()
{
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
//	this.pHideIntervalId = setTimeout("delayedToolTipAdvancedHide()", this.pHideDelay);
	this.hideToolTipFollowup();
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.hideToolTipNow = function()
{
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
	this.hideToolTipFollowup();
};


// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.hideToolTipFollowup = function()
{
	if (this.pHandle)
	{
		this.pHandle.style.visibility = "hidden";
		this.isVisible = false;
	}
};


// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.getToolTip = function()
{
	if (this.pHandle)
	{
		return this.pHandle;
	}
	return null;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.getContents = function()
{
	return this.pContents;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.setMaxWidth = function(maxWidth)
{
	this.pMaxWidth = maxWidth;
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.setMouseOutHandler = function(handler)
{
	var id = this.pId;
	var target = this.pHandle;

	if (typeof(handler) == "undefined" || handler == null)
	{
		delete this.pHandle.onmouseout;
	}
	else
	{
		this.pHandle.onmouseout = function(e)
		{
			if (!e) var e = window.event;
//			var target = (window.event) ? e.srcElement : e.target;
			if (target.nodeName != 'DIV') return;
			var relatedTarget = (e.relatedTarget) ? e.relatedTarget : e.toElement;

			while (relatedTarget != target && relatedTarget.nodeName != 'BODY')
			{
				relatedTarget = relatedTarget.parentNode;
			}
			if (relatedTarget == target) return;
			// Mouseout took place when mouse actually left layer
			// Handle event
			handler();
		}
	}
};

// ----------------------------------------------
// ----------------------------------------------
ToolTipAdvanced.prototype.setVisible = function(isVisible)
{
	clearTimeout(this.pShowIntervalId);
	clearTimeout(this.pHideIntervalId);
	if (isVisible == true)
	{
		this.pHandle.style.visibility = "visible";
		this.isVisible = true;
	}
	else
	{
		this.pHandle.style.visibility = "hidden";
		this.isVisible = false;
	}
}

// ----------------------------------------------
// ----------------------------------------------
function delayedMouseOutHandler()
{
}

// ----------------------------------------------
// ----------------------------------------------
function repositionToolTipAdvanced(x, y, useOffset)
{
	var toolTipHandle = ToolTipAdvanced.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;
		
		if (useOffset == true)
		{
			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;
			}
		}
		else
		{
			newX = x;
			newY = y;
		}
		
		if (toolTipHandle)
		{
			toolTipHandle.style.left = newX + "px";
			toolTipHandle.style.top = newY + "px";
		}
	}
}

// ----------------------------------------------
// ----------------------------------------------
function delayedToolTipAdvancedShow()
{
	ToolTipAdvanced.getInstance().showToolTipFollowup();
}

// ----------------------------------------------
// ----------------------------------------------
function delayedToolTipAdvancedHide()
{
	ToolTipAdvanced.getInstance().hideToolTipFollowup();
}