/*
 * ul2finder
 * written by Christian Heilmann (http://icant.co.uk)
 * modified by becky Sept. 2005
 * turns the nested list with the ID "finder" into a dynamic list
 * uses the CSS classes defined in the variables
*/
// Check if the browser supports DOM, and start the script if it does.
if(document.getElementById && document.createTextNode)
{
	window.onload=function(bleh)
{
	// Define variables used and classes to be applied/removed
	var i,ols,als,finder,current,currentol;
	var parentClass='parent';
	var showClass='shown';
	var hideClass='hidden';
	var openClass='open';
	// check if our finder list exists, if not, stop all activities
	finder=document.getElementById('finder');
	if(!finder){return;}

	// add the class domenabled to the body
	cssjs('add',document.body,'domenabled')

	// loop through all lists inside finder, position and hide them 
	// by applying the class hidden
	ols=document.getElementById('finder').getElementsByTagName('ol');
	for(i=0;i<ols.length;i++)
	{
		cssjs('add',ols[i],hideClass);
	}
	// find current chapter and un-hide
//	current=document.getElementById('current');
//	currentol=current.parentNode;
//	cssjs('swap',currentol,hideClass,showClass)

	// loop through all links of inside finder
	lis=document.getElementById('finder').getElementsByTagName('li');
	for(i=0;i<lis.length;i++)
	{
		// if the li containing the link has no nested list, skip this one
		if(!lis[i].getElementsByTagName('ol')[0])
		{
			continue;
		}
		var newa=document.createElement('a');
		newa.href='#';
		newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
		lis[i].replaceChild(newa,lis[i].firstChild);
		// otherwise apply the parent class
		cssjs('add',newa,parentClass);

		// if the user clicks on the link
		lis[i].getElementsByTagName('a')[0].onclick=function()
		{
			// change the current link from parent to open 	
			if (this.className == parentClass)
			{
				cssjs('swap',this,parentClass,openClass)
				cssjs('add',this.parentNode.getElementsByTagName('ol')[0],showClass)
			}
			else if (this.className == openClass)
			{
				cssjs('swap',this,openClass,parentClass)
				cssjs('swap',this.parentNode.getElementsByTagName('ol')[0],showClass,hideClass)
			}

			// don't follow the real HREF of the link
			return false;
		}
	}	
	// change the current chapter heading from parent to open
	currentolli=currentol.parentNode;
	currentollia=currentolli.getElementsByTagName('a')[0];
	cssjs('swap',currentollia,parentClass,openClass)
/*
	currentollia.onclick=function()
	{
		// change the current link from parent to open 	
		if (this.className == parentClass)
		{
			cssjs('swap',this,parentClass,openClass)
			cssjs('add',this.parentNode.getElementsByTagName('ol')[0],showClass)
		}
		else if (this.className == openClass)
		{
			cssjs('swap',this,openClass,parentClass)
			cssjs('remove',this.parentNode.getElementsByTagName('ol')[0],showClass)
		}
		// don't follow the real HREF of the link
		return false;
	}
*/

	/*
	 * cssjs
	 * written by Christian Heilmann (http://icant.co.uk)
	 * eases the dynamic application of CSS classes via DOM
	 * parameters: action a, object o and class names c1 and c2 (c2 optional)
	 * actions: swap exchanges c1 and c2 in object o
	 *			add adds class c1 to object o
	 *			remove removes class c1 from object o
	 *			check tests if class c1 is applied to object o
	 * example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
	 */
	function cssjs(a,o,c1,c2)
	{
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp('\\b'+c1+'\\b').test(o.className)
			break;
		}
	}
}

}
