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