/*
dom_tooltip.js

DOM Tooltip by Chris Roberts
columcille@gmail.com
http://www.musterion.net/
*/

// glossTip_tipInitialized keeps things from happening before all the settings
// are set up.
var glossTip_tipInitialized = 0;

// The following variables track page/mouse position, etc
var glossTip_curPageX = 0;
var glossTip_curPageY = 0;
var glossTip_prevPosX = 0;
var glossTip_prevPosY = 0;
var glossTip_scrollPageY = 0;
var glossTip_viewPageX = 0;
var glossTip_viewPageY = 0;
var glossTip_viewScreenX = 0;
var glossTip_viewScreenY = 0;
var glossTip_totalScreenX = 0;
var glossTip_totalScreenY = 0;

// Stores the tip object when only one is being displayed
var glossTip_tipBox = "";

// glossTip_multipleTips sets whether or not to allow multiple tooltips to
// open at the same time. Set to 1, several can open. Set to 0, a new tooltip
// will erase the previous tooltip. 
var glossTip_multipleTips = 1;

// Should the user be allowed to drag around or resize divs? Note that some 
// of these may automatically be changed based on the move type you choose
// below.
var glossTip_allowDrag = 1;
var glossTip_allowResize = 1;

/*************************************************************************
 * glossTip_moveType has several settings:			                     *
 *																		 *
 * 1. Normal method, tooltip follows pointer							 *
 * 2. While the pointer is over the link or tooltip the tooltip is       *
 *    frozen															 *
 * 3. Pointer freeze, tooltip opens and is frozen where it was first	 *
 *    triggered															 *
 * 4. Fixed freeze, tooltip opens and is frozen at a fixed position		 *
 *																		 *
 * When tooltip is frozen, clicks outside of the tooltip will close it.  *
 * Also clicking "close" (or X or something I haven't decided yet) will  *
 * also close it.														 *
 *																		 *
 *************************************************************************/

var glossTip_moveType = 3;

// If you are specifying a fixed position for the tooltip, where should it
// go? Tooltip will be given a position attribute of Fixed and positioned
// relative to the screen based on these numbers. Value of 9999 will be
// ignored. Both must be set to something other than 9999 or both will be
// ignored and the default movetype changed to 3.
var glossTip_fixedFreezeX = 10;
var glossTip_fixedFreezeY = 20;

// Remaining variables should not be changed, they are used internally for
// handling tooltip data

// Various settings used when resizing or dragging around divs
var glossTip_dragDiv = 0;
var glossTip_resizeDiv = 0;
var glossTip_firstDrag = 1;
var glossTip_firstResize = 1;

var glossTip_resizeId = "";
var glossTip_dragId = "";

var glossTip_currentTipIdentifier = 999999;

var glossTip_Tips = new Array();

//Fixed position
var glossTip_posX = 300;
var glossTip_posY = 300;

window.document.onmousemove = glossTip_trackMouse;
window.document.onmouseup = glossTip_mouseIsUp;

function glossTip_mouseIsUp()
{
	glossTip_dragEnd();
	glossTip_resizeEnd();
}

function glossTip_trackMouse(e)
{
	if (glossTip_tipInitialized == 1)
	{
		var e = (e) ? e : ((event) ? event : null);
	
		glossTip_prevPosX = glossTip_curPageX;
		glossTip_prevPosY = glossTip_curPageY;
		
		// Functions do different things depending on whether or not a
		// doctype is present. Node type 10 is a doctype but IE reads
		// a doctype as Node type 8 - a comment.
		if (document.childNodes[0].nodeType == 10 || document.childNodes[0].nodeType == 8)
		{
			glossTip_curPageX = e.clientX + document.documentElement.scrollLeft;
			glossTip_curPageY = e.clientY + document.documentElement.scrollTop;
	
			glossTip_scrollPageY = document.documentElement.scrollTop;
	
			glossTip_viewPageX = e.clientX;
			glossTip_viewPageY = e.clientY;
	
			glossTip_viewScreenX = document.documentElement.clientWidth;
			glossTip_viewScreenY = document.documentElement.clientHeight;
	
			glossTip_totalScreenX = document.body.offsetWidth;
			glossTip_totalScreenY = document.body.offsetHeight;
		} else {
			glossTip_curPageX = e.clientX + document.body.scrollLeft;
			glossTip_curPageY = e.clientY + document.body.scrollTop;
	
			glossTip_scrollPageY = document.body.scrollTop;
	
			glossTip_viewPageX = e.clientX;
			glossTip_viewPageY = e.clientY;
	
			glossTip_viewScreenX = document.body.clientWidth;
			glossTip_viewScreenY = document.body.clientHeight;
	
			glossTip_totalScreenX = document.documentElement.scrollWidth;
			glossTip_totalScreenY = document.documentElement.scrollHeight;
		}
	
		if (glossTip_tipBox != "" && glossTip_tipBox.style.display == "block" && (glossTip_moveType == 1))
		{
			glossTip_moveTip();
		}
		
		if (glossTip_dragDiv == 1 && glossTip_resizeDiv != 1)
		{
			glossTip_dragTip();
		} else if (glossTip_dragDiv == 1 && glossTip_resizeDiv == 1) {
			glossTip_dragDiv = 0;
		}
	
		if (glossTip_resizeDiv == 1)
		{
			glossTip_resizeTip();
		}
	}
}

