/*
 * adapted from "ul2finder"
 * written by Christian Heilmann (http://icant.co.uk)
 * turns the nested list with the ID "domnav" into a dynamic list
 * uses the CSS classes defined in the variables
 */
 
function ul2finder()
{
	// Define variables used and classes to be applied/removed
	var i,uls,als,lis,finder;
	var parentClass='parent';
	var showClass='shown';
	var hideClass='hidden';
	var openClass='open';
	var offClass='off';
	var onClass='on';

	// check if our finder list exists, if not, stop all activities
	finder=document.getElementById('domnav');
	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
	uls=document.getElementById('domnav').getElementsByTagName('ul');
	for(i=0;i<uls.length;i++)
	{
		cssjs('add',uls[i],hideClass);
	}	

	// loop through all links of inside finder
	lis=document.getElementById('domnav').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('ul')[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()
		{
		// loop through all lists inside finder
			for(var i=0;i<uls.length;i++)
			{
				// avoid the list connected to this link
				var found=false;
				for(j=0;j<uls[i].getElementsByTagName('ul').length;j++)
				{
					if(uls[i].getElementsByTagName('ul')[j] == 		
						this.parentNode.getElementsByTagName('ul')[0])
					{
						found=true;
						break;
					}
				}
				// and hide all others
				if(!found)
				{
					cssjs('add',uls[i],hideClass)
					cssjs('remove',uls[i],showClass)
					cssjs('remove',uls[i].parentNode.getElementsByTagName('a')[0],openClass)
					cssjs('add',uls[i].parentNode.getElementsByTagName('a')[0],parentClass)
				}
			}	
			// change the current link from parent to open 	
			cssjs('swap',this,parentClass,openClass)
			// show the current nested list 
			cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass)

			// don't follow the real HREF of the link
			return false;
		}
	}	

	/*showActive by Eric Shepherd. Takes the current page
	/*and writes CSS classes to orient the user to his
	/*location in the site.*/

	showActive = function() { //shows active site area in nav
		var allBody = document.getElementsByTagName("body");
		var theBody = allBody.item(0).id;
		var bodyArray = theBody.split("-");
		var theLink = "li-" + bodyArray[1];
		cssjs('add',document.getElementById(theLink),openClass); //highlight class for active area
		theChild = "dom-" + bodyArray[1];
		cssjs('add',document.getElementById(theChild),showClass); //shows subnav of active section
		for(var i=0;i<document.getElementById(theChild).getElementsByTagName('a').length;i++) {
			var entireLink = document.getElementById(theChild).getElementsByTagName('a')[i].href;
			var strippedLink = stripLink(entireLink);
			if((strippedLink == bodyArray[2])&&(entireLink.indexOf("#") == -1)) {
					cssjs('add',document.getElementById(theChild).getElementsByTagName('li')[i],onClass);
				} else {
					cssjs('add',document.getElementById(theChild).getElementsByTagName('li')[i],offClass);
				}
		//special instructions for industrial, as the script isn't scaling well
			//if(document.getElementsByTagName('body')[0].className == "industrial domenabled") {
				//cssjs('add',document.getElementById('li-industrial'),openClass);
				//cssjs('remove',document.getElementById('li-industrial'),offClass);
				//cssjs('add',document.getElementById('dom-industrial'),showClass);
			//}
		}
		
		//we need special treatment for the market navs, easy because of the IDs
		if(bodyArray[1] == "markets") {
			if((bodyArray[2] == "fuelcell")||(bodyArray[2] == "automotive")) { //again, hacks for industrial problem child
				bodyArray[2] = "industrial";
			}
			var aID = "a-" + bodyArray[2];
			cssjs('add',document.getElementById(aID),onClass);
		}
	}

	
	/*
	 * cssjs
	 * written by Christian Heilmann (http://icant.co.uk)
	 */

	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;
			
		}
	}

	showActive(); //to get the active section notated on the nav bar
	
}

// Check if the browser supports DOM, and start the script if it does.
if(document.getElementById && document.createTextNode)
{
	window.onload=ul2finder;
}

function stripLink(linkHref) //splits the url into pieces
{
	var linkArray = (linkHref.split("-")[1]).split(".");
	return linkArray[0];
}

