function PrepareMenu(id)
{ 
	YAHOO.util.Event.onAvailable(id, this.handleOnAvailable, this);  
} 

PrepareMenu.prototype.handleOnAvailable = function(me)
{
	//This is the function we're adding an onClick event in the menus.
	function clickHandler(e)
	{
		// When we get a click, we need to find the a's parent li's inner ul...
		var childULelement = YAHOO.util.Dom.getElementsBy( function(e) {return true;}, "ul", this.parentNode);
		
		// ...and make it visible or invisible, as appropriate.
		// We also update the "treeview" arrows here by updating the class of the parent li appropriately.
		var ul;
		for (ul in childULelement)
		{
			if ( ! YAHOO.util.Dom.hasClass(childULelement[ul], "visible") )
			{
				if ( childULelement[ul].parentNode == this.parentNode ) // We need to make sure we aren't digging down deeper than one level in our heirarchy.
				{
					YAHOO.util.Dom.addClass(childULelement[ul], "visible");
					YAHOO.util.Dom.replaceClass(this.parentNode, "hassubmenu", "displaysubmenu");
				}
			}
			else
			{
				YAHOO.util.Dom.removeClass(childULelement[ul], "visible");
				// We need to make sure we set all child ul's parent li's to "hassubmenu" or "closed".
				YAHOO.util.Dom.replaceClass(childULelement[ul].parentNode, "displaysubmenu", "hassubmenu");
			}
		}
		
	}
	
	// First, we need to remove the nojavascript class from the navigation ul, since we obviously have javascript.
	YAHOO.util.Dom.removeClass(this,"nojavascript");
	
	// Next, we grab an array of HTMLElements that are a's under "this" node.
	var Aelements = YAHOO.util.Dom.getElementsBy( function(e) {return true;}, "a", this);
	// Now, we add a click event to each a element in the navigation element. This is the function defined above.
	YAHOO.util.Event.addListener( Aelements, "click", clickHandler );

	// Finally, we remove the href's from the a's that are functioning as headings. Since we've got this expanding menu, we want to expand the menu rather than use the headings as links. We still need the a's for their inner text and for roll-over effects.
	var a;
	for (a in Aelements)
	{
				if ( Aelements[a].parentNode.className == "displaysubmenu" | Aelements[a].parentNode.className == "hassubmenu" )
				{
					   Aelements[a].href = "javascript:toggleMenu()";
				}
	}

// Darn IE 6 (and lower, though that's not tested) needs some extra help. Conditionally, thankfully.
// We need to fix IE by putting firstchild classes in.
/*@cc_on
   /*@if (@_jscript_version < 5.7)
	var LIelements = YAHOO.util.Dom.getElementsByClassName("hassubmenu", "li", this);
	var li;
	for (li in LIelements)
	{
		var LI_Aelements = YAHOO.util.Dom.getElementsBy( function(e) {return true;}, "a", LIelements[li]);
		var li_a;
		for (li_a in LI_Aelements)
		{
			if ( LI_Aelements[li_a].parentNode == LIelements[li] )
			{
				YAHOO.util.Dom.addClass(LI_Aelements[li_a], "firstchild");
			}
		}
	}
   /*@end
@*/

		/* We finally find the link to the current page and make sure that it is shown in the menu. */
		/* We do need to do the IE fix first if we're going to do it, otherwise creates problems. */
		var current;
		var currentPageElement = YAHOO.util.Dom.getElementsBy( function(e) {return ( document.URL.toUpperCase()==e.getAttribute("href").toUpperCase() );}, "a", this);

		for (current in currentPageElement) // For each menu item that links to this page...
		{
			YAHOO.util.Dom.addClass( currentPageElement[current], "currentpage" );

			if (currentPageElement[current].innerHTML != "All Entries" )
			{
			var elementToChange;
			elementToChange = currentPageElement[ current ].parentNode;
  
			// We open up all the menu items above it.
			while ( elementToChange != this )
			{
				// If we have an LI above the elementToChange that hassubmenu, we need to displaysubmenu.
				if ( elementToChange.tagName == "LI" && YAHOO.util.Dom.hasClass( elementToChange, "hassubmenu" ) )
				{
					YAHOO.util.Dom.replaceClass( elementToChange, "hassubmenu", "displaysubmenu" );
				}
				else if ( elementToChange.tagName == "UL" )
					{
						YAHOO.util.Dom.addClass( elementToChange, "visible" );
					}
					elementToChange = elementToChange.parentNode;
				}
			}
		}


} 

var obj = new PrepareMenu("navigation");

/* Here is a nice javascript function to that does nothing. We actually use it as a href function
   in the code above to replace the link text. */
function toggleMenu( e ) { }


/* Extra HaloScan failsafe stuff */
/* This will get overriden by the real HaloScan code if we're hitting a server. */
/* At some point, I really should change *all* the HaloScan stuff to be inserted via JavaScript. */
var hs	 = new Array;
var hstb = new Array;
var hsc	 = new Array;

function HaloScanCount(id, type)
{
	id = id.toLowerCase();
	if (type=="trackback")
		hsc = hstb;
	else
		hsc = hs;
	if(hsc[id])
	{
		document.write(hsc[id] + " ");
	}
	else
	{
		document.write("0 ");
	}
}

function HaloScanPlural(id, type)
{
	id = id.toLowerCase();
	if (type=="trackback")
		hsc = hstb;
	else
		hsc = hs;
	if(hsc[id] != 1)
	{
		document.write("s");
	}
}

function showComments(id)
{
	if ( document.getElementById(id).style.display=="block" )
	{
		document.getElementById(id).style.display="none";
		document.getElementById(id).src="";
	}
	else
	{
		document.getElementById(id).style.display="block";
		document.getElementById(id).src="http://www.haloscan.com/comments/dahjelle/" + id;
	}
}


/* This is a failsafe for the Google urchin tracker, just to avoid javascript errors. */

function urchinTracker() { return true; }

