function InputfieldRepeater(i){var b=50;var r=i("body").hasClass("AdminThemeDefault");var x=null;var c=function(B,C){if(typeof C!="undefined"){if(C=="InputfieldRepeaterItemEdit"||C=="InputfieldRepeaterItemAdd"){B.stopPropagation();var A=i(this).find(".InputfieldRepeater");if(A.length){A.each(function(){y(i(this))})}return}}y(i(this))};var k=function(G){var F=i(this);var D=F.closest(".InputfieldHeader");var C=D.parent();if(g(F)){return false}if(C.hasClass("InputfieldRepeaterNewItem")){var A=C.children(".InputfieldContent").children(".InputfieldRepeaterAddItem").children("input");A.attr("value",parseInt(A.attr("value")-1));C.remove()}else{var B=C.attr("data-page");var E=C.find("#delete_repeater"+B);if(E.is(":checked")){E.removeAttr("checked");D.removeClass("ui-state-error").addClass("ui-state-default");C.removeClass("InputfieldRepeaterDeletePending")}else{E.attr("checked","checked");D.removeClass("ui-state-default").addClass("ui-state-error");if(!C.hasClass("InputfieldStateCollapsed")){D.find(".toggle-icon").click()}C.addClass("InputfieldRepeaterDeletePending")}D.find(".InputfieldRepeaterItemControls").css("background-color",D.css("background-color"))}l(C.closest(".InputfieldRepeater"));G.stopPropagation()};var p=function(){var C=i(this);var D=i(this).closest("li");var A=D.hasClass("InputfieldRepeaterDeletePending");if(g(C)){return false}function B(){D.parent().children("li").each(function(){var E=i(this);var F=E.children(".InputfieldHeader").find(".InputfieldRepeaterTrash");if(E.hasClass("InputfieldRepeaterDeletePending")){if(A){F.click()}}else{if(!A){F.click()}}})}if(A){B()}else{ProcessWire.confirm(ProcessWire.config.InputfieldRepeater.labels.removeAll,B)}};var e=function(){var B=i(this);if(g(B)){return false}var A=B.closest(".InputfieldRepeaterItem");ProcessWire.confirm(ProcessWire.config.InputfieldRepeater.labels.clone,function(){var D=A.attr("data-page");var C=A.closest(".InputfieldRepeater").children(".InputfieldContent").children(".InputfieldRepeaterAddItem").find(".InputfieldRepeaterAddLink:eq(0)");C.attr("data-clone",D).click();i("html, body").animate({scrollTop:C.offset().top-100},250,"swing")});return false};var w=function(E){var D=i(this);var C=D.attr("data-on");var A=D.attr("data-off");var B=D.closest(".InputfieldRepeaterItem");var F=B.find(".InputfieldRepeaterPublish");if(x){clearTimeout(x)}x=setTimeout(function(){if(g(D)){return false}if(D.hasClass(C)){D.removeClass(C).addClass(A);B.addClass("InputfieldRepeaterUnpublished InputfieldRepeaterOff");F.val("-1")}else{D.removeClass(A).addClass(C);B.removeClass("InputfieldRepeaterUnpublished InputfieldRepeaterOff").addClass("InputfieldRepeaterWasUnpublished");F.val("1")}l(B.closest(".InputfieldRepeater"))},250);E.stopPropagation()};var f=function(){var A=i(this);var B=A.find(".InputfieldRepeaterLoaded");if(parseInt(B.val())>0){return}A.addClass("InputfieldRepeaterItemLoading")};var z=function(){var H=i(this);var K=H.find(".InputfieldRepeaterLoaded");d(H);if(parseInt(K.val())>0){t(H);return}K.val("1");var B=H.find(".InputfieldContent").hide();var D=H.closest(".InputfieldRepeater");var G=D.attr("data-page");var C=parseInt(H.attr("data-page"));var J=D.attr("id");var I=J.replace("wrap_Inputfield_","");var A=ProcessWire.config.InputfieldRepeater.editorUrl+"?id="+G+"&field="+I+"&repeater_edit="+C;var E=H.find(".InputfieldRepeaterDrag");var F=K.closest(".Inputfields");if(D.hasClass("InputfieldRenderValueMode")){A+="&inrvm=1"}if(D.hasClass("InputfieldNoDraft")){A+="&nodraft=1"}E.removeClass("fa-arrows").addClass("fa-spin fa-spinner");J=J.replace(/_repeater\d+$/,"");i.get(A,function(N){var L=i(N).find("#"+J+" > .InputfieldContent > .Inputfields > .InputfieldRepeaterItem > .InputfieldContent > .Inputfields > .InputfieldWrapper > .Inputfields > .Inputfield");F.append(L);H.removeClass("InputfieldRepeaterItemLoading");InputfieldsInit(F);var M=L.find(".InputfieldRepeater");if(M.length){M.each(function(){y(i(this))})}B.slideDown("fast",function(){E.removeClass("fa-spin fa-spinner").addClass("fa-arrows");t(H)});setTimeout(function(){F.find(".Inputfield").trigger("reloaded",["InputfieldRepeaterItemEdit"])},50);n(N)})};var j=function(){d(i(this))};var o=function(){var D=i(this);var J=D.parent("p").prev("ul.Inputfields");var B=D.closest(".InputfieldRepeater");var E=D.parent().children("input");var I=0;var O=D.attr("data-noajax").length==0;var C=D.attr("data-clone");function M(P){var Q=P.attr("id")+"_";while(i("#"+Q).length>0){Q+="_"}P.attr("id",Q);J.append(P);P.css("display","block");h(P,true);D.trigger("repeateradd",[P])}if(typeof C=="undefined"||!C){C=null}if(C){D.removeAttr("data-clone")}if(!O){var F=J.children(".InputfieldRepeaterNewItem");I=F.length;if(I>0){if(I>1){F=F.slice(0,1)}var H=F.clone(true);M(H);E.attr("value",I);l(B)}return false}var K=B.attr("data-page");var N=B.attr("id").replace("wrap_Inputfield_","");var G=D.parent().find(".InputfieldRepeaterSpinner");var A=ProcessWire.config.InputfieldRepeater.editorUrl+"?id="+K+"&field="+N;G.removeClass(G.attr("data-off")).addClass(G.attr("data-on"));if(C){A+="&repeater_clone="+C}else{A+="&repeater_add="+D.attr("data-type")}var L=J.find(".InputfieldRepeaterUnpublished, .InputfieldRepeaterWasUnpublished");if(L.length){A+="&repeater_not=";L.each(function(){A+=i(this).attr("data-page")+","})}i.get(A,function(R){G.removeClass(G.attr("data-on")).addClass(G.attr("data-off"));var P=i(R).find(".InputfieldRepeaterItemRequested");if(!P.length){return}M(P);P.wrap("<div />");InputfieldsInit(P.parent());y(P);P.unwrap();P.find(".Inputfield").trigger("reloaded",["InputfieldRepeaterItemAdd"]);P.find(".InputfieldRepeaterSort").val(J.children().length);i("html, body").animate({scrollTop:P.offset().top},500,"swing");d(P);l(B);t(P);var Q=P.find(".InputfieldRepeater");if(Q.length){Q.each(function(){y(i(this))})}n(R)});return false};var s=function(E){E.stopPropagation();E.preventDefault();if(x){clearTimeout(x)}if(i(this).closest(".InputfieldRepeater").hasClass("InputfieldRepeaterAccordion")){return false}var C=i(this).closest(".InputfieldRepeater");var F=C.children(".InputfieldContent").children(".Inputfields").children(".InputfieldRepeaterItem");if(!F.length){return false}var B=F.eq(0);if(B.hasClass("InputfieldStateCollapsed")){var D=ProcessWire.config.InputfieldRepeater.labels.openAll;var A=".InputfieldStateCollapsed"}else{var D=ProcessWire.config.InputfieldRepeater.labels.collapseAll;var A=".InputfieldRepeaterItem:not(.InputfieldStateCollapsed)"}ProcessWire.confirm(D,function(){F.filter(A).each(function(){i(this).children(".InputfieldHeader").find(".toggle-icon").click()})});return false};function g(A){if(A.hasClass("pw-icon-disabled")){ProcessWire.alert(ProcessWire.config.InputfieldRepeater.labels.disabledMinMax);return true}return false}function t(A){if(!A.closest(".InputfieldRepeater").hasClass("InputfieldRepeaterAccordion")){return false}var E=A.attr("id");var C=false;var B=A.parent().children(".InputfieldRepeaterItem");var D=false;var F=false;B.each(function(){var G=i(this);if(G.attr("id")==E){D=true;return}if(G.hasClass("InputfieldStateCollapsed")){return}if(!G.is(":visible")){return}if(!D){C=true}G.children(".InputfieldHeader").find(".toggle-icon").trigger("click",[{duration:0}]);F=true});if(C&&F){i("html, body").animate({scrollTop:A.offset().top-10},0)}return true}function h(C,E){var B;B=C.children(".InputfieldHeader").find(".InputfieldRepeaterItemLabel");if(typeof B=="undefined"){B=C.children("label")}var F=B.html();var A=F;if(typeof F!="undefined"){if(E&&F.indexOf("#")>-1){var D=C.siblings(".InputfieldRepeaterItem:visible").length+1;F=F.replace(/#[0-9]+/,"#"+D)}while(F.indexOf("}")>-1){F=F.replace(/\{/,'<span class="ui-priority-secondary" style="font-weight:normal">');F=F.replace(/}/,"</span>")}if(F!=A){B.html(F)}}}function m(C,G,B){var A=C.item.find(".InputfieldRepeaterDepth");var E=-1;var F=parseInt(A.val());var D=C.position.left;if(D<0){E=F-Math.round(Math.abs(D)/b)}else{E=Math.round(D/b)+F}if(E<1){E=0}else{if(E>G){E=G}}if(B){if(E){C.item.css("margin-left",(E*b)+"px")}else{C.item.css("margin-left",0)}A.val(E);C.item.children(".InputfieldHeader").removeClass("ui-state-error")}return E}function q(A){A.find(".InputfieldRepeaterDepth").each(function(){var D=i(this);var E=D.val();var B=D.closest(".InputfieldRepeaterItem");var C=B.css("margin-left");if(C=="auto"){C=0}C=parseInt(C);var F=E*b;if(F!=C){B.css("margin-left",F+"px")}});A.children(".InputfieldContent").css("position","relative")}function a(D,A){var C=parseInt(D.attr("data-depth"));var B={items:"> li:not(.InputfieldRepeaterNewItem)",handle:".InputfieldRepeaterDrag",start:function(F,E){E.item.find(".InputfieldHeader").addClass("ui-state-highlight");E.item.find("textarea.InputfieldCKEditorNormal.InputfieldCKEditorLoaded").each(function(){i(this).removeClass("InputfieldCKEditorLoaded");var G=CKEDITOR.instances[i(this).attr("id")];G.destroy();CKEDITOR.remove(i(this).attr("id"))});E.item.find(".InputfieldTinyMCE textarea").each(function(){tinyMCE.execCommand("mceRemoveControl",false,i(this).attr("id"))})},stop:function(F,E){if(C>0){m(E,C,true)}E.item.find(".InputfieldHeader").removeClass("ui-state-highlight");i(this).children().each(function(G){i(this).find(".InputfieldRepeaterSort").slice(0,1).attr("value",G)});E.item.find("textarea.InputfieldCKEditorNormal:not(.InputfieldCKEditorLoaded)").each(function(){i(this).closest(".InputfieldCKEditor").trigger("reloaded",["InputfieldRepeaterSort"])});E.item.find(".InputfieldTinyMCE textarea").each(function(){tinyMCE.execCommand("mceAddControl",false,i(this).attr("id"))})}};if(C>0){q(D);B.grid=[b,1];B.sort=function(F,G){var H=m(G,99,false);var E=G.item.children(".InputfieldHeader");if(H>C){E.addClass("ui-state-error")}else{if(E.hasClass("ui-state-error")){E.removeClass("ui-state-error")}}}}else{B.axis="y"}i(".InputfieldRepeaterDrag",A).hover(function(){i(this).parent("label").addClass("ui-state-focus")},function(){i(this).parent("label").removeClass("ui-state-focus")});A.sortable(B)}function v(F,H,E){var G=i("<i class='fa fa-copy InputfieldRepeaterClone'></i>").css("display","block");var B=i("<i class='fa fa-trash InputfieldRepeaterTrash'></i>");var D=i("<i class='fa InputfieldRepeaterToggle' data-on='fa-toggle-on' data-off='fa-toggle-off'></i>");var A=ProcessWire.config.InputfieldRepeater;var C=!H.hasClass("InputfieldRepeaterNoAjaxAdd");if(A){D.attr("title",A.labels.toggle);B.attr("title",A.labels.remove);G.attr("title",A.labels.clone)}F.each(function(){var O=i(this);if(O.hasClass("InputfieldRepeaterHeaderInit")){return}var N="fa-arrows";var L=O.parent();if(L.hasClass("InputfieldRepeaterNewItem")){N="fa-plus";O.addClass("ui-priority-secondary")}O.addClass("ui-state-default InputfieldRepeaterHeaderInit");O.prepend("<i class='fa fa-fw "+N+" InputfieldRepeaterDrag'></i>");if(!E){var I=i("<span class='InputfieldRepeaterItemControls'></span>");var K=D.clone(true).addClass(O.parent().hasClass("InputfieldRepeaterOff")?"fa-toggle-off":"fa-toggle-on");var M=B.clone(true);var J=O.find(".toggle-icon");I.prepend(J);if(C){I.prepend(G.clone(true))}I.prepend(K).prepend(M);O.prepend(I);I.css("background-color",O.css("background-color"))}h(L,false)})}function y(D){if(D.hasClass("InputfieldRepeaterItem")){var A=D;var E=D.closest(".InputfieldRepeater");var C=true}else{var A=D.find(".Inputfields:eq(0)");var E=D;var C=false}if(A.hasClass("InputfieldRepeaterInit")){return}var B=A.closest(".InputfieldRenderValueMode").length>0;A.addClass("InputfieldRepeaterInit");if(C){v(D.children(".InputfieldHeader"),E,B)}else{v(i(".InputfieldRepeaterItem > .InputfieldHeader",D),E,B)}if(B){q(E);return}i(".InputfieldRepeaterTrash",D).hover(function(){var F=i(this).closest("label");if(!F.parents().hasClass("InputfieldRepeaterDeletePending")){F.addClass("ui-state-error")}F.find(".InputfieldRepeaterItemControls").css("background-color",F.css("background-color"))},function(){var F=i(this).closest("label");if(!F.parent().hasClass("InputfieldRepeaterDeletePending")){F.removeClass("ui-state-error")}F.find(".InputfieldRepeaterItemControls").css("background-color",F.css("background-color"))});if(C){A=E.find(".Inputfields:eq(0)")}a(E,A);i(".InputfieldRepeaterAddLink:not(.InputfieldRepeaterAddLinkInit)",E).addClass("InputfieldRepeaterAddLinkInit").click(o);if(E.hasClass("InputfieldRepeaterMax")){l(E)}}function l(A){if(!A.hasClass("InputfieldRepeaterMax")&&!A.hasClass("InputfieldRepeaterMin")){return}var I=parseInt(A.attr("data-max"));var F=parseInt(A.attr("data-min"));if(I<=0&&F<=0){return}var B=A.children(".InputfieldContent");var G=B.children(".Inputfields").children("li:not(.InputfieldRepeaterDeletePending):not(.InputfieldRepeaterOff):visible").length;var E=B.children(".InputfieldRepeaterAddItem");var D="";var C="";if(I>0){if(G>=I){E.hide();D="hide"}else{if(!E.is(":visible")){E.show();D="show"}}}if(F>0){if(G<=F){C="hide";B.addClass("InputfieldRepeaterTrashHidden")}else{if(B.hasClass("InputfieldRepeaterTrashHidden")){B.removeClass("InputfieldRepeaterTrashHidden");C="show"}}}if(D.length||C.length){var H=B.children(".Inputfields").children(".InputfieldRepeaterItem");if(D.length){H.each(function(){var J=i(this).children(".InputfieldHeader").find(".InputfieldRepeaterClone");if(D==="show"){J.removeClass("pw-icon-disabled")}else{J.addClass("pw-icon-disabled")}})}if(C.length){H.each(function(){var K=i(this).children(".InputfieldHeader");var J=K.find(".InputfieldRepeaterTrash");var L=K.find(".InputfieldRepeaterToggle.fa-toggle-on");if(C==="show"){J.removeClass("pw-icon-disabled");L.removeClass("pw-icon-disabled")}else{J.addClass("pw-icon-disabled");L.addClass("pw-icon-disabled")}});if(C=="hide"){B.children(".Inputfields").children("li.InputfieldRepeaterDeletePending").each(function(){var J=i(this).children(".InputfieldHeader").find(".InputfieldRepeaterTrash");J.removeClass("pw-icon-disabled")})}}}}function n(B){if(B.indexOf("<\/script>")==-1){return}var C=document.createElement("div");C.innerHTML=B;var A=C.querySelectorAll(".Inputfield script");i(A).each(function(){i.globalEval(this.text||this.textContent||this.innerHTML||"")})}function d(A){if(A.closest(".InputfieldRepeaterRememberOpen").length<1){return}var B="";i(".InputfieldRepeaterItem:not(.InputfieldStateCollapsed)").each(function(){var C=parseInt(i(this).attr("data-page"));if(C>0){B+=C+"|"}});i.cookie("repeaters_open",B)}function u(){i(".InputfieldRepeater").each(function(){y(i(this))});i(document).on("reloaded",".InputfieldRepeater",c).on("click",".InputfieldRepeaterTrash",k).on("dblclick",".InputfieldRepeaterTrash",p).on("click",".InputfieldRepeaterClone",e).on("dblclick",".InputfieldRepeaterToggle",s).on("click",".InputfieldRepeaterToggle",w).on("opened",".InputfieldRepeaterItem",z).on("closed",".InputfieldRepeaterItem",j).on("openReady",".InputfieldRepeaterItem",f)}u()}jQuery(document).ready(function(a){InputfieldRepeater(a)});