/***** EW Global Variables *****/
var isopera = typeof window.opera != 'undefined';
var isie = typeof document.all != 'undefined'
   	&& !isopera && navigator.vendor != 'KDE';
var issafari = navigator.vendor == 'Apple Computer, Inc.';

/***** EW Global Functions *****/

/* Center the sub nav items */
tii_callFunctionOnElementLoad('subnav', function(){
 var menuDiv = document.getElementById('subnav');
 menuDiv.setAttribute('width', parseInt(menuDiv.offsetWidth)+'px');
 menuDiv.style.marginLeft = 'auto';
 menuDiv.style.marginRight = 'auto';
 });


/* Start the setToutHovers function on window load */
tii_callFunctionOnWindowLoad (function ()
{
	setToutHovers ();
});

/* Sets the mouseover background color change for the touts that need it 
	<a name="thovref"></a> needs to be added as a child of the div that contains the touts */
function setToutHovers ()
{
	var toutRefs = document.getElementsByName ('thovref');
	var toutRefsLength = toutRefs.length;
	for (var i = 0; i < toutRefsLength; i++)
	{
		var divRef = toutRefs [i].parentNode;
		if (divRef != null && divRef.tagName != null && divRef.tagName.toUpperCase () == 'DIV')
		{
			attachToutListeners (divRef, 'div');
			attachToutListeners (divRef, 'ul');			
			attachToutListeners (divRef, 'li');
		}
	}
}

/* Attaches the mouseover and mouseout event listeners to the tout */
var attachTL_eventSource;

function attachToutListeners (divRef, tagName)
{
	var touts = divRef.getElementsByTagName (tagName);
	var toutsLength = touts.length;
	for (var j = 0; j < toutsLength; j++)
	{
		var closeTime;
		var tout = touts [j];
		var changeBackground = 
			function (event)
			{
				var eventSource = getTitledSource (event, 'touthover');
				if (!eventSource)
				{
					return false;
				}
				if (attachTL_eventSource == eventSource)
				{
					clearTimeout (closeTime);
				}
				toutHover (eventSource, true);
			};
		var restoreBackground = 
			function (event)

			{
				var eventSource = getTitledSource (event, 'touthover');
				attachTL_eventSource = eventSource;
				closeTime = setTimeout (
					function ()
					{
						toutHover (eventSource, false);
					}, 1);
			};
		
		if (tout.className.indexOf ('touthover') > -1)
		{
			tii_addEventHandler (tout, 'mouseover', changeBackground, false);
			tii_addEventHandler (tout, 'mouseout', restoreBackground, false);
			tii_addEventHandler (tout, 'focus', changeBackground, false);
			tii_addEventHandler (tout, 'blur', restoreBackground, false);
		}
	}
}

function getTitledSource (event, title)
{
	var eventSource = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
	while (eventSource.className.indexOf (title) < 0)
	{
		eventSource = eventSource.parentNode;
	}

	return eventSource;
}

/* Tout Rollover Color */
function toutHover (obj, highlighted)
{ 
	if (highlighted)
	{
		var objClass = obj.className;
		obj.className = objClass + (objClass == '' ? '' : ' ') + 'changetout';
		obj.style.backgroundColor = '#336699';
	}
	else
	{
		obj.className = obj.className.replace(/changetout/gi, '');
		obj.style.backgroundColor = '';
	}
}


