Expandable lists problem

Expandable lists problem

I am developing an expandable list in a website. I got it working to where the nested lists expand using slideToggle and if I click on another list item, any expanded lists collapse and the clicked list expands but I cant figure out one last thing... when a list is already expanded, and the person wants to collapse it, I want them to be able to click on the parent list item to collapse the open list. It currently does collapse it but then opens it immediately after. I need to be able to put some kind of conditional that says if the targeted list was already expanded, dont expand it again. my code is below. Any help is greatly appreciated!

<html>

<head>

    <style type="text/css">
   
   
   
    </style>
   
   
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
   
    $(function(){
   
        // hiding all expandible lists
        $("li.expand a").next("ul").css("display","none");
   
        // functionality to expand lists
        $("li.expand a").click(
            function(e){
               
                // collapse other lists in window
                $('li.expand a').next('ul').slideUp('fast');
               
              //  expand clicked list
              $(e.target).next().slideToggle('fast');
             
                return false;
            }
        );
    });
   
    </script>


</head>

<body>

    <ul class="expand-list">
                       
        <li class="expand"><a href="#">list item 1</a>
       
            <ul>
                <li>hidden content</li>
                <li>hidden content</li>
            </ul>
       
        </li>
       
        <li class="expand"><a href="#">list item 2</a>
       
            <ul>
                <li>hidden content</li>
                <li>hidden content</li>
                <li>hidden content</li>
            </ul>
       
        </li>
       
    </ul>

</body>

</html>

I want it to work similar to sample 3 on this page: http://jquery.bassistance.de/treeview/demo/?1