// Prerequisites /////////////
// eventManager.js
//     vector.js
/////////////////////////////

var includedFiles_tooltips = true;

//Class containing tooltip settings
var tooltips = new clsTooltips(tooltips);

onDomReady(createTooltip, 0);

//Creates a div to use as a tooltip when the dom is loaded
function createTooltip()
{
	//Create a new div to use as the tooltip
	tooltips.tooltipDiv = document.createElement('div');
	
	//Set some essential styles and apply id before it is attached to document
	tooltips.tooltipDiv.setAttribute('id', 'tooltip');
	
	//Attach the tooltip
	document.body.appendChild(tooltips.tooltipDiv);
	
	//Get reference to div in the document
	tooltips.tooltipDiv = document.getElementById('tooltip');
	tooltips.style = tooltips.tooltipDiv.style;

	//Register event to hide tooltip when leaving the page // makes sure tooltip is not visible if use presses back to return to page which had tooltip
	addEvent(document.body, 'unload', tooltips_hideTooltip, true)

	//Register event to look for tooltips
	addEvent(document.body, 'mouseover', tooltips_addByEvent, true);

}

function clsTooltips()
{
	// clsTooltips.PROTOTYPE =========================================================================
	//this.addTooltips = _addTooltips;
	//this.attachTooltip = _attachTooltip;
	//this.displayTooltip = _displayTooltip;
	//this.hideTooltip = _hideTooltip;
	
	// clsTooltips.VARIABLES =========================================================================
	this.tooltipDiv = null;
	this.currentObject = null;
	
	this.mouseOffsetX = 12;
	this.mouseOffsetY = 12;
	
	this.opacity = 0;
	
	this.delayIn = 300;
	this.fadeIn = 400;
	
	this.delayOut = 0;
	this.fadeOut= 200;
	
	this.fadeUpdateRate = 20;
	this.currentFadeTime = 0;
	
	this.currentDelayTimer = null;
	this.currentFadeTimer = null;
	
	this.maxOpacity = 84;
	
	this.maxWidth = "30%"
	
	if (navigator.appName=="Microsoft Internet Explorer")
	{
		this.fadeUpdateRate = 100;
	}

}

//Checks an object for a title and creates tooltip if found
function tooltips_addByEvent(e)
{
	//Get handle to calling object
	currentObject = getEventTarget(e, "target");
	
	//Check parent objects for titles
	while ((currentObject.title == "") && (currentObject.tagName != "BODY") && (currentObject.tagName != "HTML"))
	{
		currentObject = currentObject.parentNode;
	}

	//If we found an object with a title, set its tooltip
	if (currentObject.title != "")
	{				
		//If current object doesnt have dedicated tooltip text, set tooltip to same as title
		currentObject.tooltip = currentObject.title;
		
		//Remove objects title to prevent browser tooltips
		currentObject.title = "";

		//Set handle to current mouse over object in tooltips object
		tooltips.currentObject = currentObject;

		//Attach event listeners // uses eventManager.js
		addEvent(currentObject, 'mouseover', tooltips_displayTooltip, true);
		addEvent(currentObject, 'mousemove', tooltips_updateTooltip, true);
		addEvent(currentObject, 'mouseout', tooltips_hideTooltip, true);
		
		tooltips_delayIn(e);
	}
}

//Updates tooltip for any element which has had a tooltip set
function tooltips_displayTooltip(e)
{			
	//Get handle to calling object
	tooltips.currentObject = getEventTarget(e, "target");
	
	tooltips_delayIn(e);
}

//Makes tooltip transparent then prepares the fade in effect
function tooltips_delayIn(e)
{
	clearTimeout(tooltips.currentDelayTimer);
	clearTimeout(tooltips.currentFadeTimer);
	
	tooltips_setOpacity(0);
	tooltips.currentFadeTime = 0;
	
	tooltips.currentDelayTimer = setTimeout(tooltips_fadeIn, tooltips.delayIn);
	tooltips_updateTooltip(e);
}

