$(document).ready(function() {

                $('ul.menu > li').siblings().find('ul').hide(); // set initial state
                $('div.display').not('div#displayfirst').hide();

                $('li > ul a').click(
                    function ()
                    {

                        var current = $(this).next();
                        var old = $(this).parent().siblings().children('ul:visible');

                        if ( current.is(':visible') | current.children().size() == 0 ) // if menu is open or no sub menu, do nothing
                            return false;

                        current.slideDown('fast'); // slide down the sub menu
                        $(this).addClass('open');

                        old.slideUp('fast').removeClass('open'); // get rid of previously opened menu
                        
                    }
                );

                $('ul.menu > li > a').click(
                    function ()
                    {

                        var current = $(this).next();
                        var old = $(this).parent().siblings().find('ul:visible'); //  close all the other sub menus and their children
                        

                        if ( current.is(':visible') | current.children().size() == 0    )
                            return false;

                        current.slideDown('fast');
                        $(this).addClass('open');

                        old.slideUp('fast').removeClass('open');

                    }
                );

                $('ul.menu a').click (
                    function () {
                        var commonName = $(this).attr('name');
                        
                        if ($('div.display:visible').size() == 0) {
                                $('div[name='+commonName+']').fadeIn('slow');
                            }
                            else {
                                $('div.display:visible').fadeOut('fast',
                                    function () {
                                        $('div[name='+commonName+']').fadeIn('slow');
                                    }
                                );
                            }
                    }
                );

                

});
