!function(w){"use strict";w.subformRepeatable=function(e,t){if(this.$container=w(e),this.$container.data("subformRepeatable"))return a;this.$container.data("subformRepeatable",a),this.options=w.extend({},w.subformRepeatable.defaults,t),this.template="",this.prepareTemplate(),this.$containerRows=this.options.rowsContainer?this.$container.find(this.options.rowsContainer):this.$container,this.lastRowNum=this.$containerRows.find(this.options.repeatableElement).length;var a=this;this.$container.on("click",this.options.btAdd,function(e){e.preventDefault();var t=w(this).parents(a.options.repeatableElement);t.length||(t=null),a.addRow(t)}),this.$container.on("click",this.options.btRemove,function(e){e.preventDefault();var t=w(this).parents(a.options.repeatableElement);a.removeRow(t)}),this.options.btMove&&this.$containerRows.sortable({items:this.options.repeatableElement,handle:this.options.btMove,tolerance:"pointer"}),this.$container.trigger("subform-ready")},w.subformRepeatable.prototype.prepareTemplate=function(){if(this.options.rowTemplateSelector){var e=this.$container.find(this.options.rowTemplateSelector)[0]||{};this.template=w.trim(e.text||e.textContent)}else{var t=this.$container.find(this.options.repeatableElement).get(0),a=w(t).clone();try{this.clearScripts(a)}catch(e){window.console&&console.log(e)}this.template=a.prop("outerHTML")}},w.subformRepeatable.prototype.addRow=function(e){var t=this.$containerRows.find(this.options.repeatableElement).length;if(t>=this.options.maximum)return null;var a=w.parseHTML(this.template);e?w(e).after(a):this.$containerRows.append(a);var o=w(a);o.attr("data-new","true"),this.fixUniqueAttributes(o,t);try{this.fixScripts(o)}catch(e){window.console&&console.log(e)}return this.$container.trigger("subform-row-add",o),o},w.subformRepeatable.prototype.removeRow=function(e){this.$containerRows.find(this.options.repeatableElement).length<=this.options.minimum||(this.$container.trigger("subform-row-remove",e),e.remove())},w.subformRepeatable.prototype.fixUniqueAttributes=function(e,t){var a=e.attr("data-group"),o=e.attr("data-base-name"),r=(t=t||0,Math.max(this.lastRowNum,t)),i=o+r;this.lastRowNum=r+1,e.attr("data-group",i);for(var n=e.find("[name]"),s={},l=0,p=n.length;l<p;l++){var f=w(n[l]),c=f.attr("name"),h=c.replace(/(\[\]$)/g,"").replace(/(\]\[)/g,"__").replace(/\[/g,"_").replace(/\]/g,""),d=c.replace("["+a+"][","["+i+"]["),u=h.replace(a,i),m=0,b=h;"checkbox"===f.prop("type")&&c.match(/\[\]$/)?((m=s[h]?s[h].length:0)||(f.closest("fieldset.checkboxes").attr("id",u),e.find('label[for="'+h+'"]').attr("for",u).attr("id",u+"-lbl")),b+=m,u+=m):"radio"===f.prop("type")&&((m=s[h]?s[h].length:0)||(f.closest("fieldset.radio").attr("id",u),e.find('label[for="'+h+'"]').attr("for",u).attr("id",u+"-lbl")),b+=m,u+=m),s[h]?s[h].push(!0):s[h]=[!0],f.attr("name",d),f.attr("id",u),e.find('label[for="'+b+'"]').attr("for",u).attr("id",u+"-lbl")}},w.subformRepeatable.prototype.clearScripts=function(e){w.fn.chosen&&e.find("select.chzn-done").each(function(){var e=w(this);e.next(".chzn-container").remove(),e.show().addClass("fix-chosen")})},w.subformRepeatable.prototype.fixScripts=function(e){e.find('a[onclick*="jInsertFieldValue"]').each(function(){var e=w(this),t=e.siblings('input[type="text"]').attr("id"),a=e.prev(),o=a.attr("href");e.attr("onclick","jInsertFieldValue('', '"+t+"');return false;"),a.attr("href",o.replace(/&fieldid=(.+)&/,"&fieldid="+t+"&"))}),w.fn.fieldMedia&&e.find(".field-media-wrapper").fieldMedia(),w.fn.fieldUser&&e.find(".field-user-wrapper").fieldUser(),window.SqueezeBox&&window.SqueezeBox.assign&&SqueezeBox.assign(e.find("a.modal").get(),{parse:"rel"})},w.subformRepeatable.defaults={btAdd:".group-add",btRemove:".group-remove",btMove:".group-move",minimum:0,maximum:10,repeatableElement:".subform-repeatable-group",rowTemplateSelector:"script.subform-repeatable-template-section",rowsContainer:null},w.fn.subformRepeatable=function(e){return this.each(function(){var e=e||{},t=w(this).data();if(!t.subformRepeatable){for(var a in t)t.hasOwnProperty(a)&&(e[a]=t[a]);var o=new w.subformRepeatable(this,e);w(this).data("subformRepeatable",o)}})},w(window).on("load",function(){w("div.subform-repeatable").subformRepeatable()})}(jQuery);