function tooltips_fadeIn()
{
	if (tooltips.currentFadeTime < tooltips.fadeIn)
	{
		if (tooltips.currentFadeTime == 0)
		{
			tooltips.currentFadeTimer = setInterval(tooltips_fadeIn, tooltips.fadeUpdateRate)
		}
		tooltips_setOpacity(tooltips.opacity + tooltips.maxOpacity / (tooltips.fadeIn / tooltips.fadeUpdateRate) );
		tooltips.currentFadeTime += tooltips.fadeUpdateRate;
	}
	else
	{
		tooltips_setOpacity(tooltips.maxOpacity);
		tooltips.currentFadeTime = 0;
		clearInterval(tooltips.currentFadeTimer);
	}
}

//Updates tooltip for any element which has had a tooltip set
function tooltips_updateTooltip(e)
{			
	//Set the content of the tooltip if it has changed
	if (tooltips.tooltipDiv.innerHTML != tooltips.currentObject.tooltip)
	{
		//Set content of the tooltip
		tooltips.tooltipDiv.innerHTML = tooltips.currentObject.tooltip;
		
		//Set tooltip text of object to same as actual tooltip
		//accounts for formatting applied by browser
		tooltips.currentObject.tooltip = tooltips.tooltipDiv.innerHTML;
	}

	//If the tooltip has content, set some properties then display it
	if ((tooltips.tooltipDiv.innerHTML != "") && (tooltips.tooltipDiv.innerHTML != "undefined"))
	{
		//Get position of mouse
		if (e.pageX)
		{
			var mouseX = e.pageX;
			var mouseY = e.pageY;
		}
		else
		{
			var mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			var mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		//Set position of tooltip
		tooltips.tooltipDiv.style.left = (mouseX + tooltips.mouseOffsetX) + "px";
		tooltips.tooltipDiv.style.top = (mouseY + tooltips.mouseOffsetY) + "px";
		
		//Show the tooltip
		if (tooltips.tooltipDiv.style.display != "block")
		{
			tooltips.tooltipDiv.style.display = "block";
		}
		
		//Reposition the tooltip if it is off the edge of the screen
		//Has to be done after display has been set to block, otherwise it has no width (but it should be transparent anyway)
		var newPosition;
		if (tooltips.tooltipDiv.offsetLeft + tooltips.tooltipDiv.offsetWidth + tooltips.mouseOffsetX - document.getElementsByTagName("html")[0].scrollLeft > getInnerWidth())
		{
			newPosition = mouseX - tooltips.mouseOffsetX - tooltips.tooltipDiv.offsetWidth;
			if (newPosition < 0)
			{
				newPosition = document.getElementsByTagName("html")[0].scrollWidth - tooltips.tooltipDiv.offsetWidth;
			}
			tooltips.tooltipDiv.style.left = newPosition + "px";
		}
		
		if (tooltips.tooltipDiv.offsetTop + tooltips.tooltipDiv.offsetHeight + tooltips.mouseOffsetY - document.getElementsByTagName("html")[0].scrollTop > getInnerHeight())
		{
			tooltips.tooltipDiv.style.top = (mouseY - tooltips.mouseOffsetY - tooltips.tooltipDiv.offsetHeight) + "px";
		}
	}
}

//Hide the tooltip
function tooltips_hideTooltip(e)
{
	clearTimeout(tooltips.currentDelayTimer);
	clearTimeout(tooltips.currentFadeTimer);
	tooltips.currentDelayTimer = setTimeout(tooltips_fadeOut, tooltips.delayOut);
}

function tooltips_fadeOut()
{
	tooltips.cuurentObject = null;
	tooltips.tooltipDiv.style.display = "none";	
}

//Function to set opacity of tooltip
function tooltips_setOpacity(newOpacity)
{
	tooltips.opacity = newOpacity;
	tooltips.style.opacity = (newOpacity / 100);
    tooltips.style.MozOpacity = (newOpacity / 100);
    tooltips.style.KhtmlOpacity = (newOpacity / 100);
    tooltips.style.filter = "alpha(opacity=" + newOpacity + ")"; 
}

//Get height of document
function getInnerHeight()
{
	if (typeof window.innerHeight != 'undefined')
	{
		return window.innerHeight;
	}
	if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}		 
	else
	{
		return document.getElementsByTagName('body')[0].clientHeight;
	}
}

//Get width of document
function getInnerWidth()
{
	if (typeof window.innerWidth != 'undefined')
	{
		return window.innerWidth;
	}
	if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}		 
	else
	{
		return document.getElementsByTagName('body')[0].clientWidth;
	}
}
