put open and closing menu actions in functions and added support for closing menus on document.click and esc key