(function(window,settings){var NativeHandler,YouTubeHandler;window.wp=window.wp||{};if(!('addEventListener'in window)){return;}
function trigger(target,name){var evt;if('function'===typeof window.Event){evt=new Event(name);}else{evt=document.createEvent('Event');evt.initEvent(name,true,true);}
target.dispatchEvent(evt);}
function CustomHeader(){this.handlers={nativeVideo:new NativeHandler(),youtube:new YouTubeHandler()};}
CustomHeader.prototype={initialize:function(){if(this.supportsVideo()){for(var id in this.handlers){var handler=this.handlers[id];if('test'in handler&&handler.test(settings)){this.activeHandler=handler.initialize.call(handler,settings);trigger(document,'wp-custom-header-video-loaded');break;}}}},supportsVideo:function(){if(window.innerWidth<settings.minWidth||window.innerHeight<settings.minHeight){return false;}
return true;},BaseVideoHandler:BaseHandler};function BaseHandler(){}
BaseHandler.prototype={initialize:function(settings){var handler=this,button=document.createElement('button');this.settings=settings;this.container=document.getElementById('wp-custom-header');this.button=button;button.setAttribute('type','button');button.setAttribute('id','wp-custom-header-video-button');button.setAttribute('class','wp-custom-header-video-button wp-custom-header-video-play');button.innerHTML=settings.l10n.play;button.addEventListener('click',function(){if(handler.isPaused()){handler.play();}else{handler.pause();}});this.container.addEventListener('play',function(){button.className='wp-custom-header-video-button wp-custom-header-video-play';button.innerHTML=settings.l10n.pause;if('a11y'in window.wp){window.wp.a11y.speak(settings.l10n.playSpeak);}});this.container.addEventListener('pause',function(){button.className='wp-custom-header-video-button wp-custom-header-video-pause';button.innerHTML=settings.l10n.play;if('a11y'in window.wp){window.wp.a11y.speak(settings.l10n.pauseSpeak);}});this.ready();},ready:function(){},isPaused:function(){},pause:function(){},play:function(){},setVideo:function(node){var editShortcutNode,editShortcut=this.container.getElementsByClassName('customize-partial-edit-shortcut');if(editShortcut.length){editShortcutNode=this.container.removeChild(editShortcut[0]);}
this.container.innerHTML='';this.container.appendChild(node);if(editShortcutNode){this.container.appendChild(editShortcutNode);}},showControls:function(){if(!this.container.contains(this.button)){this.container.appendChild(this.button);}},test:function(){return false;},trigger:function(name){trigger(this.container,name);}};BaseHandler.extend=function(protoProps){var prop;function CustomHandler(){var result=BaseHandler.apply(this,arguments);return result;}
CustomHandler.prototype=Object.create(BaseHandler.prototype);CustomHandler.prototype.constructor=CustomHandler;for(prop in protoProps){CustomHandler.prototype[prop]=protoProps[prop];}
return CustomHandler;};NativeHandler=BaseHandler.extend({test:function(settings){var video=document.createElement('video');return video.canPlayType(settings.mimeType);},ready:function(){var handler=this,video=document.createElement('video');video.id='wp-custom-header-video';video.autoplay=true;video.loop=true;video.muted=true;video.playsInline=true;video.width=this.settings.width;video.height=this.settings.height;video.addEventListener('play',function(){handler.trigger('play');});video.addEventListener('pause',function(){handler.trigger('pause');});video.addEventListener('canplay',function(){handler.showControls();});this.video=video;handler.setVideo(video);video.src=this.settings.videoUrl;},isPaused:function(){return this.video.paused;},pause:function(){this.video.pause();},play:function(){this.video.play();}});YouTubeHandler=BaseHandler.extend({test:function(settings){return'video/x-youtube'===settings.mimeType;},ready:function(){var handler=this;if('YT'in window){YT.ready(handler.loadVideo.bind(handler));}else{var tag=document.createElement('script');tag.src='https://www.youtube.com/iframe_api';tag.onload=function(){YT.ready(handler.loadVideo.bind(handler));};document.getElementsByTagName('head')[0].appendChild(tag);}},loadVideo:function(){var handler=this,video=document.createElement('div'),VIDEO_ID_REGEX=/^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;video.id='wp-custom-header-video';handler.setVideo(video);handler.player=new YT.Player(video,{height:this.settings.height,width:this.settings.width,videoId:this.settings.videoUrl.match(VIDEO_ID_REGEX)[1],events:{onReady:function(e){e.target.mute();handler.showControls();},onStateChange:function(e){if(YT.PlayerState.PLAYING===e.data){handler.trigger('play');}else if(YT.PlayerState.PAUSED===e.data){handler.trigger('pause');}else if(YT.PlayerState.ENDED===e.data){e.target.playVideo();}}},playerVars:{autoplay:1,controls:0,disablekb:1,fs:0,iv_load_policy:3,loop:1,modestbranding:1,playsinline:1,rel:0,showinfo:0}});},isPaused:function(){return YT.PlayerState.PAUSED===this.player.getPlayerState();},pause:function(){this.player.pauseVideo();},play:function(){this.player.playVideo();}});window.wp.customHeader=new CustomHeader();document.addEventListener('DOMContentLoaded',window.wp.customHeader.initialize.bind(window.wp.customHeader),false);if('customize'in window.wp){window.wp.customize.selectiveRefresh.bind('render-partials-response',function(response){if('custom_header_settings'in response){settings=response.custom_header_settings;}});window.wp.customize.selectiveRefresh.bind('partial-content-rendered',function(placement){if('custom_header'===placement.partial.id){window.wp.customHeader.initialize();}});}})(window,window._wpCustomHeaderSettings||{});