/* Functions to control display and (often delayed) hiding of menu bars. */

var timer = false, timer2 = false; // global vars - keep track of running processes.
var mainMenu;
var LONGDELAY = 2.8, SHORTDELAY = 0.23;

function switch_menu( newMenu )
{
	var core, obj, ctop = 0, liArr, i, hrefText, hrefArr, catArray = [];
	
	// If a switch from one menu to another was planned, kill it now. A delayed switch is planned whenever
	// the user leaves one menu. We then plan on returning to the current menu a few seconds later. But if
	// the user immediately goes to a second one, rather than planning on switching back to the current one,
	// just go to this new one. Another case dealt with by stop_switch() is if the user hovered over one new
	// upper menu-tab and now moved to this one. When he reached the 1st one, a delayed switch was planned to
	// that tab's lower menu. Now that he has moved from it to a new tab, kill the 1st planned switch.
	stop_switch();
	
	// Since it's difficult for setTimeout() below to call a function with a parameter, it calls it without
	// and we set newMenu to mainMenu (see more detailed explanation below).
	if ( ! newMenu )
		newMenu = mainMenu;

	core = document.getElementById( "navbar-core" );
	liArr = core.getElementsByTagName( "li" ); // all the entries in upper navbar
	for ( i=0; i<liArr.length; i++ ) {
		// Do a first pass through upper menu. Purpose is to check if we need to change anything (i.e. if
		// user is hovering over a new menu). While we're at it we concoct the array of values we want.
		hrefText = liArr[i].getElementsByTagName( "a" )[0].href;
		// hrefText = liArr[i].childNodes[1].href;
		hrefArr = hrefText.split( "/" );
		hrefText = hrefArr[3]; // the spot right after e.g. /www.aish.com/ - may be blank for homepage
		if ( hrefText == '' )
			hrefText = "home";
		if ( hrefText == newMenu && ( liArr[i].className == "active" || liArr[i].className == "activered" ) ) {
			// This means the menu tab the user is hovering over is already the active one - do nothing.
			return true;
		}
		// Otherwise, keep concocting our array of menu-links
		catArray[i] = hrefText;
	}
	// If we reached here, we need to change the menus:
	for ( i=0; i<catArray.length; i++ ) {
		if ( catArray[i] == newMenu ) {
			if ( ! liArr[i].className )
				liArr[i].className = "active";
			else
				liArr[i].className = "activered";
				// Note: "activered" is really a stub today (09/14/11) and identical to active in look. I set
				// it here only so that we'll know to change it back to "redtext" when it's no longer active.
			document.getElementById( "navsub-"+catArray[i]).style.display = "block";
		} else {
			// Make all other tabs inactive; hide their lower navbars:
			if ( liArr[i].className == "active" )
				liArr[i].className = ""
			else if ( liArr[i].className == "activered" )
				liArr[i].className = "redtext";
			document.getElementById( "navsub-"+catArray[i]).style.display = "none";
		}
	}
	return true;
}

function delayed_switch_menu( newMenu, shortDur )
	// Calls switch_menu() after a delay. If shortDur is set, wait SHORTDELAY; otherwise LONGDELAY. An
	// assumption of this function is that short delays are employed when we're switching *to* a new lower
	// menu (i.e., when the user is hovering over a new upper menu-tab, in which case we begin a process to
	// switch to the corresponding lower menu), and long delays are employed when a user has left a new menu
	// and now a process is begun to switch back to the current one (i.e., the one corresponding to the
	// actual page the user is on). Bassed on the above assumptions, we set the timed processes to different
	// timer variables so that we'll know which ones to cancel down the road.
{
	var delay;
	
	if ( shortDur )
		delay = SHORTDELAY;
	else
		delay = LONGDELAY;
	// Use setTimeout() to call switch_menu() with a delay. Since setTimeout() has difficulty with passed
	// arguments, have it use a global one instead.
	mainMenu = newMenu;
	if ( delay == SHORTDELAY )
		timer2 = setTimeout( "switch_menu()", delay * 1000 );
	else
		timer = setTimeout( "switch_menu()", delay * 1000 );
	return true;
}

function switch_decide( mainmenu, currmenu )
	// This is called when the user moves his mouse off of an upper-menu tab. 'mainmenu' is the one which is
	// truly active - meaning, we're on a page in its section. 'currmenu' is the one the user just left. The
	// desired behavior depends on a few factors, as explained below.
{
	var core, liArr, i, hrefText, hrefArr;
	
	if ( mainmenu == currmenu ) {
		// If user just left the actual correct menu, nothing to do. If we're already showing us, we're fine.
		// If we're not, we know that as soon as the user entered this menu, we began a short-delay process
		// of returning to us. Here too we need do nothing - allowing that process to continue.
		return true;
	}
	// If we're here, the user is on a new menu. Figure out if it's active already: (I skip comments below as
	// this code is copied from an earlier func).
	core = document.getElementById( "navbar-core" );
	liArr = core.getElementsByTagName( "li" ); // all the entries in upper navbar
	for ( i=0; i<liArr.length; i++ ) {
		hrefText = liArr[i].getElementsByTagName( "a" )[0].href;
		hrefArr = hrefText.split( "/" );
		hrefText = hrefArr[3]; // the spot right after e.g. /www.aish.com/ - may be blank for homepage
		if ( hrefText == '' )
			hrefText = "home";
		if ( liArr[i].className == "active" || liArr[i].className == "activered" ) {
			// We've found the currently active one - see if it's the one we're now on:
			if ( hrefText == currmenu ) {
				// This means the one the user just left is now the active one. (That occurs if he hovered
				// over it long enough to cause the switch.) We must now begin a long-delayed process of
				// switching back to mainmenu.
				return delayed_switch_menu( mainmenu );
			} else {
				// We're not showing the menu the user just left. This occurs if user didn't hover over it
				// long enough to make it change. Now that user has left, we must cancel the short-delay
				// process which was going to make it change. The id of short-delay processes are stored in
				// timer2.
				clearTimeout( timer2 );
			}
			return true;
		}
	}
	// We really shouldn't get here - *some* menu item should have matched.
	return true;
}

function stop_switch()
	// This is called when the user hovers his mouse over the lower menu. All outstanding processes are
	// terminated. If the user has just come from an upper menu-tab different from the current one ('current'
	// meaning the one which actually corresponds to the webpage he's on), 2 possible processes may have been
	// started: (a) If the user hovered over the new upper menu-tab long enough (0.2 sec), the lower menu was
	// switched to the one corresponding to that menu-tab. As soon as he leaves the upper menu-tab, a process
	// is begun to switch back to the current one (afer a few seconds). If, however, the user moves his mouse
	// to the corresponding lower menu-bar, we kill that process ('timer') and keep him on the new menu. (b)
	// If ther user did not hover long enough on the upper menu-tab, the lower menu was not yet switched. We
	// now kill the process to switch it ('timer2') because the user didn't stay long enough to cause it.
{
	if ( timer ) {
		clearTimeout( timer );
		timer = false;
	}
	if ( timer2 ) {
		clearTimeout( timer2 );
		timer2 = false;
	}
	return true;
}

