Sorry I can't help, I'm not a JavaScript coder. It just occurred to me that the concept was potentially flawed anyway... Have you seen anywhere where this functionality is implemented? If so, you should <ctrl>U or <ctrl>P the page and study it. That, or at least give us a link to it, so we can see the functionality you are seeking to implement...
By the way when you say "hide" do you mean that the tabs will automatically display when moused over? If so:
1. That is very different from closing the tab.
2. What about mobile users (who do not have hover functionality)?
By the way, I appreciate that I may be asking questions that are not directly relevant to the task you have been set. However, thinking around the objective could lead to a better solution than focusing simply on the task as set...