Advanced jQuery Popout Window
<p>Several problems:</p>

<p>1) I am trying to make this script run more efficiently.</p>

<p>2) When the user clicks either pop out button it opens a windows and hides the element. (Currently I am using .detach() to remove the embedded video player because in Firefox .toggle() just hides the player but keeps the audio playing. Is there a better way to do this?</p>

<p>3) In theory by clicking the button again or closing the window manually it should un hide or .toggle() the element but does not for the video player due to detach().</p>

<p>4) If a user pops out the window manually closes it and then pops it out again to only close it once more the element does not .toggle() back.</p>

<p>See it in action here, <a href="http://www.mst3k.tv/" rel="nofollow">http://www.mst3k.tv/</a>.</p>

<pre><code>$(document).ready(function() {
  $('#lights').click(function(){$('#darkness').fadeToggle(500);});
  $("#lights").toggle(function(){$("#lights").attr('id','lightsoff');},function(){$("#lightsoff").attr('id','lights');});
  /**VIDEO**/
  var videoWin;
  $('#video-toggle').click(function(){
      $('#video').fadeToggle(500);
      $('#video').detach();
      });
  $('#video-toggle').click(function(){
      if (videoWin &amp;&amp; !videoWin.closed) {
        videoWin.close();
        return false;
      }
      videoWin = window.open(
        $(this).attr('rel'),
        'videoWin',
        'width=600,height=480,toolbar=0,top=0,left=0,menubar=0,location=0,status=0,scrollbars=0,resizable=1');
      return false;
      }
    );
  var watchVideo = setInterval(function() {
      if (videoWin.closed) {clearTimeout(watchVideo);$('#video').show(500)}
      return false;
   }, 1);
  /**CHAT**/
  var chatWin;
  $('#chat-toggle').click(function(){
      $('#chat').fadeToggle(500);
      /*$('#chat').detach();*/
      });
  $('#chat-toggle').click(function(){
      if (chatWin &amp;&amp; !chatWin.closed) {
        chatWin.close();
        return false;
      }
      chatWin = window.open(
        $(this).attr('rel'),
        'chatWin',
        'width=320,height=480,toolbar=0,top=0,left=601,menubar=0,location=0,status=0,scrollbars=0,resizable=1');
      return false;
      }
    );
  var watchChat = setInterval(function() {
      if (chatWin.closed) {clearTimeout(watchChat);$('#chat').show(500)}
      return false;
   }, 1);
  /*$("a.btn").fitText(1.2, { minFontSize: "6px", maxFontSize: "14px" });*/
});
</code></pre>
