var links = [];
var timeout;

function initPage()
{
	// Template code from PSD2HTML for the nav ------------------
	var nav = document.getElementById('nav');
	var count;
	if (nav){
		var navEl = nav.getElementsByTagName('li');
		for (var i=0; i<navEl.length; i++) {
			if (navEl[i].parentNode == nav) links.push(navEl[i]);
		};
		for (var i = 0; i < links.length; i++) {
			links[i].onmouseover = function()
			{
				// Reset nav before highlighting.
				if(timeout)
				{
					clearTimeout(timeout);
					for (var j = 0; j < links.length; j++)
					{
						links[j].className = links[j].className.replace('noactive','');
						links[j].className = links[j].className.replace('active','');
					}
				}

				for (var j = 0; j < links.length; j++) links[j].className += ' noactive';
				this.className = this.className.replace('noactive','');
				this.className += ' active';
			}
			
			// We need the event target for the mouseOut so use some custom code.
			addEvent( links[i], 'mouseout', getMoutFor(links[i]), false );
		}
	}	
}

function resetNav(target)
{
	target.className = target.className.replace('active','');
	for (var j = 0; j < links.length; j++)
	{
		links[j].className = links[j].className.replace('noactive','');
		if(links[j].className.indexOf('current') != -1)
			links[j].className += ' active';
	} 
	
}

if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initPage);

// Callback Functions ------------------------------
function mouseOutHandler(e, targetElement)
{
	var el = window.event ? targetElement : e ? e.currentTarget : null;
	if(!el) return;
	timeout = setTimeout(function() {resetNav(el)}, 700);
}

// Functions for getting the proper event target in IE
function getMoutFor(node) {
	return function(e) { mouseOutHandler(e, node); };
}

function addEvent( el, eType, fn, useCapture )
{
	if (el.addEventListener)
	{
		el.addEventListener(eType, fn, useCapture);
		return true;
	}
	else if (el.attachEvent)
	{
		return el.attachEvent('on' + eType, fn);
	}
	else
	{
		el['on' + eType] = fn;
	}
}
