/*
 * ul2nav
 * written by Christian Heilmann (http://icant.co.uk)
 * turns the nested list with the ID "nav" into a dynamic list
 * uses the CSS classes defined in the variables
 * used and modified with much love by co-op media.
 */
function ul2nav()
{
	// Define variables used and classes to be applied/removed
	var i,uls,als,nav;
	var parentClass='parent';
	var showClass='shown';
	var hideClass='hidden';
	var openClass='open';
	// check if our nav list exists, if not, stop all activities
	nav=$('nav');
	if(!nav){return;} else {
		nav.setStyle('visibility','visible');
	}
	

	// loop through all lists inside nav, position and hide them 
	// by applying the class hidden
	uls = $$('ul#nav ul');
	uls.each(function(theUl){
		theUl.addClass(hideClass);
	});

	// loop through all links of inside nav
	lis=$('nav').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 = new Element('a', {
		    'class': parentClass,
		    'href': '#'
		});
		
		newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
		lis[i].replaceChild(newa,lis[i].firstChild);
		// otherwise apply the parent class
		// if the user clicks on the link
		lis[i].getElementsByTagName('a')[0].onclick=function()
		{
		// loop through all lists inside nav
			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)
				{
					uls[i].addClass(hideClass);
					uls[i].removeClass(showClass);
					uls[i].parentNode.getElementsByTagName('a')[0].removeClass(openClass);
					uls[i].parentNode.getElementsByTagName('a')[0].addClass(parentClass);
				}
			}	
			// change the current link from parent to open 	
			//cssjs('swap',this,parentClass,openClass)
			this.addClass(parentClass);
			this.removeClass(openClass);
			// show the current nested list 
			//cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass)
			this.parentNode.getElementsByTagName('ul')[0].addClass(showClass);

			// don't follow the real HREF of the link
			return false;
		}
	}
	
	if ($$('ul#nav li.current_page_item').getParent().getParent().getParent().hasClass('hidden')){
		$$('ul#nav li.current_page_item').getParent().getParent().getParent().addClass('shown');
	}
	if ($$('ul#nav li.current_page_item').getParent().hasClass('hidden')){
		$$('ul#nav li.current_page_item').getParent().addClass('shown');
	}
	
	if ( $chk($E('li.current_page_item', 'nav')) ){
	 	 $E('li.current_page_item', 'nav').getParent().getParent().getFirst().setStyle('color','#111111');
	 }


	if ( $chk($E('li.current_page_item', 'nav')) ){
		$E('li.current_page_item', 'nav').getParent().getParent().getParent().getParent().getFirst().setStyle('color','#111111');
	}
	

}

