/* 
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>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

/*
Modified by Dean Whillier

Set to only collapse child ul's that have a class of 'menuitem' set.
Prevents collapsing of grandchild ul's that make up a multi-level navigation but don't need to be collapsed
*/

function initMenus() {
	jQuery('ul.menu ul.level2').hide();
	jQuery.each(jQuery('ul.menu'), function(){
	    jQuery('#' + this.id + ' ul.current').show();
	    jQuery('#' + this.id + '.expandfirst ul:first').show();
	    jQuery('#' + this.id + ' ul.expanded').show();
	});
	
	jQuery('ul.menu>li>a').click(

		function() {

	        var checkElement = jQuery(this).next();
		    var checkElementTest = jQuery(this).next(); 
		    var parent = this.parentNode.parentNode.id;
		    //parent = subNav

		    //This is to toggle the base nav level images to add/remove the + for expanding
		    if (jQuery('#' + parent + ' ul').hasClass('level2')) {
		        jQuery.each(jQuery('ul.menu>li>a'), function() {
		            jQuery(this).removeClass('current');
		        });
		        jQuery(this).addClass('current');
		    }

		    //not in use?
		    if (jQuery('#' + parent).hasClass('noaccordion')) {
		        jQuery(this).next().slideToggle('normal');
		        return false;
		    }

		    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		       // alert('yo1');
		        if (jQuery('#' + parent).hasClass('collapsible')) {
		            jQuery('#' + parent + ' ul:visible.level2').slideUp('normal');
		            jQuery('ul.menu ul.notmenu').slideDown('normal');
		        }
		        return false;
		    }
		    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		        //alert('yoyo1');
		        jQuery('#' + parent + ' ul:visible.level2').slideUp('normal');
		        checkElement.slideDown('normal');
		        return false;
		    }
		}
	);
}
jQuery(document).ready(function() {initMenus();});
