I have managed to come up with this javascript to slide down divs. 

When clicked, it slides everything up, and then slides the one that was clicked.

The problem is when the same header is clicked when it was already open, causes the content to slide back up then back down again. 

What is needed for the code to only slide back up if it is already displayed?

```
    function toggle_visibility(id) {
        var thelist = document.getElementsByClassName("alist");
        for (var i = 0; i < thelist.length; i++) {
              $(thelist[i]).slideUp( "slow", function() {});
        }

    //break if the DIV was displayed last!
        var e = document.getElementById(id);
        $( e ).slideDown( "slow", function() {});
    }

```