var mainmenu = (function() { var $listitems = $( '#mainmenu > ul > li' ), $menuitems = $listitems.children( 'a' ), $body = $( 'body' ), current = -1; function init() { $menuitems.on( 'click', open ); $listitems.on( 'click', function( event ) { event.stoppropagation(); } ); } function open( event ) { var $item = $( event.currenttarget ).parent( 'li.has-submenu' ), idx = $item.index(); if($item.length != 0){ if( current !== -1 ) { $listitems.eq( current ).removeclass( 'mainmenu-open' ); } if( current === idx ) { $item.removeclass( 'mainmenu-open' ); current = -1; } else { $item.addclass( 'mainmenu-open' ); current = idx; $body.off( 'click' ).on( 'click', close ); } return false; } else window.location = $item.find('a').attr('href'); } function close( event ) { $listitems.eq( current ).removeclass( 'mainmenu-open' ); current = -1; } return { init : init }; })();