// 
/*

This set of functions creates show/hide functionality with a <ul class="links">.

HTML Syntax:
<ul class="links">
  <li>Group Title
      <ul>
          <li><a href="foo">Link 1</a></li>
          <li><a href="bar">Link 2</a></li>
      </ul>
  <li>
</ul>

This script grabs the first bit of text in each <li> (in this case, "Group Title"), wraps a <span> around it, then re-selects that span and attaches a click behavior.
The click behavior toggles the visibility of the nested <ul> and toggles a class of "active" on the parent <li>.

*/


// For jQuery to play nicely with others in a complex WordPress install
jQuery.noConflict();

jQuery(document).ready(function() {
	
	// hide the nested <ul>s if they're not already
	jQuery("ul.links ul").hide();


	// loop through each node in each first-level <li>	
	jQuery("ul.links>li").contents().each(function(i,node) {

		// is this node a text node, e.g. "Group Title" in the example above?
		if(node.nodeType == 3 && node.textContent != "\n") {
			
			// wrap a span around it. Can't attach a click handler to just a text node.
			jQuery(node).wrap(document.createElement("span"));
		}
	});
			
	// attach the click behavior to the spans we just created
	jQuery("ul.links>li span").each(function() {

		jQuery(this).click( function() {
			jQuery(this).next("ul").slideToggle(); 
			jQuery(this).parent().toggleClass("active");
			return false;	
		});
		
	});

});