// glossTip_clearTip is used by the tippy plugin
function glossTip_clearTip(forced)
{
	if (glossTip_tipBox != "" && (glossTip_moveType < 3 || forced == true))
	{
		while (glossTip_tipBox.childNodes.length > 0)
		{
			glossTip_tipBox.removeChild(glossTip_tipBox.childNodes[0]);
		}
		
		document.body.removeChild(glossTip_tipBox);
		glossTip_tipIdentifier = glossTip_tipBox.getAttribute("id");
		glossTip_tipBox = "";
		
		glossTip_Tips[glossTip_tipIdentifier] = 0;

		glossTip_currentTipIdentifier = 999999;
		
		glossTip_widthOffset = 0;
		glossTip_heightOffset = 0;
		glossTip_tipBoxHeight = 0;
		glossTip_tipBoxWidth = 0;
	}
}

function glossTip_closeClick(glossTip_tipId)
{
	var glossTip_clicked = document.getElementById(glossTip_tipId);
		
	while (glossTip_clicked.childNodes.length > 0)
	{
		glossTip_clicked.removeChild(glossTip_clicked.childNodes[0]);
	}
	
	document.body.removeChild(glossTip_clicked);
	
	glossTip_currentTipIdentifier = 999999;
	
	glossTip_widthOffset = 0;
	glossTip_heightOffset = 0;
	glossTip_tipBoxHeight = 0;
	glossTip_tipBoxWidth = 0;
	
	glossTip_Tips[glossTip_tipId] = 0;
}

function glossTip_closeAllTips()
{
	glossTip_tipArrLen = glossTip_Tips.length;
	for (var glossTip_i = 0 ; glossTip_tipArrLen ; glossTip_i++)
	{
		glossTip_closeClick(glossTip_Tips[glossTip_i]);
	}
	
	glossTip_Tips = new Array();
}

// Create a new tooltip object
function glossTip_newTip(glossTip_tipIdentifier)
{
	// Juggle the flags. Some flags should be set, others should be unset
	// based on certain settings. These are coded based on my own preferences,
	// how I thought it should be done. Tweak it however you wish.
	
	// If movetype is 1 or 2 then multiple tips, dragging, and resizing should
	// be disabled
	if (glossTip_moveType == 1 || glossTip_moveType == 2)
	{
		glossTip_multipleTips = 0;
		glossTip_allowDrag = 0;
		glossTip_allowResize = 0;
	}
	
	// Fixed tips should only be used if a position was specified. If
	// not specified, switch type to 3.
	//
	// If fixed is used, tips should not be dragable or resizable, and
	// only one at a time should be used.
	if (glossTip_moveType == 4 && (glossTip_fixedFreezeX == 9999 || glossTip_fixedFreezeY == 9999))
	{
		glossTip_moveType = 3;
	} else if (glossTip_moveType == 4 && glossTip_fixedFreezeX != 9999 && glossTip_fixedFreezeY != 9999) {
		glossTip_allowDrag = 0;
		glossTip_allowResize = 0;
		glossTip_multipleTips = 0;
		
		glossTip_closeAllTips();
	}
	
	// If multiple tips can be opened, user should be able to drag them around
	// so they don't overlap each other
	if (glossTip_multipleTips == 1)
	{
		glossTip_allowDrag = 1;
	}
	
	if (glossTip_multipleTips == 0 && glossTip_currentTipIdentifier != 999999)
	{
		glossTip_closeClick(glossTip_currentTipIdentifier);
	}
	
	glossTip_tipBox = document.createElement("div");
	glossTip_tipBox.className = "glossTip_Tip";
	glossTip_tipBox.id = glossTip_tipIdentifier;
	glossTip_tipBox.style.position = "absolute";
	glossTip_tipBox.style.display = "none";
	glossTip_tipBox.onmousedown = function() { glossTip_bringForward(glossTip_tipIdentifier); };
	
	window.document.body.appendChild(glossTip_tipBox);
	
	glossTip_Tips[glossTip_tipIdentifier] = 1;
}

