//var hostRoot  = "http://staging.asascience.com";	// Used by initSNAttribs (for building dynamic hrefs) :: ON STAGING
 var hostRoot  = "http://www.asascience.com";	// Used by initSNAttribs (for building dynamic hrefs)
var navCursor = "default";							// Used by initSNAttribs (for setting sub-nav cursor)
var clsHref   = "dynaref";							// Used by initSNAttribs (classname of hrefs to build)
var clsCursor = "button2";							// Used by initSNAttribs (classname of sub-nav items to set cursor)

function goToLoc(loc)
{
	window.location = hostRoot + "\\" + loc;
}



function initInputs()
{
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "text" && (inputs[i].name == "search"))
		{
			inputs[i].onfocus = function ()
			{
				if (this.value == "Search")
					this.value = "";
			}
			inputs[i].onblur = function ()
			{
				if (this.value == "" && this.name == "search") this.value = "Search";
			}
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", initInputs, false);
else if (window.attachEvent)
	window.attachEvent("onload", initInputs);
	
initNav = function() {
	if (typeof document.body.style.maxHeight == 'undefined') {
		var nav = document.getElementById("main-nav");
		var lis = nav.getElementsByTagName("li");
		for (var i=0; i<lis.length; i++)
		{
			lis[i].onmouseover = function()
			{
				this.className += " hover";
			}
			lis[i].onmouseout = function()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
if (document.all && !window.opera)
	attachEvent("onload", initNav);
	
initHoverIE = function() {
	if (typeof document.body.style.maxHeight == 'undefined') {
		var nav = document.getElementById("promo-box");
		if(nav)
		{
			var lis = nav.getElementsByTagName("div");
			for (var i=0; i<lis.length; i++)
			{
				lis[i].onmouseover = function()
				{
					this.className += " hover";
				}
				lis[i].onmouseout = function()
				{
					this.className = this.className.replace(" hover", "");
				}
			}
		}
	}
}
if (document.all && !window.opera)
	attachEvent("onload", initHoverIE);

function initPage()
{
	var blocks = document.getElementsByTagName("li");
	for (var i=0; i<blocks.length; i++)
	{
		if (blocks[i].className.indexOf("roll") != -1)
		{
			if ( blocks[i].className.indexOf("close") == -1 ) blocks[i].className += " open";
			var links = blocks[i].getElementsByTagName("a");
		
			for (var k=0; k<links.length; k++) {
					
				if (links[k].className == "button") {
						
					links[k].onclick = function()
					{
						if ( this.parentNode.className.indexOf("open") != -1 ) 
						{
							this.parentNode.className = this.parentNode.className.replace("open", "close");
						}
						else if ( this.parentNode.className.indexOf("close") != -1 )
						{
							// [TL4] Added this else if to prevent the classname from extending on every click
							this.parentNode.className = this.parentNode.className.replace("close", "open");
						}
						else
						{
							this.parentNode.className += " open";
						}
						
						return false;
					}
				}
			}
		}
	}
	var blocks2 = document.getElementsByTagName("li");
	for (var i=0; i<blocks2.length; i++)
	{
		if (blocks2[i].className.indexOf("roll2") != -1)
		{
			if ( blocks2[i].className.indexOf("close2") == -1 ) 
			{
				// [TL4] Modified following IF to leave menus open when visiting a page included in that subnav
				if ( (blocks2[i].id == getSubDir(1)) || (blocks2[i].id == getSubDir(2)) || (blocks2[i].id == getSubDir(3)) )
				{
					blocks2[i].className += " open2";
				}
				else
				{
					blocks2[i].className += " close2"; //updated this line from "open2" to "close2" for intial menu state
				}
			}
			
			var links2 = blocks2[i].getElementsByTagName("a");
		
			for (var k=0; k<links2.length; k++) 
			{
					
				if (links2[k].className == "button2")
				{
						
					links2[k].onclick = function()
					{
//						var itis = this.parentNode.className;
//						alert(itis);
						
						if ( this.parentNode.className.indexOf("open2") != -1 ) 
						{
							this.parentNode.className = this.parentNode.className.replace("open2", "close2");
						}
						else if ( this.parentNode.className.indexOf("close2") != -1 )
						{
							// [TL4] Added this else if to prevent the classname from extending on every click
							this.parentNode.className = this.parentNode.className.replace("close2", "open2");
						}
						else
						{
							this.parentNode.className += " open2";
						}			
						
						return false;
					}
				}
			}
		}
	}
}


if (window.addEventListener){
	window.addEventListener("load", initPage, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initPage);
}


// [TL4] Initializes sub-nav attributes to..
//       .. 1) Dynamically build href's within the *.tpl files
//       .. 2) Set the cursor for the subnav dropdowns
//*****************************************************************************
function initSNAttribs()
{
	var ATag = document.getElementsByTagName( "a" );
	var ATagLen = ATag.length;
	
	// Enumerate through all <a> tags in the document
	for ( var i=0; i<ATagLen; i++ )
	{
		// Build hrefs for items within clsHref ONLY
		//   [TLX] changed this to encorporate "item-" class as well
		if ( (ATag[i].className == clsHref) || (ATag[i].className.indexOf("item-") != -1) || (ATag[i].className == "btn-subscribe") || (ATag[i].className == "view") )
		{
			// Overwrite the href with hostRoot + the id of ATag
			ATag[i].href = hostRoot + "/" + ATag[i].id;
		}
		
		// Set the cursor for items within clsCursor ONLY
		if (ATag[i].className == clsCursor)	// not an "else if" in case clsCursor = clsHref
		{
			ATag[i].style.cursor = navCursor;
		}
	}
}


if (window.addEventListener){
	window.addEventListener("load", initSNAttribs, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initSNAttribs);
}

//*****************************************************************************

function detectSafari()
{
	if (navigator.appVersion.indexOf("Safari") != -1)
	{
		if (!window.getComputedStyle) document.body.className += " safari";
			else document.body.className += " safari3";
	}
}
if (window.addEventListener)
{
	window.addEventListener("load", detectSafari, false);
}

//site search functionality
function doSearch()
{
            var thekeyword = document.getElementById("keyword").value;
            //alert(thekeyword);
			//for testing
			//window.location="http://staging.asascience.com/search/index.shtml?keyword="+thekeyword;
            window.location="http://asascience.com/search/index.shtml?keyword="+thekeyword;
}
	
function checkString() //split the string for search words and then iframe to load search results
{
	var thisURL = location.href;
		//alert(thisURL); // this alert doesn't do anything
	var theParts = thisURL.split("?");
	var theKeywordStr = theParts[1].split("=");
	
	//for testing
	//document.getElementById("searchResults").src="http://staging.asascience.com/siteSearch/search.aspx?"+theKeywordStr[1];
	document.getElementById("searchResults").src="http://asascience.com/siteSearch/search.aspx?"+theKeywordStr[1];
}

function clearKeyword()
{
	document.getElementById("keyword").value = "";
}

//end site search functionality



//*****************************************************************************
// [TL4] Edits to allow subnav options to "stick" open
// *Also see edits within initPage()
//
// Edit: Fixed class open/close schema; className progressively lengthens (leaks)
//*****************************************************************************

// Grabs the subdirectory that contains this page
//   level determines how many levels to look back
//   ex. getSubDir(2) on the file "C:\program files\test\mydoc.html" returns "program files"
function getSubDir(level)
{
	var sPath = window.location.pathname;
		sPath = sPath.split("/");
		
	var sSub  = "";
	if ( sPath.length > parseInt(level) )
		sSub  = sPath[sPath.length - (1 + parseInt(level))];
	
	return sSub;
}






