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

function initMenus()
{
    // Hide all sub UL.
    $('ul.menu ul').hide();
    
    // .. ?
    $.each($('ul.menu'), function()
    {
        var $active = $("li.active");
        
        // Only work off the second LI.
        if($active.length == 2)
        {
            $active = $($active[1]);
        }
        
        // Apply "current" to the active LI, and if NOT the menu head, apply it to the parent LI.
        $active.addClass('current');
        if ( !$active.parent().parent().hasClass('menu') )
        {
            $active.parent().parent().addClass('current');
            $active.parent().show();
        }
        
        //$active.parent().show(); // parent of active li's is the ul.
		//$active.parent().prev().addClass("currentParent"); // parent category styles.
        $("#firstItem").addClass("active");
    });
		
    
    $('ul.menu li a').click(
        function( event )
        {
            // Prevent link sending us anywhere
            event.preventDefault();
            
            // Fetch nessecary objects.
            var $this = $(this);
            var $child_ul = $this.parent().find('ul');
            var $menu = $this.parents('.menu');
            
            // change location if we don't have expandable children
            if ( $child_ul.length == 0 )
            {
                location.href = $this.attr('href');
                $menu.find('.current').removeClass('current');
                
                $this.parents('li').addClass('current');
            }
            
            // Perform rollout if not already selected.
            if ( !$this.parent().hasClass('active') )
            {
                $menu.find('li.active:not(.current) ul').slideUp();
                $menu.find('li.active:not(.current)').removeClass('active');
                
                $this.parent().addClass('active');
                $child_ul.slideDown();
            }           
        }
    );
}
$(document).ready(function() {initMenus();});

