jQuery(document).ready(function(jm){
jQuery.fn.initMenu = function() {  
    return this.each(function(){
        var theMenu = jm(this).get(0);
        jm('.children', this).hide();
        jm('li.expand > .children', this).show();
        jm('li.current_page_ancestor > .children', this).show();
        jm('li.expand > .children', this).prev().addClass('active');
        jm('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = jm(this).next();
                var parent = this.parentNode.parentNode;
                if(jm(parent).hasClass('noaccordion')) {
                    if(theElement[0] === undefined) {
                        window.location.href = this.href;
                    }
                    jm(theElement).slideToggle('normal', function() {
                        if (jm(this).is(':visible')) {
                            jm(this).prev().addClass('active');
                        }
                        else {
                            jm(this).prev().removeClass('active');
                        }    
                    });
                    return false;
                }
                else {
                    if(theElement.hasClass('children') && theElement.is(':visible')) {
                        if(jm(parent).hasClass('collapsible')) {
                            jm('.children:visible', parent).first().slideUp('normal', 
                            function() {
                                jm(this).prev().removeClass('active');
                            }
                        );
                        return false;  
                    }
                    return false;
                }
                if(theElement.hasClass('children') && !theElement.is(':visible')) {         
                    jm('.children:visible', parent).first().slideUp('normal', function() {
                        jm(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        jm(this).prev().addClass('active');
                    });
                    return false;
                }
            }
        }
    );
});
};

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