/* Introduction
Simple JQuery menu.
HTML structure to use:

    Notes: 

    Each menu MUST have a class 'menu' set. If the menu doesn't have this, the
    JS won't make it dynamic.  If you want a panel to be expanded at page load,
    give the containing LI element the classname 'here'.  Use this to set the
    right state in your page (generation) code.
    
    Each submenu MUST have the class 'sub' set.

<ul class="menu">
    <li>
        <a href="#">Sub menu heading</a>
        <ul class="sub">
            <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>
    // This item is open at page load time
    <li class="here">
        <a href="#">Sub menu heading</a>
        <ul class="sub">
            <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>
    // This item has no submenus.  It jus links to a page.
    // It would be good for a 'HOME' link.
    <li>
        <a href="http://site.com/">Link</a>
    </li>
    ...
    ...
</ul>

Copyright 2007-2010 by Marco van Hylckama Vlieg

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

Free to use any way you like.
*/


jQuery.fn.initMenu = function() {  
    return this.each(function(){
    
        //Set some variables (there were more, they just didn't do anything, so I deleted them)
        var speed = 'normal'; //If in doubt, choose 'normal'
        
        //Show the current menu
        $('.sub', this).hide();
        $('li.here > .sub', this).show();
        $('li.here > .sub', this).prev().addClass('active');
        
        // Do some stuff!
        $('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if(theElement.hasClass('sub') && theElement.is(':visible')) {
                    $('.sub:visible', parent).first().slideUp(speed, 
                        function() {
                            $(this).prev().removeClass('active');
                        }
                    );
                    return false;
                }
                if(theElement.hasClass('sub') && !theElement.is(':visible')) {         
                    $('.sub:visible', parent).first().slideUp(speed, function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown(speed, function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }
            }
        );
    });
};

$(document).ready(function() {$('.menu').initMenu();});
