function onLoad(page)
{
}

function activateMenus()
{
	// initialize dropdown menus
	var ul = $('link_ul');
	var nodes = ul.childNodes;
	for(var n = 0; n < nodes.length; n++)
		if(nodes[n].nodeName.toLowerCase() == 'li')
			new Menu(nodes[n]);
}

function $(id)
{
	return document.getElementById(id);
}

function swapImg(oldImg)
{
	var swap = true;
	oldImg.onmouseout = function() { swap = false; };
	
	var newImg = document.createElement('img');
	newImg.onmouseout = function() { newImg.parentNode.replaceChild(oldImg, newImg); };
	newImg.onload     = function() { if(swap) oldImg.parentNode.replaceChild(newImg, oldImg); };
	newImg.src = oldImg.src.replace(/_off/i, '_over');
}

function showEmail(email)
{
	$('contactEmail').innerHTML = email;
	var department = $('department_sel'), business = $('business_sel'), home_auto = $('home_auto_sel'), life_health = $('life_health_sel');
	business.style.display = home_auto.style.display = life_health.style.display = 'none';
	if(check(business))
		department.selectedIndex = 1;
	else if(check(home_auto))
		department.selectedIndex = 2;
	else if(check(life_health))
		department.selectedIndex = 3;
	
	function check(sel)
	{
		for(var n = 0; n < sel.options.length; n++)
		{
			if(sel.options[n].value == email)
			{
				sel.selectedIndex = n;
				sel.style.display = '';
				return true;
			}
		}
		return false;
	}
}

