/**
 * functions.js
 *
 * @author Tim Shields
 * @desc global js file for www.ricowi.com
 */


/**
 * set up event monitoring functions
 */
if (window.attachEvent) /* IE */
{
	var addEvent = function (elem, event, func, prop)
		{
			if (typeof(elem) == 'string')
				var elem = document.getElementById(elem);
			elem.attachEvent('on'+event, func);
		}

	var removeEvent = function (elem, event, func, prop)
		{
			if (typeof(elem) == 'string')
				var elem = document.getElementById(elem);
			elem.detachEvent('on'+event, func);
		}

	var getEvent = function (event)
		{
			var e = window.event;
			e.target = e.srcElement;
			if (e.target.nodeType == 3)
				e.target = e.target.parentNode;
			return e;
		}
}
else if (window.addEventListener) /* Mozilla */
{
	var addEvent = function (elem, event, func, prop)
		{
			if (typeof(elem) == 'string')
				var elem = document.getElementById(elem);
			elem.addEventListener(event, func, prop);
		}

	var removeEvent = function (elem, event, func, prop)
		{
			if (typeof(elem) == 'string')
				var elem = document.getElementById(elem);
			elem.removeEventListener(event, func, prop);
		}

	var getEvent = function (event)
		{
			if (event.target.nodeType == 3)
				event.target = event.target.parentNode;
			return event;
		}
}


/**
 * preventDefault
 * @desc function to suppress default response to an event
 */
function preventDefault(e)
{
	if (e.preventDefault)
	{
		e.preventDefault();
	}
	else
	{
		e.returnValue = false;
	}
	return;
}


addEvent(window, 'load', initMain, true);

/**
 * initMain
 * @desc page initialization
 */
function initMain()
{
	/* event listeners */
	addEvent(document, 'mouseover', createToolTip, true);
	if (document.getElementById('search'))
	{
		addEvent('search', 'focus', setSearch, true);
		addEvent('search', 'blur', setSearch, true);
	}
}


/**
 * objOver
 * @desc adds "over" to the classname property of any object
 */
function objOver(obj) {
	if(obj.className == '')
	{
		obj.className = "over";
	}
	else
	{
		obj.className += " over";
	}
}


/**
 * objOut
 * @desc removes "over" from the classname property of any object
 */
function objOut(obj)
{
	/* Firefox removes leading space, IE Does not. If space is not accounted for classname keeps growing */
	var overStr = (obj.className.indexOf(" over") != -1) ? " over" : "over";
	var downStr = (obj.className.indexOf(" down") != -1) ? " down" : "down";
	var curClasses = obj.className.split(overStr).join("").split(downStr).join("");
	obj.className = null;
	obj.className = curClasses;
}


/**
 * setSearch
 * @desc sets the value of the search text field on blur or focus events
 */
function setSearch(e)
{
	e = getEvent(e);

	if (e.type == 'focus' && e.target.value == 'search')
		e.target.value = '';

	if (e.type == 'blur' && e.target.value == '')
		e.target.value = 'search';

	return;
}


/**
 * Below here are tool tip related functions
 *
 * The tool tips can be placed on any page. Any document element
 * with a 'tooltip' attribute will trigger this function on mouseover
 * and the value of tooltip will be displayed in a popup element.
 *
 * I have defined 2 types of tool tips.
 *
 * Simple tooltips contain a single string value. An example might
 * be a tip for a form field, like 'Please enter phone number with
 * area code'. Simple tool tips may NOT contain HTML markup (It will
 * be html encoded as it is appended as a text node).
 *
 * Complex tooltips contain three pipe separated strings. The
 * first string appears as a title bar. The second string appears
 * as the body of the popup. The third string should be 'true' or
 * 'false' and will determine whether the popup is 'sticky'. If it
 * is sticky (true), a close button will be added to the title bar
 * which must be clicked to close the tool tip. The 'body' portion
 * of a complex tool tip may contain HTML markup.
 */

/**
 * Global scope variables for tool tip functionality
 */
var toolTip = null; /* the tool tip document element object */
var ttDelayTime = 0; /* delay between mouseover and display - 0 for no delay */
var ttDisplayTime = 0; /* total display time - 0 to hide on mouseout */
var ttSticky = false; /* current sticky value */
var closeBtnSrc = 'http://www.ricowi.com/dev/images/icons/close.gif'; /* url for close button image source on sticky tool tips */


/**
 * createToolTip
 * @desc creates a toolTip document element
 */
