var e,t=t||{};Element.prototype.replaceWith||(Element.prototype.replaceWith=ReplaceWithPolyfill),CharacterData.prototype.replaceWith||(CharacterData.prototype.replaceWith=ReplaceWithPolyfill),DocumentType.prototype.replaceWith||(DocumentType.prototype.replaceWith=ReplaceWithPolyfill),e=H5P.jQuery,t.createTable=function(t){var a=e('<table class="h5p-admin-table'+(void 0!==H5PAdminIntegration.extraTableClasses?" "+H5PAdminIntegration.extraTableClasses:"")+'"></table>');if(t){var n=e("<thead></thead>"),o=e("<tr></tr>");e.each(t,(function(t,a){a instanceof Object||(a={html:a}),e("<th/>",a).appendTo(o)})),a.append(n.append(o))}return a},t.createTableRow=function(t){var a=e("<tr></tr>");return e.each(t,(function(t,n){n instanceof Object||(n={html:n}),e("<td/>",n).appendTo(a)})),a},t.createLabeledField=function(t,a){var n=e('<div class="h5p-labeled-field"></div>');return n.append('<div class="h5p-label">'+t+"</div>"),n.append('<div class="h5p-value">'+a+"</div>"),n},t.translateReplace=function(t,a){return e.each(a,(function(e,a){t=t.replace(new RegExp("\\"+e,"g"),a)})),t},t.throbber=function(t){return e("<div/>",{class:"h5p-throbber",text:t})},t.getRebuildCache=function(t){var a=e('<div class="h5p-admin-rebuild-cache"><p class="message">'+t.message+'</p><p class="progress">'+t.progress+"</p></div>"),n=e("<button>"+t.button+"</button>").appendTo(a).click((function(){var o=e("<div/>",{class:"h5p-spinner"}).replaceAll(n),r=["|","/","-","\\"],i=0,l=setInterval((function(){o.text(r[i]),++i===r.length&&(i=0)}),100),p=a.find(".progress"),build=function(){e.post(t.url,(function(e){if("0"===e)clearInterval(l),a.remove(),location.reload();else{var t=p.text().split(" ");t[0]=e,p.text(t.join(" ")),build()}}))};build()}));return a},t.Table=function(t){var a,n,o,r,i,l={};void 0!==t.table&&(l.class=t.table);var p=e("<table/>",l),c=e("<thead/>").appendTo(p),s=e("<tfoot/>").appendTo(p),d=e("<tbody/>").appendTo(p),addCol=function(t,a,l){var p={on:{}};a instanceof Object?(void 0!==a.text&&(p.text=a.text),void 0!==a.class&&(p.class=a.class),void 0!==n&&!0===a.sortable&&(p.role="button",p.tabIndex=0,void 0===r&&(r=l,i=0),r===l&&(p.class="h5p-sort",1===i&&(p.class+=" h5p-reverse")),p.on.click=function(){sort(c,l)},p.on.keypress=function(e){32===(e.charCode||e.keyCode)&&sort(c,l)})):p.text=a;var c=e("<th>",p).appendTo(t);r===l&&(o=c)},sort=function(e,t){t===r?0===i?(i=1,e.addClass("h5p-reverse")):(i=0,e.removeClass("h5p-reverse")):(o.removeClass("h5p-sort").removeClass("h5p-reverse"),o=e.addClass("h5p-sort"),r=t,i=0),n({by:r,dir:i})};this.setHeaders=function(t,o,l){a=t.length,n=o,l&&(r=l.by,i=l.dir);for(var p=e("<thead/>"),s=e("<tr/>").appendTo(p),d=0;d<t.length;d++)addCol(s,t[d],d);c.replaceWith(p),c=p},this.setRows=function(t){for(var a=e("<tbody/>"),n=0;n<t.length;n++)for(var o=e("<tr/>").appendTo(a),r=0;r<t[n].length;r++)e("<td>",{html:t[n][r]}).appendTo(o);return d.replaceWith(a),d=a},this.setBody=function(t){var n=e("<tbody/>"),o=e("<tr/>").appendTo(n);e("<td>",{colspan:a}).append(t).appendTo(o),d.replaceWith(n),d=n},this.setFoot=function(t){var n=e("<tfoot/>"),o=e("<tr/>").appendTo(n);e("<td>",{colspan:a}).append(t).appendTo(o),s.replaceWith(n)},this.appendTo=function(e){p.appendTo(e)}},t.Pagination=function(t,a,n,o){var r=0,i=Math.ceil(t/a),l=e("<button/>",{html:"&lt;",class:"button",title:o.previousPage}).click((function(){goTo(r-1)})),p=e("<span/>").click((function(){c.width(p.width()).show().val(r+1).focus(),p.hide()})),c=e("<input/>",{type:"number",min:1,max:i,on:{blur:function(){gotInput()},keyup:function(e){if(13===e.keyCode)return gotInput(),!1}}}).hide(),s=e("<button/>",{html:"&gt;",class:"button",title:o.nextPage}).click((function(){goTo(r+1)})),gotInput=function(){var e=parseInt(c.hide().val());isNaN(e)||goTo(e-1),p.show()},updateUI=function(){var e=r+1;l.attr("disabled",0===r),s.attr("disabled",e===i),p.html(o.currentPage.replace("$current",e).replace("$total",i))},goTo=function(e){e===r||e<0||e>=i||(r=e,updateUI(),n(e*a))};this.update=function(e,n){if(e!==t||n!==a){if(t=e,a=n,i=Math.ceil(t/a),c.attr("max",i),r>=i)return void goTo(i-1);updateUI()}},this.appendTo=function(e){l.add(p).add(c).add(s).appendTo(e)},updateUI()};