var e,t=t||{};e=H5P.jQuery,t.PAGER_SIZE=20,t.init=function(){t.$adminContainer=H5P.jQuery(H5PAdminIntegration.containerSelector),t.library=H5PAdminIntegration.libraryInfo,t.currentContent=t.library.content,t.currentPage=0,t.currentFilter="",t.filterCache=[],t.$adminContainer.append(t.createLibraryInfo()),t.$adminContainer.append(t.createContentElement())},t.createLibraryInfo=function(){var n=e('<div class="h5p-library-info"></div>');return e.each(t.library.info,(function(e,t){n.append(H5PUtils.createLabeledField(e,t))})),n},t.createContentElement=function(){return void 0!==t.library.notCached?H5PUtils.getRebuildCache(t.library.notCached):void 0!==t.currentContent?(t.$content=e('<div class="h5p-content"><h3>'+t.library.translations.contentHeader+"</h3></div>"),t.createSearchElement(),t.createPageSizeSelector(),t.createContentTable(),t.createPagerElement(),t.$content):void(t.$content=e('<div class="h5p-content empty">'+t.library.translations.noContent+"</div>"))},t.createContentTable=function(){t.$contentTable&&t.$contentTable.remove(),t.$contentTable=H5PUtils.createTable();var e=t.currentPage*t.PAGER_SIZE,n=e+t.PAGER_SIZE;for(n>t.currentContent.length&&(n=t.currentContent.length);e<n;e++){var a=t.currentContent[e];t.$contentTable.append(H5PUtils.createTableRow(['<a href="'+a.url+'">'+a.title+"</a>"]))}t.$contentTable.insertAfter(t.$search)},t.createPagerElement=function(){t.$previous=e('<button type="button" class="previous h5p-admin"><</button>'),t.$next=e('<button type="button" class="next h5p-admin">></button>'),t.$previous.on("click",(function(){t.$previous.hasClass("disabled")||(t.currentPage--,t.updatePager(),t.createContentTable())})),t.$next.on("click",(function(){t.$next.hasClass("disabled")||(t.currentPage++,t.updatePager(),t.createContentTable())})),t.$pagerInfo=e('<span class="pager-info"></span>'),t.$pager=e('<div class="h5p-content-pager"></div>').append(t.$previous,t.$pagerInfo,t.$next),t.$content.append(t.$pager),t.$pagerInfo.on("click",(function(){var n=t.$pagerInfo.innerWidth();t.$pagerInfo.hide();var pageNumerUpdated=function(){var e=a.val()-1;r.remove(),t.$pagerInfo.css({display:"inline-block"}),/^\d+$/.test(e)&&e>=0&&e<t.getNumPages()&&e!=t.currentPage&&(t.currentPage=e,t.updatePager(),t.createContentTable())},a=e("<input/>",{type:"number",min:1,max:t.getNumPages(),on:{blur:pageNumerUpdated,keyup:function(e){13===e.keyCode&&pageNumerUpdated()}}}).css({width:n}),r=e("<span/>",{class:"h5p-pager-goto"}).css({width:n}).append(a).insertAfter(t.$pagerInfo);a.focus()})),t.updatePager()},t.getNumPages=function(){return Math.ceil(t.currentContent.length/t.PAGER_SIZE)},t.updatePager=function(){if(t.$pagerInfo.css({display:"inline-block"}),t.getNumPages()>0){var e=H5PUtils.translateReplace(t.library.translations.pageXOfY,{$x:t.currentPage+1,$y:t.getNumPages()});t.$pagerInfo.html(e)}else t.$pagerInfo.html("");t.$previous.toggleClass("disabled",t.currentPage<=0),t.$next.toggleClass("disabled",t.currentContent.length<(t.currentPage+1)*t.PAGER_SIZE)},t.createSearchElement=function(){var n;t.$search=e('<div class="h5p-content-search"><input placeholder="'+t.library.translations.filterPlaceholder+'" type="search"></div>'),e("input",t.$search).on("change keypress paste input",(function(){n&&clearTimeout(n),n=setTimeout((function(){!function(){var n=e(".h5p-content-search > input").val();if(t.currentFilter!==n){if(0===n.trim().length)t.currentContent=t.library.content;else if(t.filterCache[n])t.currentContent=t.filterCache[n];else{var a=t.library.content;n.length>1&&t.currentFilter===n.substr(0,t.currentFilter.length)&&(a=t.currentContent),t.currentContent=e.grep(a,(function(e){return e.title&&e.title.match(new RegExp(n,"i"))}))}t.currentFilter=n,t.filterCache[n]=t.currentContent,t.currentPage=0,t.createContentTable(),t.$searchResults&&t.$searchResults.remove(),n.trim().length>0&&(t.$searchResults=e('<span class="h5p-admin-search-results">'+t.currentContent.length+" hits on "+t.currentFilter+"</span>"),t.$search.append(t.$searchResults)),t.updatePager()}}()}),500)})),t.$content.append(t.$search)},t.createPageSizeSelector=function(){t.$search.append('<div class="h5p-admin-pager-size-selector">'+t.library.translations.pageSizeSelectorLabel+':<span data-page-size="10">10</span><span class="selected" data-page-size="20">20</span><span data-page-size="50">50</span><span data-page-size="100">100</span><span data-page-size="200">200</span></div>'),e(".h5p-admin-pager-size-selector > span",t.$search).on("click",(function(){t.PAGER_SIZE=e(this).data("page-size"),e(".h5p-admin-pager-size-selector > span",t.$search).removeClass("selected"),e(this).addClass("selected"),t.currentPage=0,t.createContentTable(),t.updatePager()}))},e(document).ready((function(){t.initialized||(t.initialized=!0,t.init())}));