function autoSizeLayout(elements, width, height)
{
	var pageWidth  = width  || (document.documentElement ? document.documentElement.clientWidth  : document.body.clientWidth);
	var pageHeight = height || (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
	for(var n = 0; n < elements.length; n++)
	{
		var element = (typeof(elements[n]) == 'string') ? document.getElementById(elements[n]) : elements[n];
		if(element)
		{
			var style = element.currentStyle || getComputedStyle(element, null) || element.style;
			var left, right, top, bottom;
			if(style.width == 'auto')
			{
				if(style.left && style.right && (left = style.left.match(/([\d]*)(px|%)/i)) && (right = style.right.match(/([\d]*)(px|%)/i)))
				{
					left  = (left[2].toLowerCase()  == 'px') ? left[1]  : Math.floor(pageWidth * left[1]  / 100);
					right = (right[2].toLowerCase() == 'px') ? right[1] : Math.floor(pageWidth * right[1] / 100);
					element.style.width = (pageWidth - left - right)+'px';
				}
				else alert('autoSizeLayout: didn\'t match style for '+elements[n]+' : '+style.left+' '+style.right);
			}
			if(style.height == 'auto')
			{
				if(style.top && style.bottom && (top = style.top.match(/([\d]*)(px|%)/i)) && (bottom = style.bottom.match(/([\d]*)(px|%)/i)))
				{
					if(pageHeight)
					{
						top    = (top[2].toLowerCase()    == 'px') ? top[1]    : Math.floor(pageHeight * top[1]     / 100);
						bottom = (bottom[2].toLowerCase() == 'px') ? bottom[1] : Math.floor(pageHeight * bottom[1]  / 100);
						element.style.height = (pageHeight - top - bottom)+'px';
					}
				}
				else alert('autoSizeLayout: didn\'t match style for '+elements[n]+' : '+style.top+' '+style.bottom);
			}
		}
		else alert('failed to find element: '+elements[n]);
	}
	window.onresize = function()
	{
		autoSizeLayout(elements, width, height)
	};
}

// MouseOver Fading Object
Menu.current = null; // current menu that is displayed
Menu.canFade = (navigator.userAgent.match(/Firefox/i) != null); // whether or not the browser can handle fades
function Menu(listElement) 
{
	if(listElement.getElementsByTagName('li').length == 0)
		return;

	var ref = this;

	// public functions
	this.invokeFadeOut = invokeFadeOut;

	// private vars
	var mouseOverObj	= listElement; // document.getElementById(mouseOverElementId);
	if(mouseOverObj != null)
	{
		mouseOverObj.onmouseover = beginMouseOver;
		mouseOverObj.onmouseout  = beginMouseOut;
	}
	else onError('Element not found for id: '+mouseOverElementId, 'Menu()', 'init.js');
	
	var fadingObj		= mouseOverObj.getElementsByTagName("ul")[0]; // first and only child
	var fadeLength		= 150; // time it takes to completely fade, *2 when hidding Menu.current
	var fadeInDelay   = 100; // wait time before beginning to appear
	var fadeOutDelay  = 200; // wait time before beginning to fade
	var fadeInterval  = 10;  // smoothness, smaller is smoother
	
	var delayInTimer	= null;
	var delayOutTimer	= null;
	var fadeInInterval	= null;
	var fadeOutInterval	= null;
	var opacityCur		= 0;
	var opacityMin    = 0;
	var opacityMax    = 99; // The 99 is there, because ff flickers from 99% opacity to 100.
	var fadeChange = opacityMax / (fadeLength / fadeInterval);
	
	// private functions
	function clearTimers()
	{
		if(delayInTimer != null)
		{
			window.clearTimeout(delayInTimer);
			delayInTimer = null;
		}
		if(delayOutTimer != null)
		{
			window.clearTimeout(delayOutTimer);
			delayOutTimer = null;
		}
		if(fadeInInterval != null)
		{
			window.clearInterval(fadeInInterval);
			fadeInInterval = null;
		}
		if(fadeOutInterval != null)
		{
			window.clearInterval(fadeOutInterval);
			fadeOutInterval = null;
		}
	}
	
	function beginMouseOver()
	{
		if(delayOutTimer != null)
		{
			window.clearTimeout(delayOutTimer);    
			delayOutTimer = null;
		}

		if(Menu.current != ref)
		{
			if(Menu.current != null)
				Menu.current.invokeFadeOut();
			if(delayInTimer == null)
			{
				if(Menu.canFade)
					delayInTimer = window.setTimeout(startFadeIn, fadeInDelay);
				else delayInTimer = window.setTimeout(showIn, fadeInDelay);
			}
		}
		else if(Menu.canFade && opacityCur < opacityMax)
		{
			if(fadeOutInterval != null)
			{
				window.clearInterval(fadeOutInterval);    
				fadeOutInterval = null;
			}
			delayInTimer = window.setTimeout(startFadeIn, fadeInDelay);
		}
		
		function showIn() // non-fade
		{
			clearTimers();
			Menu.current = ref;
			fadingObj.style.display = 'block';
		}
		
		function startFadeIn()
		{
			clearTimers();

			Menu.current = ref;
			setOpacity(opacityCur);
			fadingObj.style.display = 'block';
			fadeInInterval = window.setInterval(runFadeIn, fadeInterval);
			
			function runFadeIn() 
			{
				opacityCur += fadeChange;
				setOpacity(opacityCur);
				if(opacityCur >= opacityMax)
					window.clearInterval(fadeInInterval);
			}
		}
	}
	
	function beginMouseOut()
	{
		window.clearTimeout(delayInTimer); delayInTimer = null;
		if (delayOutTimer == null) 
		{
			if(Menu.canFade)
				delayOutTimer = window.setTimeout(startFadeOut, fadeOutDelay);
			else delayOutTimer = window.setTimeout(showOut, fadeOutDelay);
		}
		
		function showOut() // non-fade
		{
			clearTimers();
			fadingObj.style.display = 'none';
			Menu.current = null;
		}
		
		function startFadeOut()
		{
			clearTimers();

			fadeOutInterval = window.setInterval(runFadeOut, fadeInterval);
			function runFadeOut() 
			{
				opacityCur -= fadeChange;
				setOpacity(opacityCur);
				if (opacityCur <= opacityMin) 
				{
					fadingObj.style.display = 'none';
					Menu.current = null;
					window.clearInterval(fadeOutInterval);
				}
			}
		}
	}
	
	function invokeFadeOut()
	{
		clearTimers();
		
		if(Menu.canFade)
		{
			setOpacity(opacityCur=75);
			fadeOutInterval = window.setInterval(runFadeOut, fadeInterval);
		}
		else // non-fade
		{
			fadingObj.style.display = 'none'; 
		}
		
		function runFadeOut() 
		{
			opacityCur -= (fadeChange * 2);
			setOpacity(opacityCur);
			if (opacityCur <= opacityMin) 
			{
				fadingObj.style.display = 'none'; 
				window.clearInterval(fadeOutInterval);
			} 
		}
	}

	function setOpacity(opacityCur)
	{
		if (opacityCur > opacityMax)
			opacityCur=opacityMax;
		fadingObj.style['opacity']      = opacityCur / 100;
		fadingObj.style['-moz-opacity'] = opacityCur / 100;
		fadingObj.style['filter']       = 'alpha(opacity='+opacityCur+')';
	}
}
