﻿var oldUL = null; 
function checkSameTier(element)
{
	/* structure
	<ul class="conferenceDocs roottier" >
	<li><a href='#' class='heirarchy' onclick="javascript:checkSameTier(this);return false;">1 Plenary Presentations &raquo;</a>
	<ul style="display:none" class="normaltier">
		<li><a href='#' class='heirarchy' onclick="javascript:checkSameTier(this);return false;">GARD guide &raquo;</a>

		<ul style="display:none" class="normaltier">
			<li><a href='./conf_doc/1_Plenary_Presentations/GARD_guide/01_GARD_Guide_Introduction.pdf' class="pdfLank" >01_GARD_Guide_Introduction.pdf</a></li>
			<li><a href='./conf_doc/1_Plenary_Presentations/GARD_guide/02_GARD_Guide_Content.pdf' class="pdfLank" >02_GARD_Guide_Content.pdf</a></li>
			<li><a href='./conf_doc/1_Plenary_Presentations/GARD_guide/03_GARD_Guide_Application.pdf' class="pdfLank" >03_GARD_Guide_Application.pdf</a></li>
			<li><a href='./conf_doc/1_Plenary_Presentations/GARD_guide/04_GARD_Guide_NEXT_PHASE.pdf' class="pdfLank" >04_GARD_Guide_NEXT_PHASE.pdf</a></li>
		</ul>
	</li>
	</ul>
	*/
	
	var origEl = element;	// <a> element sitting in a <li> tag in a <ul> tag 
	var node = origEl.parentNode.parentNode;	// (should be) the <ul> element
	
	// Gather the intended UL that the user intends to be hidden/shown
	var chosenUL = origEl.nextSibling.nextSibling;
	/*
	origEl = <ul><li><a>
	origEl.nextSibbling = <ul><li>#text (? no idea why, probably a linebreak, a space, some padding who knows what but it's there all the same)
	origEl.nextSibling.nextSibling = <ul><li><ul> Ah here we are
	
	okay technically my php has added this #text node (bad Paul)
	for futre project if one wants to fullproof this code,
	one could have second node/trawl routine here instead of this direct dial with sibblings,
	or functionalise the node trawler with UL check (as featured below) and add some fancy parameters and boolean / node value returns
	
	*/
	var i = 0;
	var innerEl = null;
	// node.className = "";
	
	

	
	
	
	/* 	CLOSE 
		go through all the <li> in the <ul> (node) and set thier displays to none
	*/
	for (i = 0; i < node.childNodes.length; i++)
	{
		// for mac ie5 this for loop is more appropriate than the one above 
		// 	for( var x = 0; node.childNodes[x]; x++ )
		sameTierEl = node.childNodes[i];
		for (j=0; j < sameTierEl.childNodes.length; j++) 
		{
			innerEl = sameTierEl.childNodes[j];
			
			/* possible hits are <a>, #text, <ul> */
			if (innerEl.nodeName == "UL")
			{
				if (innerEl != chosenUL)
				{
					innerEl.style.display = "none";
				}
			}
		}
	}
	
	
	
	/*
		OPEN
			
		and yes i know i could have incorporated into the above CLOSE "loop", 
		but then you'd have the possibility of the scenario of having TWO open UL tags at the same time for handfull of milleseconds,
		this will cause a momentary spasm of a longer html page, (heaven forbid a flicker)
		however if you have  other scripts that rely on scrollHeights they might get freaked out 
		besides then you wouldn't have this nice symmetry in the code of a close and open sections =O)
	*/
	
	/* last minute turn off currently selected UL children*/
	var chosenUL_LI_child = null;
	var chosenInnerLINode = null;
	
	// alert("chosenUL.nodeName == " chosenUL.nodeName);
	if ( chosenUL.style.display != "list-item")
	{
		
		
		/*
			last minute turn off the children of selected UL , so one can revisit a link without an open list being present
		*/
		/*
		for (x=0; x < chosenUL.childNodes.length; x++) 
		{
			chosenUL_LI_child = chosenUL.childNodes[x];
			
			alert("innerEl.nodeName = " + innerEl.nodeName);
			// possible hits are , #text, <LI> 
		//	alert("CHOSEN HAS TAGS.nodeName == " + chosenUL_LI_child.nodeName);
		
		
			if (chosenUL_LI_child.nodeName == "LI")
			{
			//	alert("CHOSEN HAS LI TAGS");
				for (y = 0; y < chosenUL_LI_child.childNodes.length; y++)
				{
					// for mac ie5 this for loop is more appropriate than the one above 
					// 	for( var x = 0; node.childNodes[x]; x++ )
					chosenInnerLINode = chosenUL_LI_child.childNodes[y];
					for (z=0; z < chosenInnerLINode.childNodes.length; z++) 
					{
						
						chosenInnerNodeUL = chosenInnerLINode.childNodes[z];
				//		alert("CHOSEN HAS LI TAG which has children . nodename = " + chosenInnerNodeUL.nodeName);
						
						// possible hits are <a>, #text, <ul>
						if (chosenInnerNodeUL.nodeName == "LI")
						{
					//		alert("CHOSEN HAS LI TAGS");
							chosenInnerNodeUL.style.display = "none";
						}
					}
				}
			}
		}
		*/
		
		// SHOW CHOSEN UL!!!
		chosenUL.style.display = "list-item";
		//alert("list item needs to be SHOWN");
	}
	else
	{
		chosenUL.style.display = "none";
		//alert("list item needs to be HIDDEN");
	}
	
	/*
	last minute addition which goes against a whole deal of "the concept" of the above, but hey it's a last minute taped on addition so sue me,
	it closes the old UL node, which may lay outside the original node trawl and won't get closed.
	the suggested node trawler would be handy to iteratively search through the node.children, and turn them off, within the old node.
	as this would also check to see the possibility that old UL (a parent to the newly selected UL (of which is a child to the old) 
	*/
	
	/*
	if (oldUL != null && chosenUL != oldUL)
	{
		oldUL.style.display = "none";
		
	}
	
	oldUL = chosenUL;
	*/
	
	
}
