﻿/* implements the "onYouTubeIframeAPIReady" function called by the YouTube IFrame API */
function onYouTubeIframeAPIReady()
{
	console.log('onYouTubeIframeAPIReady() called');
	jQuery('.ccms-banner-muteYouTubeVideoPlayer').each(function (index, item)
	{
		try
		{
			var divId = $(item).attr('id');
			var videoId = $(item).data('videoid');

			var player = new YT.Player(divId, {
				videoId: videoId,		// YouTube Video ID
				playerVars: {
					autoplay: 1,		// Auto-play the video on load
					controls: 1,		// Show pause/play buttons in player
					showinfo: 0,		// Hide the video title
					modestbranding: 1,	// Hide the Youtube Logo
					loop: 1,			// Run the video in a loop
					fs: 0,				// Hide the full screen button
					cc_load_policy: 0,	// Hide closed captions
					iv_load_policy: 3,	// Hide the Video Annotations
					autohide: 0			// Hide video controls when playing
				},
				events: {
					onReady: function (e)
					{
						// mute the video after load
						e.target.mute();

						// play the video after load
						e.target.playVideo();
					},
					onStateChange: function (e)
					{
						if (e.data === YT.PlayerState.ENDED)
						{
							player.playVideo();
						}
					}
				}
			});
		}
		catch (ex) { console.error(ex); }
	});
}
