var activeNav;

function prep()
{
	var NFL_INDEX = 3;
	var NASCAR_INDEX = 2;
	
	var primaryNavs = getPrimaryNavs();
	
	for (var i = 0; i < primaryNavs.length; i++)
	{
		var secondaryNavs = primaryNavs[i].getElementsByTagName("li");
		
		if (primaryNavs[i].parentNode.parentNode.id == "menu")
		{
			primaryNavs[i].getElementsByTagName("a")[0].onfocus = primaryNavFocus;
		}
		
		if (secondaryNavs.length > 0)
		{
			secondaryNavs = primaryNavs[i].getElementsByTagName("a");
			
			for (var j = 1; j < secondaryNavs.length; j++)
			{
				secondaryNavs[j].onfocus = secondaryNavFocus;
				secondaryNavs[j].onblur = secondaryNavBlur;
			}
		}
	}
	
	document.getElementById("menu").getElementsByTagName("input")[0].onfocus = function(){if (activeNav) activeNav.className = activeNav.className.replace(/active/g, "");};
	
	document.body.onmousedown = secondaryNavBlur;
	
	// make the search box hide/show the default text on focus/blur
	
	var defaultSearchText = document.getElementById("header").getElementsByTagName("form")[0].getElementsByTagName("input")[0].value;
	
	document.getElementById("header").getElementsByTagName("form")[0].getElementsByTagName("input")[0].onfocus = function(){this.value = (this.value == defaultSearchText ? "" : this.value);hideMenu()};
	
	document.getElementById("header").getElementsByTagName("form")[0].getElementsByTagName("input")[0].onblur = function(){this.value = (this.value.length > 0 ? this.value : defaultSearchText);};
	
	// construct the sidebar and breadcrumb, but only if not on the homepage
	// if NFL or NASCAR, use AJAX to retrieve a custom sidebar
	
	if (document.getElementById("active") == primaryNavs[4])
	{
		if (primaryNavs[4].getElementsByTagName("li")[NFL_INDEX].className.indexOf("active") >= 0)
		{
			constructFootball101SidebarAndBreadcrumb();
		}
		else if (primaryNavs[4].getElementsByTagName("li")[NASCAR_INDEX].className.indexOf("active") >= 0)
		{
			constructNascarSidebarAndBreadcrumb();
		}
		else if (document.getElementById("sidebar"))
		{
			constructSidebarAndBreadcrumb();
		}
	}
	else if (document.getElementById("active") && document.getElementById("active") != primaryNavs[0])
	{
		constructSidebarAndBreadcrumb();
	}
	
	if (window.replaceFlash)
	{
		replaceFlash(document.getElementById("flash"), "/en_US/consumer/media/flash/playerProductInstall.swf");
		
		replaceFlash(document.getElementById("ask-doctor"), "/en_US/consumer/media/flash/playerProductInstall.swf");
	}
	
	/* throws an error in IE, so function was removed
	document.getElementById("bbbLink").oncontextmenu = function()
	{
		alert("Use without permission is prohibited. The BBB Accreditation seal is a trademark of the Council of Better Business Bureaus, Inc.");
		
		return false;
	}
	*/
	
	if (window.moreOnLoad)
	{
		moreOnLoad();
	}
}

function getPrimaryNavs()
{
	var lis = document.getElementById("menu").getElementsByTagName("li");
	var primaryNavs = new Array();
	
	primaryNavs[primaryNavs.length] = lis[0];
	
	for (var i = 1; i < lis.length; i++)
	{
		if (lis[i].getElementsByTagName("li").length > 0)
		{
			primaryNavs[primaryNavs.length] = lis[i];
		}
	}
	
	return primaryNavs;
}

function constructFootball101SidebarAndBreadcrumb()
{
	// only done within Football 101, so the NFL index page doesn't load ajax.js
	if (window.loadTextDoc)
	{
		loadTextDoc("/en_US/consumer/contests-and-fun/NFL/football-101/sidebar.jsp", loadFootball101SidebarAndBreadcrumb);
	}
	else if (document.getElementById("sidebar"))
	{
		constructSidebarAndBreadcrumb();
	}
}

function constructNascarSidebarAndBreadcrumb()
{
	if (window.loadTextDoc)
	{
		loadTextDoc("/en_US/consumer/contests-and-fun/NASCAR/sidebar.jsp", loadCustomSidebarAndBreadcrumb);
	}
	else if (document.getElementById("sidebar"))
	{
		constructSidebarAndBreadcrumb();
	}
}

function loadCustomSidebarAndBreadcrumb(html)
{
	loadCustomSidebar(html);
	
	var home = document.getElementById("menu").getElementsByTagName("li")[0];
	var breadcrumb = document.getElementById("main").getElementsByTagName("div")[0];
	var lis = document.getElementById("sidebar").getElementsByTagName("li");
		
	for (var i = 0; i < lis.length; i++)
	{
	    if (lis[i].className.indexOf("active") >= 0)
		{
			breadcrumb.innerHTML = home.innerHTML + " <span>" + document.getElementById("active").getElementsByTagName("span")[0].innerHTML + "<\/span> " + " <span>" + trim(lis[0].innerHTML) + "<\/span> " + lis[i].firstChild.innerHTML;
		}
	}
}

