/* 
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 'expand'.
Use this to set the right state in your page (generation) code.

Optional extra classnames for the UL element that holds an accordion:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandall : expands all sub menus and adds the class noaccordion

<ul 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>
// This item is open at page load time
<li class="expand"><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>

====================================================================

<ul class="menu">
		<li><a href="http://www.i-marco.nl/">Home</a></li>
		<li>
			<a href="#">Weblog Tools</a>
			<ul class="acitem">
				<li><a href="http://www.pivotx.net/">PivotX</a></li>
				<li><a href="http://www.wordpress.org/">WordPress</a></li>
				<li><a href="http://www.textpattern.com/">Textpattern</a></li>
				<li><a href="http://typosphere.org/">Typo</a></li>
			</ul>
		</li>
		<li>
			<a href="#">A Panel</a>
			<div class="acitem panel">
				<p>This contains stuff</p>
				<p>There can be <a href="http://www.i-marco.nl/">links</a> too</p>
				<ul>
					<li>blerk</li>
					<li>wonk</li>
					<li><a href="#">meh</a></li>
				</ul>
			</div>
		</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(){
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
		
		$('.acitem li a').each(function() {    // find the selected link and add the class "selected" to it
			if ($(this).attr("href")==getFilename()) {
				$(this).addClass("selected");
				return false;
			}
		});
		
		if ($(this).hasClass('expandall')) {
			$(this).addClass('noaccordion');
			$('ul.acMenu > li').addClass('expand');	
		} else {
			$('a.selected', this).parents('li').eq(1).addClass('expand');  // find selected item add expand class to li container
		}
		
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
			
		
        $('li a', this).click(
            function() {
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
				
				if ($(theElement).prev().hasClass('slide')) {
					
                if($(parent).hasClass('noaccordion')) {
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }    
                    });
                    return false;
                }
                else {
                    if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', 
                            function() {								
                                $('.acitem:visible', parent).first().prev().removeClass('active');
								$(this).prev().removeClass('active');
                            }
                        );
                    }
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {                        
                    $('.acitem:visible', parent).first().slideUp('normal', function() {																		
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }
            }
			}
        }
    );
});
};

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