// Allow settings to be passed into a function rather than set directly
function glossTip_setSettings(glossTip_setMultipleTips, glossTip_setAllowDrag, glossTip_setAllowResize, glossTip_setMoveType, gloss_posX, gloss_posY)
{
	if (glossTip_setMoveType == "follow")
	{
		glossTip_moveType = 1;
	} else if (glossTip_setMoveType == "sticky") {
		glossTip_moveType = 2;
	} else {
		glossTip_moveType = 3;
	}
	
	if (glossTip_setMultipleTips == "true")
	{
		glossTip_multipleTips = 1;
	} else {
		glossTip_multipleTips = 0;
	}
	
	if (glossTip_setAllowDrag == "true")
	{
		glossTip_allowDrag = 1;
	} else {
		glossTip_allowDrag = 0;
	}
	
	if (glossTip_setAllowResize == "true")
	{
		glossTip_allowResize = 1;
	} else {
		glossTip_allowResize = 0;
	}
	//Michael Hill added this.  Imports positions set in database.
	glossTip_posX = parseInt(gloss_posX);
	glossTip_posY = parseInt(gloss_posY);
	glossTip_tipInitialized = 1;
}

// A unique identifier should be passed to glossTip_tipIdentifier so the script
// can tell the difference between tooltip requests.
function glossTip_toolText(glossTip_tipIdentifier, glossTip_tipText, glossTip_headerText, glossTip_headerLink)
{
	if (glossTip_Tips[glossTip_tipIdentifier] != 1)
	{
		glossTip_newTip(glossTip_tipIdentifier);
		
		// If the tooltip follows mouse, remove the height style attributes so the tooltip
		// can resize to fit the content.
		if (glossTip_moveType < 3)
		{
			glossTip_tipBox.style.height = "auto";
		}

		// Build the tip header
		var glossTip_tipHead = document.createElement("div");
		glossTip_tipHead.className = "glossTip_TipHeader";
		
		if (glossTip_moveType < 3)
		{
			glossTip_tipHead.style.height = "auto";
		}

		glossTip_tipHead.id = "glossTip_TipHeader_" + glossTip_tipIdentifier;
			
		if (glossTip_allowDrag == 1)
		{
			glossTip_tipHead.onmousedown = function() { glossTip_dragStart(glossTip_tipIdentifier); };
		}
		
		if (glossTip_headerText != "")
		{
			var glossTip_tipHeadText = document.createTextNode(glossTip_headerText);

			if (glossTip_headerLink == undefined || glossTip_headerLink == "")
			{
				glossTip_tipHead.appendChild(glossTip_tipHeadText);
			} else {
				var glossTip_tipHeaderLink = document.createElement("a");
				glossTip_tipHeaderLink.setAttribute("href", glossTip_headerLink);
				glossTip_tipHeaderLink.setAttribute("title", glossTip_tipHeadText);
				
				glossTip_tipHeaderLink.appendChild(glossTip_tipHeadText);
				glossTip_tipHead.appendChild(glossTip_tipHeaderLink);
			}
			
			glossTip_tipBox.appendChild(glossTip_tipHead);
		}
		
		if (glossTip_moveType > 2)
		{
			var glossTip_tipHeadClose = document.createElement("div");
			glossTip_tipHeadClose.className = "glossTip_tipHeadClose";
			glossTip_tipHeadClose.id = "glossTip_tipHeadClose_" + glossTip_tipIdentifier;
			glossTip_tipHeadClose.onmousedown = function() { glossTip_closeClick(glossTip_tipIdentifier); };
			
			glossTip_tipBox.appendChild(glossTip_tipHeadClose);
		}
		
		if (glossTip_allowResize == 1)
		{
			var glossTip_resizeBox = document.createElement("div");
			glossTip_resizeBox.className = "glossTip_resizeBox";
			glossTip_resizeBox.id = "glossTip_resizeBox_" + glossTip_tipIdentifier;
			glossTip_resizeBox.onmousedown = function() { glossTip_resizeStart(glossTip_tipIdentifier); };
			glossTip_tipBox.appendChild(glossTip_resizeBox);
		}
	
		var glossTip_tipBody = document.createElement("div");
		glossTip_tipBody.className = "glossTip_TipBody";
		
		if (glossTip_moveType < 3)
		{
			glossTip_tipBody.style.height = "auto";
		}
		
		glossTip_tipBody.id = "glossTip_TipBody_" + glossTip_tipIdentifier;
		glossTip_tipBody.onmouseup = function() { };
	
		glossTip_tipBody.innerHTML = glossTip_tipText;
	
		glossTip_tipBox.appendChild(glossTip_tipBody);
	
		glossTip_tipBox.style.visibility = "hidden";
		glossTip_tipBox.style.display = "block";
	
		if (glossTip_moveType == 4)
		{
			glossTip_tipBox.style.position = "fixed";
			glossTip_tipBox.style.top = glossTip_fixedFreezeY + "px";
			glossTip_tipBox.style.left = glossTip_fixedFreezeX + "px";
		} else {
			glossTip_moveTip();
		}
		
		glossTip_bringForward(glossTip_tipIdentifier);
		glossTip_tipBox.style.visibility = "visible";
		
		glossTip_currentTipIdentifier = glossTip_tipIdentifier;
	}
}

