function onYouTubePlayerAPIReady() { $(document).ready(function () { $.fancybox({ href: "http://www.youtube.com/embed/L9szn1QQfas?enablejsapi=1&wmode=opaque&autoplay=1", type: "iframe", beforeShow: function () { // Find the iframe ID var id = $.fancybox.inner.find('iframe').attr('id'); // Create video player object and add event listeners var player = new YT.Player(id, { events: { 'onStateChange': function (event) { if (event.data === 0) { $.fancybox.close(); } // if } // onStateChange } // events }); // YT.Player } // beforeShow }); // fancybox }); // ready } // onYouTubePlayerAPIReady