Need a little help with accordion menu

Need a little help with accordion menu

Hello, 
Totaly new in jquery here and I need a little help.
So I have one accordion menu which is working but I have to add one function. Now when I click on menu item is open properly but if doesn't close if I click again on the item. For example I have: 
  1. Item1-head
  2.  - sub-item1
  3.  - sub-item2
I want when I click on item1-head to open and if I click again on item1-head to close. Now only is opening..
Here is the code
  1.     $(document).ready(function () {
  2.         $('#nav > li > a').click(function(e){
  3.             if ($(this).attr('class') != 'active'){
  4.                 $('#nav li ul').slideUp();
  5.                 $(this).next().slideToggle();
  6.                 $('#nav li a').removeClass('active');
  7.                 $(this).addClass('active');
  8.             }
  9.             e.preventDefault();
  10.         });
  11.     });