var glossTip_topIndex = 10;
var glossTip_tipOnTop = "";
function glossTip_bringForward(glossTip_forwardId)
{
	if (glossTip_forwardId != glossTip_tipOnTop)
	{
		glossTip_topIndex++;
		
		document.getElementById(glossTip_forwardId).style.zIndex = glossTip_topIndex;
		
		glossTip_tipOnTop = glossTip_forwardId;
	}
}

function glossTip_resizeStart(glossTip_tipIdentifier)
{
	glossTip_resizeDiv = 1;
	glossTip_resizeId = glossTip_tipIdentifier;
	document.onmousedown = function() { return false; };
	document.onselectstart = function() { return false; };
}

function glossTip_resizeEnd()
{
	glossTip_resizeDiv = 0;
	glossTip_firstResize = 1;
	glossTip_widthOffset = 0;
	glossTip_heightOffset = 0;
	glossTip_tipBoxHeight = 0;
	glossTip_tipBoxWidth = 0;
	glossTip_resizeId = "";
	document.onmousedown = null;
	document.onselectstart = null;
}

function glossTip_dragStart(glossTip_tipIdentifier)
{
	glossTip_dragDiv = 1;
	glossTip_dragId = glossTip_tipIdentifier;
	document.onmousedown = function() { return false; };
	document.onselectstart = function() { return false; };
}

function glossTip_dragEnd()
{
	glossTip_dragDiv = 0;
	glossTip_firstDrag = 1;
	glossTip_dragId = "";
	document.onmousedown = null;
	document.onselectstart = null;
}

function glossTip_dragTip()
{
	glossTip_tipBox = document.getElementById(glossTip_dragId);
	
	if (glossTip_firstDrag == 1)
	{
		glossTip_offLeft = glossTip_tipBox.offsetLeft;
		glossTip_offTop = glossTip_tipBox.offsetTop;

		glossTip_modLeft = glossTip_prevPosX - glossTip_offLeft;
		glossTip_modTop = glossTip_prevPosY - glossTip_offTop;

		glossTip_firstDrag = 0;
	}

	var glossTip_tipXloc = glossTip_curPageX - glossTip_modLeft;
	var glossTip_tipYloc = glossTip_curPageY - glossTip_modTop;

	glossTip_tipBox.style.left = glossTip_tipXloc + "px";
	glossTip_tipBox.style.top = glossTip_tipYloc + "px";
	glossTip_tipBox.blur();
}

