/* ----------------------------------------------------------------------------

	name:
	menu.js
	
	author:
	Simon Bruce
	
	Copyright:
	2002, autom-8 pty ltd
	
	Description:
	DOM compliant code (with an IE events twist) for creating opening and
	closing UL lists (collapseList) and opening and closing of a "Switch 
	Item" and an item to hide (initializeSwitchBox).
		
	All these functions are designed to degrade gracefully in browsers
	not supported dom functionality.
		
	Works in:
	Tested in Mozilla 1.1 and up to 1.2
	Internet Explorer for Windows 5.5 and up.
	Opera 7 beta 1
	
---------------------------------------------------------------------------- */

	
/* ----------------------------------------------------------------------------

	This function should be called from your web page
	with the following
	
	collapseList(listId, lngCollapseThisLevel)
	listId = the id of the UL tag that is
		to be collapsed
	lngCollapseThisLevel = the number of levels
		to NOT collapse. I.e. if you gave a value
		of 1 then the first level of the UL would
		not be collapsed and all further levels
		would be. You may safetly leave out this
		parameter to display all levels
		
---------------------------------------------------------------------------- */
function collapseList(listId, lngCollapseThisLevel) {
	// check for browser compatability
	if (!isCompliant()) return;
	
	// check that the id given exists.
	if (!document.getElementById(listId)) {
		window.status = 'collapseList() error: Can not collapse list with an id of \'' + listId + '\' because it does not exist.';
		return false;
	}
	
	if (lngCollapseThisLevel == null) lngCollapseThisLevel = 0;
	
	// collapse the list of objects
	collapseListObject(document.getElementById(listId), lngCollapseThisLevel);
	
	// add code to all A tags in the list so that their events
	// do not get overwritten by the more dominant UL tags.
	var aNodeList = document.getElementById(listId).getElementsByTagName('a');
	for (var x = 0; x < aNodeList.length; x++) {
		if(!aNodeList[x].onclick) {
			aNodeList[x].onclick = function(e) {
				cancelBubble(e);
			}
		}
	}
}


/* ----------------------------------------------------------------------------
	
	This function should generally be called only from
	the collapseList function or itself (this function
	is recursive)

---------------------------------------------------------------------------- */
function collapseListObject(list, lngCollapseThisLevel) {
	if (!list) return false;
	
	// get all li children
	for(var x = 0 ; x < list.childNodes.length ; x++) {
		if(list.childNodes[x].nodeName.toLowerCase() == 'li') {
			var cn = list.childNodes[x]; //current node
			
			// we only want nodes that have child ul nodes
			for(var y = 0 ; y < cn.childNodes.length ; y++) {
				if(cn.childNodes[y].nodeName.toLowerCase() == 'ul') {
					// process the child node
					collapseListObject(cn.childNodes[y], lngCollapseThisLevel <= 0 ? 0 : lngCollapseThisLevel - 1);
					
					// if this node should be hidden then hide it
					// and attach an onclick event.
					if (!lngCollapseThisLevel) {
						cn.childNodes[y].style.display = 'none';
						
						cn.onclick = function(e) {
							for(var y = 0 ; y < this.childNodes.length ; y++) {
								if(this.childNodes[y].nodeName.toLowerCase() == 'ul') {
									this.childNodes[y].style.display = (this.childNodes[y].style.display == "block") ? "none" : "block";
								}
							}
							cancelBubble(e);
							return false;
						}
					}
				}
			}
		}
	}
}


