/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>
*/
jQuery.haccordion = function()
{
	$j('ul.menu ul').hide();
	$j.each($j('ul.menu'), function()
	{
		var cookie = $j.cookie(this.id);
		if(cookie != null && String(cookie).length < 1)
		{
			$j('#' + this.id + '.expandfirst ul:first').show();
		}
		else
		{			
			$j('#' + this.id + ' .' + cookie).next().show();
		}
		return;
	});
	$j('ul.menu li a').click(function()
	{
		var checkElement = $j(this).next();
		var parent = this.parentNode.parentNode.id;

		if($j('#' + parent).hasClass('noaccordion'))
		{
			if((String(parent).length > 0) && (String(this.className).length > 0))
			{
				if($j(this).next().is(':visible'))
				{
					$j.cookie(parent, null);
				}
				else
				{
					$j.cookie(parent, this.className);
				}
				$j(this).next().slideToggle('normal');
			}				
		}
		if((checkElement.is('ul')) && (checkElement.is(':visible')))
		{
			if($j('#' + parent).hasClass('collapsible'))
			{
				$j('#' + parent + ' ul:visible').slideUp('normal');
			}
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible')))
		{
			$j('#' + parent + ' ul:visible').slideUp('normal');
			if((String(parent).length > 0) && (String(this.className).length > 0))
			{
				$j.cookie(parent, this.className);
			}
			checkElement.slideDown('normal');
			return false;
		}
	});
}