title: FancyBox and Youtube API events
body: <p>I'm trying to fire an event once the youtube video that I'm playing into my fancybox is ended.</p>
  
  <p>So I can close automatically the fancybox once the video is over.</p>
  
  <p>I have the last fancybox version and I'm on the youtube API, sticking to exemples but it seems that ytplayer object is "undefined" and I can't make it work properly.</p>
  
  <p>I have read a lot of stuffs on internet including this one, which seems to be good: <a href="https://stackoverflow.com/questions/7541890/how-make-fancybox-auto-close-when-youtube-video-id-done">How make fancybox auto close when youtube video id done?</a></p>
  
  <p>This is the code I'm using: <a href="http://jsfiddle.net/xn4FB/" rel="nofollow noreferrer">JsFiddle</a></p>
  
  <pre><code>$(".fancybox").fancybox({
      'openEffect'  : 'none',
      'closeEffect' : 'none',
      'overlayOpacity' : 0.7,
      'helpers' : {
          media : {}
      },
      'afterLoad' :   function() {
          function onYouTubePlayerReady(playerId) {
              //alert(playerId);
              //alert(document.getElementById("myytplayer"));
              ytplayer = document.getElementById("myytplayer");
              ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          }
  
          function onytplayerStateChange(newState) {
              //alert("Player's new state: " + newState);
              if (newState == 0){
                  $.fancybox.close(true);
              }
          }
      }
  });
  
  // Launch fancyBox on first element
  $(".fancybox").eq(0).trigger('click');
  </code></pre>
  
  <p>If someone get this working, it would be awesome ! I just the video to close the fancybox once it finished !</p>
  
  <p>Thanks a lot</p>