Perfect and optimize the nav-main and the nav-second JavaScript code