function createToolTip(e)
{
	e = getEvent(e);
	preventDefault(e);
	if (e.target.getAttribute('tooltip'))
	{
		destroyToolTip();

		toolTip = document.createElement('div');
		toolTip.id = 'toolTip';
		toolTip.className = 'toolTip';
		toolTip.style.position = 'absolute';
		var tip = e.target.getAttribute('tooltip');

		var toolTipTable = document.createElement('table');
		var toolTipTbody = document.createElement('tbody');
		var toolTipTr1 = document.createElement('tr');
		var toolTipTr1Td1 = document.createElement('td');
		toolTipTr1.className = 'toolTipHead';
		var toolTipTr1Td2 = document.createElement('td');
		toolTipTr1Td2.className = 'toolTipClose';
		var toolTipTr2 = document.createElement('tr');
		var toolTipTr2Td = document.createElement('td');
		toolTipTr2Td.className = 'toolTipBody';

		if (tip.indexOf('|') > 0)
		{
			/**
			 * complex tip
			 * parts[0] = Head
			 * parts[1] = Body
			 * parts[2] = boolean sticky
			 */
			var parts = tip.split('|');

			/* head */
			toolTipTr1Td1.appendChild(document.createTextNode(parts[0]));
			toolTipTr1.appendChild(toolTipTr1Td1);

			/* body - use innerHTML to allow html for links in popups */
			toolTipTr2Td.innerHTML = parts[1];

			/* close button for sticky popup */
			if (parts[2] == 'true')
			{
				ttSticky = true;
				var toolTipClose = document.createElement('img');
				toolTipClose.id = 'toolTipClose';
				toolTipClose.src = closeBtnSrc;
				addEvent(toolTipClose, 'click', hideToolTip, true);
				toolTipTr1Td2.appendChild(toolTipClose);
				toolTipTr1.appendChild(toolTipTr1Td2);
				toolTipTr2Td.colspan = 2;
				removeEvent(document, 'mouseout', hideToolTip, true);
			}
			else
			{
				ttSticky = false;
				addEvent(document, 'mouseout', hideToolTip, true);
			}

			toolTipTr2.appendChild(toolTipTr2Td);

			toolTipTbody.appendChild(toolTipTr1);
			toolTipTbody.appendChild(toolTipTr2);
			toolTipTable.appendChild(toolTipTbody);
			toolTip.style.width = '300px';
		}
		else
		{
			/* simple tip */
			toolTipTr2Td.appendChild(document.createTextNode(tip));
			toolTipTr2.appendChild(toolTipTr2Td);
			toolTipTbody.appendChild(toolTipTr2);
			toolTipTable.appendChild(toolTipTbody);
			toolTip.style.width = '300px';
			addEvent(document, 'mouseout', hideToolTip, true);
		}
		toolTip.appendChild(toolTipTable);
		document.body.appendChild(toolTip);

		positionToolTip(e);

		setTimeout('showToolTip()', (ttDelayTime * 1000));
	}
}


/**
 * showToolTip
 * @desc displays the tool tip
 */
function showToolTip()
{
	if (toolTip)
	{
		toolTip.style.visibility = 'visible';
		if (ttDisplayTime > 0)
			setTimeout('destroyToolTip()', (ttDisplayTime * 1000));
	}
}


/**
 * hideToolTip
 * @desc hides the tool tip - can be used to set delay
 */
function hideToolTip(e)
{
	e = getEvent(e);
	destroyToolTip();
	if (ttSticky == false)
		removeEvent(document, 'mouseout', hideToolTip, true);
}


/**
 * destroyToolTip
 * @desc removes the tool tip from the document
 */
function destroyToolTip()
{
	if (toolTip)
		document.body.removeChild(toolTip);
	toolTip = null;
	return;
}


/**
 * positionToolTip
 * @desc sets the position of the tool tip in the browser window
 */
function positionToolTip(e)
{
	/* distance from the edge of viewport to the event */
	var cX = e.clientX;
	var cY = e.clientY;

	/* height and width of the viewport */
	if (typeof(window.innerWidth) != 'undefined')
	{
		var vW = window.innerWidth;
		var vH = window.innerHeight;
	}
	else
	{
		var vW = document.documentElement.offsetWidth;
		var vH = document.documentElement.offsetHeight; /* ie only, in moz this is total document height */
	}

	/* distance the document has been scrolled */
	if (window.pageXOffset)
	{
		/* this should work for moz */
		var sL = window.pageXOffset;
		var sT = window.pageYOffset;
	}
	else
	{
		/* this should work for all */
		var sL = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		var sT = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	}

	/* distance from the edge of document to the event */
	var eX = sL + cX;
	var eY = sT + cY;

	/* document coordinate that is centered in the viewport */
	var vX = Math.ceil(vW * 0.5 + sL);
	var vY = Math.ceil(vH * 0.5 + sT);

	/* set horizontal position of the element */
	if (eX < vX)
	{
		/* place element to the right of the event */
		toolTip.style.left = (eX + 10) + 'px';
		toolTip.style.right = '';
	}
	else
	{
		/* place element to the left of the event */
		toolTip.style.left = '';
		toolTip.style.right = (vW - eX + 10) + 'px';
	}

	/* set vertical position of the element */
	if (eY < vY)
	{
		/* place element below the event */
		toolTip.style.top = (eY + 10) + 'px';
	}
	else
	{
		/* place element above the event */
		toolTip.style.top = (eY - 10 - toolTip.offsetHeight) + 'px';
	}
}