var glossTip_widthOffset = 0;
var glossTip_heightOffset = 0;
var glossTip_tipBoxHeight = 0;
var glossTip_tipBoxWidth = 0;
function glossTip_resizeTip()
{
	var glossTip_ignoreHeight = 0;
	var glossTip_ignoreWidth = 0;
	
	glossTip_tipBox = document.getElementById(glossTip_resizeId);
	
	if (glossTip_firstResize == 1)
	{
		glossTip_tipBoxWidth = glossTip_tipBox.offsetWidth;
		glossTip_tipBoxHeight = glossTip_tipBox.offsetHeight;

		glossTip_firstResize = 0;
	}

	if (glossTip_curPageX > glossTip_prevPosX)
	{
		glossTip_widthOffset += (glossTip_curPageX - glossTip_prevPosX);
	} else {
		glossTip_widthOffset -= (glossTip_prevPosX - glossTip_curPageX);
	}

	if (glossTip_curPageY > glossTip_prevPosY)
	{
		glossTip_heightOffset += (glossTip_curPageY - glossTip_prevPosY);
	} else {
		glossTip_heightOffset -= (glossTip_prevPosY - glossTip_curPageY);
	}

	glossTip_newWidth = glossTip_tipBoxWidth + glossTip_widthOffset;
	glossTip_newHeight = glossTip_tipBoxHeight + glossTip_heightOffset;
	
	if (glossTip_newHeight < 100)
	{
		glossTip_ignoreHeight = 1; 
	}

	if (glossTip_newWidth < 100)
	{
		glossTip_ignoreWidth = 1; 
	}
	
	if (glossTip_ignoreHeight != 1)
	{
		glossTip_tipBox.style.height = glossTip_newHeight + "px";
		
		glossTip_bbDivHeight = glossTip_newHeight - 25;
		document.getElementById("glossTip_TipBody_" + glossTip_resizeId).style.height = glossTip_bbDivHeight + "px";
		
		glossTip_resizeButtonTop = glossTip_newHeight - 6;
		document.getElementById("glossTip_resizeBox_" + glossTip_resizeId).style.top = glossTip_resizeButtonTop + "px";
	}
	
	if (glossTip_ignoreWidth != 1)
	{
		glossTip_tipBox.style.width = glossTip_newWidth + "px";
		
		glossTip_bhDivWidth = glossTip_newWidth - 6;
		document.getElementById("glossTip_TipHeader_" + glossTip_resizeId).style.width = glossTip_bhDivWidth + "px";
		
		glossTip_bbDivWidth = glossTip_newWidth - 8;
		document.getElementById("glossTip_TipBody_" + glossTip_resizeId).style.width = glossTip_bbDivWidth + "px";
	
		glossTip_resizeButtonLeft = glossTip_newWidth - 6;
		document.getElementById("glossTip_resizeBox_" + glossTip_resizeId).style.left = glossTip_resizeButtonLeft + "px";
			
		glossTip_closeButtonLeft = glossTip_newWidth - 20;
		document.getElementById("glossTip_tipHeadClose_" + glossTip_resizeId).style.left = glossTip_closeButtonLeft + "px";
	}	
}

function glossTip_moveTip()
{
	var glossTip_tipHeight = glossTip_tipBox.offsetHeight;
	var glossTip_tipWidth = glossTip_tipBox.offsetWidth;
	
	//Position Offset
	var glossTip_tipXloc = glossTip_posX;
	if (domTip_curPageY >= glossTip_posY){
		var glossTip_tipYloc = domTip_curPageY;
	}
	else {
		var glossTip_tipYloc = glossTip_posY;
	}
	//var glossTip_tipXloc = 750;
	//var glossTip_tipYloc = 478;
	/*
	// If the tooltip extends off the side, pull it over
	if (glossTip_viewPageX + 10 + glossTip_tipWidth > glossTip_viewScreenX)
	{
		glossTip_tipXloc -= (glossTip_tipWidth + 15);
	}

	// If the tooltip will extend off the bottom of the screen, pull it back up.
	if (glossTip_viewPageY + 10 + glossTip_tipHeight > glossTip_viewScreenY)
	{
		var glossTip_pageDiff = (glossTip_viewPageY + 10 + glossTip_tipHeight - glossTip_viewScreenY);
		glossTip_tipYloc -= glossTip_pageDiff;
	}

	// If the tooltip extends off the bottom and the top, line up the top of
	// the tooltip with the top of the page
	if (glossTip_tipHeight > glossTip_viewScreenY)
	{
		glossTip_tipYloc = glossTip_scrollPageY + 5;
	}
	*/
	glossTip_tipBox.style.left = glossTip_tipXloc + "px";
	glossTip_tipBox.style.top = glossTip_tipYloc + "px";
	
}
