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

