/***************************************************************************************
        Nested list collapsing script written by Mark Wilton-Jones - 21/11/2003
Version 2.2.0 - this script takes existing HTML nested UL or OL lists, and collapses them
            Updated 13/02/2004 to allow links in root of expanding branch
                  Updated 09/09/2004 to allow state to be saved
          Updated 07/10/2004 to allow page address links to be highlighted
Updated 28/11/2004 to allow you to force expand/collapse links to use just the extraHTML
****************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

This js file has been extensively modified for specific features on the site.  
You may have better luck useing the original script and attending documentation.

____________________________________________________________________________________________________*/
var openLists = [], oIcount = 0, theSelection = 0, debugCount = 0;
var debugging = (window.location.search.indexOf('debugging=true')>-1) ? true : false;
function compactMenu(topObjID,autoCollapse,charEC,oMinimalLink) {
	// check compatibility
	if( !document.getElementsByTagName || !document.childNodes ) { return; }
	var topObj = document.getElementById( topObjID ); 
	if( !topObj ) { return; }
	// collapse tree
	compactChildren( topObj, 0, topObjID, autoCollapse, charEC, topObj.tagName.toUpperCase());
}
function compactChildren( topObj, oLev, topObjID, autoCollapse, charEC, topObjTag) {
	if( !oLev ) { topObjID = escape(topObjID); 
		if( autoCollapse ) { 
			openLists[topObjID] = []; 
		} 
	}
	for( var x = 0, childArray = topObj.childNodes; x < childArray.length; x++ ) {
		if( childArray[x].tagName ) {
			//for each immediate LI child
			var theNextUL = childArray[x].getElementsByTagName( topObjTag )[0];
			var adjust = 0;
			if( theNextUL ) {
				// adjust required because in Win IE, the first childNode is the anchor
				// while for other browsers it is the blank space before the anchor
				adjust = (typeof childArray[x].childNodes[0].innerHTML == 'undefined') ? 1 : 0;
				childArray[x].childNodes[0+adjust].onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + topObjID + '\',' + autoCollapse + ',\'' + escape(topObjTag) + '\',' + false + '); return false;' );
				// assign onclick to link
				childArray[x].childNodes[2+adjust].onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + topObjID + '\',' + autoCollapse + ',\'' + escape(topObjTag) + '\',' + true + ');' );
				theNextUL.MWJuniqueID = oIcount++;
				compactChildren( theNextUL, oLev + 1, topObjID, autoCollapse, charEC, topObjTag);
			} else {
				adjust = (typeof childArray[x].childNodes[0].innerHTML == 'undefined') ? 1 : 0;
				childArray[x].childNodes[0+adjust].onclick = new Function( 'if(this.blur) { this.blur(); } return false;' );
				// assign onclick to link
				childArray[x].childNodes[2+adjust].onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + topObjID + '\',' + autoCollapse + ',\'' + escape(topObjTag) + '\',' + true + ');' );
			}
		}
	} 
}
function clickSmack( oThisOb, oLevel, topObjID, autoCollapse, topObjTag, pageOut ) {
	// blur object if possible
	if( oThisOb.blur ) { oThisOb.blur(); }
	// change clicked object class
	if(oThisOb.className == 'arrowRight') {
		oThisOb.className = 'arrowDown';	
	} else if(oThisOb.className == 'arrowDown') {
		oThisOb.className = 'arrowRight'; 
	}
	if(oThisOb.parentNode.getElementsByTagName( unescape(topObjTag) ).length > 0) {
		// set object to UL under link
		oThisOb = oThisOb.parentNode.getElementsByTagName( unescape(topObjTag) )[0];
	} else {
		// set object to top UL
		oThisOb = document.getElementById(topObjID);
	}
	if(pageOut && oThisOb.style.display == 'block') {
		// do nothing
	} else {
		if( autoCollapse ) {
			for( var x = openLists[topObjID].length - 1; x >= oLevel; x-=1 ) {
				if( openLists[topObjID][x] ) {
					openLists[topObjID][x].style.display = 'none';
					// adjust required because in Win IE, the first childNode is the anchor
					// while for other browsers it is the blank space before the anchor
					var adjust = (typeof openLists[topObjID][x].parentNode.childNodes[0].innerHTML == 'undefined') ? 1 : 0;
					openLists[topObjID][x].parentNode.childNodes[0+adjust].className = 'arrowRight';				
					if( oLevel != x ) { 
						openLists[topObjID][x] = null; 
					}
				} 
			}
			if( oThisOb == openLists[topObjID][oLevel] ) {
				// when collapsing an open list
				openLists[topObjID][oLevel] = null; 
			} else {
				// hides display of submenu until reload
				if(pageOut) {
					oThisOb.style.visibility = 'hidden';
				}
				oThisOb.style.display = 'block';
				openLists[topObjID][oLevel] = oThisOb; 
			}
		} else { 
			oThisOb.style.display = ( oThisOb.style.display == 'block' ) ? 'none' : 'block'; 
		}
	}
}
function stateToFromStr(topObjID,cookieStr) {
	if(debugging) cookieStr = '0:1:4';
	if( !document.getElementsByTagName || !document.childNodes ) { return ''; }
	var topObj = document.getElementById( topObjID ); 
	if( !topObj ) { return ''; }
	if( !cookieStr && typeof(cookieStr) != 'undefined' ) { return ''; } 
	if( cookieStr ) { cookieStr = cookieStr.split(':'); }
	for( var oStr = '', theUl = topObj.getElementsByTagName(topObj.tagName), x = 0; theUl[x]; x++ ) {
		if( cookieStr && MWJisInTheArray( theUl[x].MWJuniqueID, cookieStr ) && theUl[x].style.display == 'none' ) { 
			theUl[x].parentNode.getElementsByTagName('a')[0].onclick();
		} else if( theUl[x].style.display != 'none' ) { 
			oStr += (oStr?':':'') + theUl[x].MWJuniqueID; 
		}
	}
	return oStr;
}
function MWJisInTheArray(oNeed,oHay) { 
	for( var i = 0; i < oHay.length; i++ ) { 
		if( oNeed == oHay[i] ) { return true; } 
	} 
	return false; 
}
function selfLink(oRootElement,oClass,oExpand) {
	if(!document.getElementsByTagName||!document.childNodes) { return; }
	oRootElement = document.getElementById(oRootElement);
	for( var x = 0, y = oRootElement.getElementsByTagName('a'); y[x]; x++ ) {
		if( y[x].getAttribute('href') && !y[x].href.match(/#$/) && getRealAddress(y[x]) == getRealAddress(location) ) {
			y[x].className = (y[x].className?(y[x].className+' '):'') + oClass;
			if( oExpand ) {
				oExpand = false;
				for( var oEl = y[x].parentNode, ulStr = ''; oEl != oRootElement && oEl != document.body; oEl = oEl.parentNode ) {
					if( oEl.tagName && oEl.tagName == oRootElement.tagName ) { 
						ulStr = oEl.MWJuniqueID + (ulStr?(':'+ulStr):''); 
					} 
				}
				stateToFromStr(oRootElement.id,ulStr);
			} 
		} 
	} 
}
function getRealAddress(topObj) { 
	return topObj.protocol + ( ( topObj.protocol.indexOf( ':' ) + 1 ) ? '' : ':' ) + topObj.hostname + ( ( typeof(topObj.pathname) == typeof(' ') && topObj.pathname.indexOf('/') != 0 ) ? '/' : '' ) + topObj.pathname + topObj.search; 
}
function getTheSelection(topObjID) {
	if( !document.getElementsByTagName ) { return; }
	var topObj = document.getElementById( topObjID ); 
	if( !topObj ) { return; }
	var anchorArray = topObj.getElementsByTagName('a');
	for(var x=0; x<anchorArray.length; x++) {
		if(anchorArray[x].href == window.location.href) {
			anchorArray[x].className = 'thisPageLink';
			anchorArray[x].href = '#';
			anchorArray[x].onclick = new Function( 'if(this.blur) { this.blur(); } return false;' );
			break;
		}
	}
}

