This is only a few lines of code but I don't see the problem!

Here is a test page:
http://phpby.me/test/

Here are the steps to recreate the bug:

1. click a main nav link
2. click a sub nav link to expose the sub sub nav
3. click the same sub nav link to close the sub sub nav
4. click the main nav to close the sub nav
5. start over
6. this time when you go to click a sub nav to expose a sub sub nav it will open then close by itself.

why does it close by itself? it should stay open.

Here's the js. it's a little sloppy, I've been hammering away at this issue and changed code so many times:
Code JavaScript:
$(function(){
 
    $('a.sub').click(function(e){
        e.preventDefault();
        $(this).parent().toggleClass('showSub');
 
 
        // this element is display:none; by default, attach event after made display:block; by toggleClass above
        $('li.showSub > ul > li > a').click(function(e){
            e.preventDefault();
 
 
            // slide up all potentially open sub navs
            //$('.showSubSub').slideUp();
 
	    // add a class for styles, and slide it down
            //$(this).next().addClass('showSubSub').slideDown();
 
            if ( $(this).next().is(":visible") ) {
                $(this).next().slideUp();
            }
 
            if  ( $(this).next().is(":hidden") ){
                $(this).next().slideDown();
            }
 
        })
 
    })
 
 
 
})