﻿
function myDictionary()
{
	this.getValueAt = getValueAt;
	this.containsKey = containsKey;
	this.getKeyIndex = getKeyIndex;
	this.getValue = getValue;
	this.add = add;
	this.getSize = getSize;
	
	this.keys = new Array();
	this.values = new Array();;
	
	function add(keyName, item)
	{
		this.keys.push(keyName);
		this.values.push(item);
	}
	
	function containsKey(keyName)
	{
		for(var i = 0; i < this.keys.length; i++)
		{
			if(this.keys[i] == keyName)
			{
				return true;
			}
		}
		
		return false;
	}
	
	function getKeyIndex(keyName)
	{
		for(var i = 0; i < this.keys.length; i++)
		{
			if(this.keys[i] == keyName)
			{
				return i;
			}
		}
		
		return -1;
	}
	
	function getValue(keyName)
	{
		var keyIndex = this.getKeyIndex(keyName);
		return this.values[keyIndex];
	}
	
	function getValueAt(keyIndex)
	{
		return this.values[keyIndex];
	}
	
	function getSize()
	{
		return this.keys.length;
	}
}

var toolTipElements = new myDictionary();

function registerIconForToolTip(elementId, tip)
{
	if(!toolTipElements.containsKey(elementId))
	{
		toolTipElements.add(elementId, new ToolTip(elementId, tip));
	}
}

function registerIconForInfoBox(elementId, info)
{
	if(!toolTipElements.containsKey(elementId))
	{
		toolTipElements.add(elementId, new InfoBox(elementId, info));
	}
}

function getToolTipByElementId(elementId)
{
	if(toolTipElements.containsKey(elementId))
	{
		return toolTipElements.getValue(elementId);
	}
	else
	{
		return null;
	}
}

function ToolTip(elementId, tip)
{
	this.iconElementId = elementId;
	this.tip = tip;
	this.getToolTipId = getToolTipId;
	this.show = show;
	this.hide = hide;
	this.updatePosition = updatePosition
	this.generateToolTipElement = generateToolTipElement;
	
	function updatePosition(mouseEventArgs)
	{
		var toolTipElement = document.getElementById(this.getToolTipId());
		Element.extend(toolTipElement);
		
		if(toolTipElement != null)
		{
			var newX = (mouseEventArgs.clientX + 20) + "px";
			var newY = (mouseEventArgs.clientY - 20) + "px";
			toolTipElement.style.left = newX;
			toolTipElement.style.top = newY;
			
			var offset = Element.cumulativeScrollOffset(toolTipElement);
			
			
			if(offset[0] > 0)
			{
				var offsetX = parseInt(offset[0]) + parseInt(newX);	
				
				toolTipElement.style.left = offsetX + "px";
			}
			if(offset[1] > 0)
			{
				
				var offsetY = parseInt(offset[1]) + parseInt(newY);
				
				toolTipElement.style.top = offsetY + "px";
			}
		}
	}
	
	function generateToolTipElement()
	{
		var divElement = document.createElement("div");
		divElement.setAttribute("class", "MenuToolTip");
		divElement.setAttribute("id", this.getToolTipId());
		divElement.appendChild(document.createTextNode(this.tip));
		
		divElement.className = "MenuToolTip";
		
		
		return divElement;
	}
	
	function show(mouseEventArgs)
	{
		var toolTipElement = document.getElementById(this.getToolTipId());
		
		if(toolTipElement == null)
		{
			var iconElement = document.getElementById(this.iconElementId);
			var iconContainer = iconElement.parentNode;
			iconContainer.appendChild(this.generateToolTipElement());
		}
		else
		{
			toolTipElement.style.display = "block";
		}
		
		this.updatePosition(mouseEventArgs);
	}
	
	function getToolTipId()
	{
		return this.iconElementId + "ToolTip";
	}
	
	function hide()
	{
		var toolTipElement = document.getElementById(this.getToolTipId());
		
		if(toolTipElement != null)
		{
			toolTipElement.style.display = "none";
		}
	}
}

function InfoBox(elementId, info)
{
	this.iconElementId = elementId;
	this.info = info;
	this.getToolTipId = getToolTipId;
	this.show = show;
	this.hide = hide;
	this.updatePosition = updatePosition
	this.generateToolTipElement = generateToolTipElement;
	this.isVisible = false;
	
	function updatePosition(mouseEventArgs)
	{
		var toolTipElement = document.getElementById(this.getToolTipId());
		Element.extend(toolTipElement);
		
		if(toolTipElement != null)
		{
			var newX = (mouseEventArgs.clientX + 30) + "px";
			var newY = (mouseEventArgs.clientY - 30) + "px";
			toolTipElement.style.left = newX;
			toolTipElement.style.top = newY;
			
			var offset = Element.cumulativeScrollOffset(toolTipElement);
			
			
			if(offset[0] > 0)
			{
				var offsetX = parseInt(offset[0]) + parseInt(newX);	
				
				toolTipElement.style.left = offsetX + "px";
			}
			if(offset[1] > 0)
			{
				
				var offsetY = parseInt(offset[1]) + parseInt(newY);
				
				toolTipElement.style.top = offsetY + "px";
			}
		}
	}
	
	function generateToolTipElement()
	{
		//To do: shouldn't use an anchor link to bring up the "hand" cursor, change this later.
	
		var closeLink = document.createElement("a");
		closeLink.setAttribute("href", "");
		closeLink.appendChild(document.createTextNode("close window"));
		
		var closeDiv = document.createElement("div");
		closeDiv.className = "CloseLink";
		closeDiv.appendChild(closeLink);
		
		var infoBoxContainer = document.createElement("div");
		infoBoxContainer.setAttribute("class", "InfoBox");
		infoBoxContainer.setAttribute("id", this.getToolTipId());
		infoBoxContainer.appendChild(document.createTextNode(this.info));
		infoBoxContainer.className = "InfoBox";
		infoBoxContainer.appendChild(closeDiv);
		
		closeDiv.setAttribute("onclick", "hideBox('" + this.iconElementId + "'); return(false);");
		closeDiv.parentNode.innerHTML = closeDiv.parentNode.innerHTML;
		
		return infoBoxContainer;
	}
	
	function show(mouseEventArgs)
	{
		if(!this.isVisible)
		{
			for(var boxIndex = 0; boxIndex < toolTipElements.getSize(); boxIndex++)
			{
				var infoBox = toolTipElements.getValueAt(boxIndex);
				if(infoBox.iconElementId != this.iconElementId)
				{
					infoBox.hide();
				}
			}
			
			var toolTipElement = document.getElementById(this.getToolTipId());
			
			if(toolTipElement == null)
			{
				toolTipElement = this.generateToolTipElement();
				var iconElement = document.getElementById(this.iconElementId);
				var iconContainer = iconElement.parentNode;
				iconContainer.appendChild(toolTipElement);
			}
			
			toolTipElement.style.display = "block";
			this.updatePosition(mouseEventArgs);
			this.isVisible = true;
		}
	}
	
	function getToolTipId()
	{
		return this.iconElementId + "InfoBox";
	}
	
	function hide()
	{
		var toolTipElement = document.getElementById(this.getToolTipId());
		
		if(toolTipElement != null)
		{
			toolTipElement.style.display = "none";
		}
		
		this.isVisible = false;
	}

}

function hideBox(elementId)
{
	var infoBox = getToolTipByElementId(elementId);
	if(infoBox != null)
	{
		infoBox.hide();
	}
	
	return false;
}