/* ----------------------------------------------------------------------------
	
	This function should be called from your web page
	with the following
	
	initializeSwitchBox(pstrSwitchId, pstrHideId, pblnDefaultValue, 
		pstrImageOpenSrc, pstrImageClosedSrc, pstrImageOpenOverSrc, 
		pstrImageClosedOverSrc)
	
	pstrSwitchId = The id of the item to act as the switch.
		
	pstrHideId = The item to be hidden or not depending on the switch item
		being clicked on.
	
	pblnDefaultValue = The default value of the hiden item. True will show
		the item by default, false will hide the item by default.
	
	pstrImageOpenSrc = Source of the image to be used when the switch is 
		open.
		
	pstrImageClosedSrc = Source of the image to be used when the switch is 
		closed.
		
	pstrImageOpenOverSrc = Source of the image to be used when the switch is 
		open and the user is hovering over the switch.
		
	pstrImageClosedOverSrc =  Source of the image to be used when the switch is 
		closed and the user is hovering over the switch.

---------------------------------------------------------------------------- */
function initializeSwitchBox(pstrSwitchId, pstrHideId, pblnDefaultValue, pstrImageOpenSrc, pstrImageClosedSrc, pstrImageOpenOverSrc, pstrImageClosedOverSrc) {
	// check for browser compatability
	if (!isCompliant()) return;

	// check that the id's given exist.
	var objSwitch = document.getElementById(pstrSwitchId);
	if (!objSwitch) {
		window.status = 'initializeSwitchBox(): Can not use the object with an id of \'' + pstrSwitchId + '\' as a switch because it does not exist.';
		return false;
	}
	
	var objToggle = document.getElementById(pstrHideId);
	if (!objToggle) {
		window.status = 'initializeSwitchBox(): Can not hide object with id of \'' + pstrHideId + '\' because it does not exist.';
		return false;
	}
	
	// either open or close the box depending on the inital value.
	objToggle.style.display = (pblnDefaultValue) ? "block" : "none";
	
	// add an image
	var newImageName = (pblnDefaultValue) ? pstrImageClosedSrc : pstrImageOpenSrc; 
	
	if (newImageName && newImageName.length > 0) {
		objSwitch.style.backgroundImage = 'url(' + newImageName + ')';
	}
	
	
	objSwitch.onmouseover = function(e) { 
		var objToggle = document.getElementById(pstrHideId);
		var newImageName = (objToggle.style.display == "block") ? pstrImageClosedOverSrc : pstrImageOpenOverSrc; 
		this.className = 'switchOver';
		if (newImageName && newImageName.length > 0) {
			this.style.backgroundImage = 'url(' + newImageName + ')';
		}
	}
	objSwitch.onmouseout = function(e) { 
		var objToggle = document.getElementById(pstrHideId);
		var newImageName = (objToggle.style.display == "block") ? pstrImageClosedSrc : pstrImageOpenSrc; 
		this.className = 'switch';
		if (newImageName && newImageName.length > 0) {
			this.style.backgroundImage = 'url(' + newImageName + ')';
		}
	}
	objSwitch.onclick = function(e) { 
		var objToggle = document.getElementById(pstrHideId);
		objToggle.style.display = (objToggle.style.display == "block") ? "none" : "block"; 
		var newImageName = (objToggle.style.display == "block") ? pstrImageClosedSrc : pstrImageOpenSrc; 
		if (newImageName && newImageName.length > 0) {
			this.style.backgroundImage = 'url(' + newImageName + ')';
		}
		cancelBubble(e);
	}
	
	return true;
}


/* ----------------------------------------------------------------------------

	Check for browser compliancy.
	
	This function returns true if the browser is compliant, false if it is
	not.
	
---------------------------------------------------------------------------- */
function isCompliant() {
	// check for DOM (getElementById)
	if (!document.getElementById) return false;
	
	// generally if this last test is true then every thing is
	// ok but unfortunatly Opera 6 will execute this code
	// but still can not modify most style values.
	// Therefore Opera 6 and below gets the big thumbs down.
	
	// check for Opera 7 and up.
	var isOpera = window.opera ? true : false;
	var fullDom = isOpera && !(navigator.userAgent.indexOf("Opera 6") > 0);
    if (isOpera && !fullDom) return false;
    
    return true;
}


/* ----------------------------------------------------------------------------

	Cancel event bubbling.
	
	IE and W3C dom events are different... so do our 
	code differently

---------------------------------------------------------------------------- */
function cancelBubble(e) {
	if(e) {
		e.stopPropagation();
	}
	else {
		if(window.event && window.event.cancelBubble != null) {
			window.event.cancelBubble = true;
		}
	}
}

