/* 
* date: 2002-01-23 
* info: http://inspire.server101.com/js/xc/ 
* 9Feb06  Barry Jacobson changed so that only one element is expanded at a time.  Also renamed variables and added comments for better readablility
*/ 


// set id for overall menu to be controled to 'x'
// 'xc' is unique name used for current menu
// 'leftLinkOpen' is id of menu item to initialize to open
//  each menu item to be expanded must have unique id
//  add text for hover tip to each 

var xcNodeA = [];  // nodes for showing and hiding elements
var xcULElemA = []; // elements for current menu items

function xcSet(menuId, menuClassName) {  // initialize xcNodeA and xcULElemA.  Optional arguments indicate to open menu item
//  Inputs: menuId = id used for entire menu being controled
//      menuClassName = class name assigned to all controled elements within menu
	if ((document.getElementById) && (document.createElement)) {    
		var ulElemA = document.getElementById(menuId).getElementsByTagName('ul');  // create array of all 'ul' elements for id == menuId
		var ulId, liNode, aClassName, closeFlag, i, j, showNode, hideNode; 
		var k = 0;
		for (i = 0; i < ulElemA.length; i++) { 
			if (ulId = ulElemA[i].getAttribute('id')) { 
				showNode = xcCtrl(ulId, menuClassName, 'x', '+', 'Show', 'expand menu'); 
				hideNode = xcCtrl(ulId, menuClassName, 'c', '-', 'Hide', 'collapse menu');  // ulElemA[i].getAttribute('title')+
				liNode = ulElemA[i].parentNode; 
				if (aClassName = !liNode.className) { // if node has a class name determine if it is in the optional arguments
					j = 2; 
					while ((closeFlag = !(ulId == arguments[j])) && (j++ < arguments.length)); 
				} 
				if (closeFlag) {  // if not in optional arguments then close item
					liNode.insertBefore(showNode, liNode.firstChild);   // display +
					ulElemA[i].style.display = 'none';  // hide submenu
				}
				else { // open item				
					liNode.insertBefore(hideNode, liNode.firstChild);  // display -
					ulElemA[i].style.display = 'block';   // show submenu
				}
				liNode.className = menuClassName; 
				xcULElemA[k] = ulElemA[i];  // store current element in global element array
				k++;
			} 
		} 
	}
	var liElemA = document.getElementById(menuId).getElementsByTagName('li');
	for (i = 0; i < liElemA.length; i++) { 
		if (liElemA[i].id == "Bullet") {
			var a = document.createElement('a'); 
			liElemA[i].className = menuClassName;
			a.setAttribute('href', liElemA[i].firstChild.href); 
			var img = document.createElement('img'); 
			img.setAttribute('src','images/Bullet.gif');
			img.setAttribute('border','0');
			a.appendChild(img); 
			var d = document.createElement('div'); 
			d.className = menuClassName + 'x';
			d.appendChild(a); 
			liElemA[i].insertBefore(d, liElemA[i].firstChild);  // display -
		}
	}
} 

function xcShow(aUlID) { // when user clicks on +, open submenu of clicked item and close all others
	var ulID;
	for (i=0;i<xcULElemA.length;i++) {
	    ulID = xcULElemA[i].getAttribute('id');
		if (ulID == aUlID) {
			xcXC(aUlID, 'block'); 
//			xcNodeA[aUlID+'c'].firstChild.focus(); 
		}
		else {
			xcXC(ulID, 'none'); 
		}
	}
} 

function xcHide(aUlID) { // when user clicks on -, close submenu
	xcXC(aUlID, 'none'); 
}

function xcXC(aUlID, newDisplay) { // toggle current ID between open and closed, if already not in specified display state.
	var ulElem = document.getElementById(aUlID); 
	if (ulElem.style.display != newDisplay) {
		if (newDisplay == 'block') {
			ulElem.parentNode.replaceChild(xcNodeA[aUlID+'c'], xcNodeA[aUlID+'x']);
			ulElem.style.display = 'block';
		}
		else {
			ulElem.parentNode.replaceChild(xcNodeA[aUlID+'x'], xcNodeA[aUlID+'c']);
			ulElem.style.display = 'none';
		}
	}
}

function xcCtrl(aUlID, aClassName, openClose, nodeText, showHide, hoverTip) {  // create open or close node
// inputs:	aUlID = current ID for menu item
//			aClassName = class name to use as first part of node class name
//			openClose = 'x' for open or 'c' for close
//			nodeText = '+' or '-'
//			showHide = 'Show' or 'Hide'
//			hoverTip = text displayed when user hovers over menu item
	var a = document.createElement('a'); 
	a.setAttribute('href', 'javascript:xc'+showHide+'(\''+aUlID+'\');'); 
	a.setAttribute('title', hoverTip); 
	var img = document.createElement('img'); 
	if (showHide == 'Show') {
		img.setAttribute('src','images/MenuPlus.gif');
	}
	else {
		img.setAttribute('src','images/MenuMinus.gif');	
	}
	img.setAttribute('border','0');
	a.appendChild(img); 
//	var t = document.createTextNode(nodeText);
//	a.appendChild(t); 
	
	var d = document.createElement('div'); 
	d.className = aClassName + openClose; 
	d.appendChild(a); 
	
	return xcNodeA[aUlID+openClose] = d; 
} 
