!function(e){"use strict";class i extends HTMLElement{constructor(){super(),this.$el=null,this.$selectedVideo=null,this.$searchForm=null,this.$pagination=null,this.$nextButton=null,this.player=null,this._params={},this._selectedVideoId="",this._paginationType="none",this._pageTopOffset=parseInt(ayg_config.top_offset)}connectedCallback(){this.$el=e(this),this.$selectedVideo=this.$el.find(".ayg-video").eq(0),this.$searchForm=this.$el.find("ayg-search-form"),this.$pagination=this.$el.find(".ayg-pagination"),this.player=this.$el.find("ayg-player").get(0),this._params=this.$el.data("params"),this._selectedVideoId=this.$selectedVideo.find(".ayg-thumbnail").data("id"),this.$pagination.length>0&&(this.$nextButton=this.$pagination.find(".ayg-pagination-next-btn"),this._paginationType=this.$nextButton.data("type")),this.$el.on("click",".ayg-video",e=>this._onThumbnailClicked(e)),this.$searchForm.on("videos.updated",e=>this._onSearch(e)),this.$pagination.on("videos.updated",e=>this._onVideosUpdated(e)),this.player.addEventListener("ended",()=>this._onVideoEnded())}disconnectedCallback(){this.$el.off("click",".ayg-video",e=>this._onThumbnailClicked(e)),this.$searchForm.off("videos.updated",e=>this._onSearch(e)),this.$pagination.off("videos.updated",e=>this._onVideosUpdated(e)),this.player.removeEventListener("ended",()=>this._onVideoEnded())}_onThumbnailClicked(i){if(this.$selectedVideo=e(i.currentTarget),this.$selectedVideo.hasClass("ayg-active"))return!1;this.$el.find(".ayg-active").removeClass("ayg-active"),this.$selectedVideo.addClass("ayg-active"),this._selectedVideoId=this.$selectedVideo.find(".ayg-thumbnail").data("id");let t=this.$selectedVideo.find(".ayg-thumbnail").data("title"),s=this.$selectedVideo.find(".ayg-thumbnail-description").html(),a=this.$selectedVideo.find(".ayg-thumbnail-image").attr("src");this.player.change({id:this._selectedVideoId,title:t,poster:a}),1==this._params.player_title&&this.$el.find(".ayg-player-title").html(t),1==this._params.player_description&&this.$el.find(".ayg-player-description").html(s),this._pageTopOffset>=0&&e("html, body").animate({scrollTop:this.$el.offset().top-this._pageTopOffset},500);let d=this.$selectedVideo.find(".ayg-thumbnail").data("url");""!=d&&window.history.replaceState(null,null,d),1==this._params.autoadvance&&"more"==this._paginationType&&this.$selectedVideo.is(":last-child")&&this.$nextButton.is(":visible")&&this.$nextButton.trigger("click")}_onSearch(e){this.$selectedVideo=this.$el.find(".ayg-video").eq(0).addClass("ayg-active")}_onVideosUpdated(e){if(this.$el.find(".ayg-active").length>0)return!1;this.$el.find(".ayg-video-"+this._selectedVideoId).length>0&&(this.$selectedVideo=this.$el.find(".ayg-video-"+this._selectedVideoId).addClass("ayg-active"))}_onVideoEnded(){if(1==this._params.autoadvance){if(this.player.stop(),this.$selectedVideo.is(":visible")){if(this.$selectedVideo.is(":last-child")){if("more"==this._paginationType||"none"==this._paginationType)1==this._params.loop&&this.$el.find(".ayg-video").eq(0).trigger("click");else if(this.$nextButton.is(":visible")){this.$nextButton.trigger("click");let e=setInterval(()=>{0==this.$el.find(".ayg-pagination.ayg-loading").length&&(clearInterval(e),this.$el.find(".ayg-video").eq(0).trigger("click"))},1e3)}}else this.$selectedVideo.next(".ayg-video").trigger("click")}else this.$el.find(".ayg-video").eq(0).trigger("click")}else 1==this._params.loop?this.player.play():this.player.stop()}}e(function(){customElements.get("ayg-theme-classic")||customElements.define("ayg-theme-classic",i)})}(jQuery);