function loadFootball101SidebarAndBreadcrumb(html)
{
	loadCustomSidebar(html);
	
	var home = document.getElementById("menu").getElementsByTagName("li")[0];
	var breadcrumb = document.getElementById("main").getElementsByTagName("div")[0];
	var lis = document.getElementById("sidebar").getElementsByTagName("li");
		
	for (var i = 0; i < lis.length; i++)
	{
	    if (lis[i].className.indexOf("active") >= 0)
		{
			breadcrumb.innerHTML = home.innerHTML + " <span>" + document.getElementById("active").getElementsByTagName("span")[0].innerHTML + "<\/span> " + " <span>" + trim(lis[0].innerHTML) + "<\/span> ";
			if (i > 1)
			{
			    breadcrumb.innerHTML = breadcrumb.innerHTML + " <span>" + trim(lis[1].innerHTML) + "<\/span> ";
			}
			breadcrumb.innerHTML = breadcrumb.innerHTML + lis[i].firstChild.innerHTML;
		}
	}
}

function loadCustomSidebar(html)
{
	document.getElementById("sidebar").innerHTML = html + document.getElementById("sidebar").innerHTML;
	
	var lis = document.getElementById("sidebar").getElementsByTagName("li");
	
	lis[1].className += " active";
	
	for (var i = 2; i < lis.length; i++)
	{
		if (document.location.href.indexOf(lis[i].getElementsByTagName("a")[0].href) == 0)
		{
			lis[i].className += " active";
			
			lis[1].className = lis[1].className.replace(/active/g, "");
			
			break;
		}
	}
}

function constructSidebarAndBreadcrumb()
{
	// copy the current section's hierarchy into the sidebar
	
	document.getElementById("sidebar").innerHTML = document.getElementById("active").innerHTML + document.getElementById("sidebar").innerHTML;
	
	// since the first <a> is not in the same <ul> as the others, put it there by cloning the first <li> node
	
	var temp = document.getElementById("sidebar").getElementsByTagName("li")[0];
	var clone = temp.cloneNode(temp);
	
	temp.className = temp.className.replace(/first-child/g, ""); // remove first-child class from what will become the second child (might be "active" also)
	
	clone.className += " active"; // set the new first-child to active initially
	
	temp.parentNode.insertBefore(clone, temp);
	
	// now get rid of the original first link, which is the primary page
	// first, copy the href and text over to the new node, which is in the <ul> with the others
	
	var sidebarAnchors = document.getElementById("sidebar").getElementsByTagName("a");
	
	sidebarAnchors[1].href = sidebarAnchors[0].href;
	
	sidebarAnchors[1].innerHTML = sidebarAnchors[0].innerHTML;
	
	// now cycle through the other <li>s to see if any of them were class="active"; if so, remove the class from the newly-created first-child node
	
	var lis = document.getElementById("sidebar").getElementsByTagName("li");
	var breadcrumb = document.getElementById("main").getElementsByTagName("div")[0];
	var home = document.getElementById("menu").getElementsByTagName("li")[0];
	
	for (var i = 1; i < lis.length; i++)
	{
		if (lis[i].className.indexOf("active") >= 0)
		{
			lis[0].className = lis[0].className.replace(/active/g, "");
			
			breadcrumb.innerHTML = home.innerHTML + " <span>" + trim(lis[0].innerHTML) + "<\/span> " + lis[i].firstChild.innerHTML;
			
			break;
		}
	}
	
	// if none of the <li>s were class="active", that means the primary is active
	
	if (document.getElementById("main").getElementsByTagName("div")[0].innerHTML.length == 0)
	{
		breadcrumb.innerHTML = home.innerHTML + " " + lis[0].firstChild.innerHTML;
	}
	
	// lastly, remove the orphaned <span>
	
	temp = document.getElementById("sidebar").getElementsByTagName("span")[0];
	
	temp.parentNode.removeChild(temp);
}

function primaryNavFocus()
{
	if (activeNav)
	{
		hideMenu();
	}
	
	this.parentNode.parentNode.className += (this.parentNode.parentNode.className.indexOf("active") >= 0 ? "" : " active");
	
	activeNav = this.parentNode.parentNode;
}

function secondaryNavFocus()
{
	var li = this.parentNode.parentNode.parentNode;
	
	if (!activeNav)
	{
		activeNav = li;
	}
	
	if (activeNav != li)
	{
		hideMenu();
	}
	
	li.className += (li.className.indexOf("active") >= 0 ? "" : " active");
	
	activeNav = li;
}

function secondaryNavBlur(e)
{
	if (!e)
	{
		e = window.event;
	}
	
	if (e.button)
	{
		if (activeNav)
		{
			hideMenu();
		}
		
		return;
	}
	
	var li = this.parentNode.parentNode.parentNode;
	
	if (!activeNav)
	{
		activeNav = li;
	}
	
	if (activeNav != li)
	{
		hideMenu();
	}
}

function hideMenu()
{
	activeNav.className = activeNav.className.replace(/active/g, "");
}
			
			// Removes leading whitespaces
			function LTrim( value )
			{
				var re = /\s*((\S+\s*)*)/;
				
				return value.replace(re, "$1");
			}
			
			// Removes ending whitespaces
			function RTrim( value )
			{
				var re = /((\s*\S+)*)\s*/;
				
				return value.replace(re, "$1");
			}
			
			// Removes leading and ending whitespaces
			function trim( value )
			{
				return LTrim(RTrim(value));
			}
