$(".js-movieModal-btn").on("click", function () {
current_modal = $(this).data("modalbtn"); //data-modalbtn="movie00"
$("#js-movieModal").fadeIn();
$('[data-modalcontent="' + current_modal + '"]').fadeIn(
var div = $(".player", this);
var playerId = div.attr("id"); //id="player00"
var youtubeId = div.data("id"); //data-id="youtube動画ID"
prepareToPlay(playerId, youtubeId);
$("#js-movieModal-overRay,#js-movieModal-close").on("click", function () {
stopVideo(currentPlayer);
removePlayerVideo(currentPlayer);
$('[data-modalcontent="' + current_modal + '"]').fadeOut();
$("#js-movieModal").fadeOut();
var scriptLoaded = false;
var currentPlayer = null;
//youtubeAPI用のJavaScript呼び出し
scriptTag = document.createElement("script");
scriptTag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag);
//API用のJavaScriptのダウンロードが完了すると呼び出される
function onYouTubeIframeAPIReady() {
function prepareToPlay(id, youtubeId) {
var intervalId = setInterval(function () {
clearInterval(intervalId);
var h = Math.floor(w * 0.5625);
//第一引数のid(div)がiframeに置き換えられる
var player = new YT.Player(id, {
// 'onReady': onPlayerReady,
onStateChange: onPlayerStateChange,
// function onPlayerReady(event) {
// event.target.playVideo();
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
function stopVideo(player) {
function removePlayerVideo(player) {
var iframe = $("#" + id);
var youtubeId = iframe.data("id");
'<div id="' + id + '" class="player" data-id="' + youtubeId + '">',