/**************************************
* Module backend_nav.js
* Purpose: Scripts for backend navigation
* Revised Sept 27, 2006 by James
* Works with the new DB-driven menu system
***************************************/

// Timer so that the menu items will stay hilighted for a short delay
var navTimer;
var delay = 300;

//Vars to store the currently selected item(s)
var topSel = null;
var botSel = null;
var botGroup = null;

//addLoadEvent(jbFindSelectedMenu);

/**************************************
 * Function addLoadEvent
 * Used from simon.incutio.com
 * Purpose: Add a window load event handler
 **************************************/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) { oldonload(); }
			func();
		}
	}
}

function jbFindSelectedMenu() {
	// Find the selected items by their parents
	t = document.getElementById('jbNavTopParent');
	if(t && t.childNodes) {
		e = t.childNodes;
		for(i = 0; i < e.length; i++) {
			if(e[i].className == 'jbNavTopSel') { topSel = e[i]; }
		}
	}

	b = document.getElementById('jbNavBottomParent');
	if(b && b.childNodes) {
		e = b.childNodes;
		for(i = 0; i < e.length; i++) {
			if(e[i].className == 'jbNavBottomGroup') { botGroup = e[i]; }
		}
	}

	if(botGroup && botGroup.childNodes) {
		if(topSel) {
			parentCenter = topSel.offsetLeft + (topSel.offsetWidth / 2);
			leftEdge = parentCenter - (botGroup.offsetWidth / 2);
			rightEdge = parentCenter + (botGroup.offsetWidth / 2);
			if(leftEdge < 20) {
				botGroup.style.float = "left";
				botGroup.style.left = "20px";
			} else if (rightEdge + 20 > window.innerWidth) {
				botGroup.style.float = "right";
				botGroup.style.right = "20px";
			} else {
				botGroup.style.float = "left";
				botGroup.style.left = Math.round(leftEdge) + "px";
			}
		}
		e = botGroup.childNodes;
		for(i = 0; i < e.length; i++) {
			if(e[i].className == 'jbNavBottomSel') { botSel = e[i]; }
		}
	}
	//alert('Top:' + topSel + '; Bot:' + botSel + '; Group:' + botGroup + ';');
}

/**************************************
* Function mout()
* Purpose: When the user moves the cursor off of a menu item, make it wait a
*          bit before clearing it so we can move between items that aren't
*          directly adjacent to each other
***************************************/
function mout() {
	navTimer = window.setTimeout('mout_clear()', delay);
}

/**************************************
* Function mout_clear()
* Purpose: Clean up nav menu on mouse out
***************************************/
function mout_clear() {
	var e = document.getElementById('jbNavTopParent').childNodes;
	for(i = 0; i < e.length; i++) {
		if(e[i].id && e[i].id.substr(0, 8) == 'jbNavTop') { e[i].className = 'jbNavTopNorm'; }
	}
	if(topSel != null) { topSel.className = 'jbNavTopSel'; }

	e = document.getElementById('jbNavBottomParent').childNodes;
	for(i = 0; i < e.length; i++) {
		//alert(e[i].id);
		if(e[i].className) { e[i].className = 'jbNavBottomGroupHidden'; }
	}

	if(botGroup != null) {
		botGroup.className = 'jbNavBottomGroup';
		e = botGroup.childNodes;
		for(i = 0; i < e.length; i++) {
			if(e[i].className && e[i].className.substring(0, 11) == 'jbNavBottom') { e[i].className = 'jbNavBottomNorm'; }
		}
		if(botSel != null) { botSel.className = 'jbNavBottomSel'; }
	}
}

/**************************************
* Function mover(String e)
* Purpose: Style nav menu on mouse over
***************************************/
function mover(topId, botId) {
	// Get rid of the clear timer because we're still over a nav element
	window.clearTimeout(navTimer);

	// Set all top-level elements to normal style
	e = document.getElementById('jbNavTopParent').childNodes;
	for(i = 0; i < e.length; i++) {
		if(e[i].id && e[i].id.substr(0, 8) == 'jbNavTop') { e[i].className = 'jbNavTopNorm'; }
	}
	// Set the hilighted top element to selected style
	//top = document.getElementById('top' + topId);
	//if(top != null) { top.className = 'jbNavTopSel'; }
	topS = document.getElementById('jbNavTop' + topId);
	if(topS) { topS.className = 'jbNavTopSel'; }

	// Set all bottom-level groups to invisible
	e = document.getElementById('jbNavBottomParent').childNodes;
	for(i = 0; i < e.length; i++) {
		//alert(e[i].className);
		if(e[i].className && e[i].className.substring(0, 16) == 'jbNavBottomGroup') { e[i].className = 'jbNavBottomGroupHidden'; }
	}
	// Set the hilighted bottom group to visible
	grp = document.getElementById('grp' + topId);
	if(grp != null) {
		grp.className = 'jbNavBottomGroup';
		// Move the group underneath the parent element
		if(topS) {
			parentCenter = topS.offsetLeft + (topS.offsetWidth / 2);
			leftEdge = parentCenter - (grp.offsetWidth / 2);
			rightEdge = parentCenter + (grp.offsetWidth / 2);
			if(leftEdge < 20) {
				grp.style.float = "left";
				grp.style.left = "20px";
			} else if (rightEdge + 20 > window.innerWidth) {
				grp.style.float = "right";
				grp.style.right = "20px";
			} else {
				grp.style.float = "left";
				grp.style.left = Math.round(leftEdge) + "px";
			}
		}

		// Set all bottom-level elements to normal style
		e = grp.childNodes;
		for(i = 0; i < e.length; i++) {
			if(e[i].className && e[i].className.substring(0, 11) == 'jbNavBottom') { e[i].className = 'jbNavBottomNorm'; }
		}
		// If a bottom element has been hilighted, set it to selected style
		if(botId != null) { document.getElementById(botId).className = 'jbNavBottomSel'; }
	}
}

