(function(e){e.JRepeatable=function(t,n,r,i){var r=null,s=null,o=null,u,n,a,f=e(t+"_button"),l=false,c=null,h=e("<div>");h.css({"background-color":"#000",opacity:.4,"z-index":9998,position:"fixed",left:0,top:0,height:"100%",width:"100%"}).hide();h.appendTo("body");openWindow=function(){if(!s){makeWin();o.prependTo(s)}o.show();s.show();resizeWin();h.show();a=getTrs().clone()};makeWin=function(){if(s){return}s=e("<div/>");s.css({padding:"5px","background-color":"#fff",display:"none","z-index":9999,position:"absolute",left:"50%",top:e(document).scrollTop()+e(window).height()/2-s.outerHeight()/2});s.appendTo("body");var t=e('<button class="btn button btn-primary"/>').text(Joomla.JText._("JAPPLY"));t.on("click",function(e){e.stopPropagation();store();c.find("table").replaceWith(o);close()});var n=e('<button class="btn button btn-link"/>').text(Joomla.JText._("JCANCEL"));n.on("click",function(t){l=true;t.stopPropagation();e(o).find("tbody tr").replaceWith(a);c.find("table").replaceWith(o);close();s=null});var r=e('<div class="controls form-actions"/>').css({"text-align":"right","margin-bottom":0}).append([n,t]);s.append(o);s.append(r);if(!l){build()}watchButtons()};resizeWin=function(){var e=-1*(s.width()/2);s.css({"margin-left":e})};close=function(){o.hide();s.hide();h.hide()};getRadioValues=function(){var t=[],n;e.each(getTrs(),function(n,r){var i=e(r).find('input[type="radio"]:checked');var s=i.length>0?i.val():s="";t.push(s)});return t};setRadioValues=function(t){e.each(getTrs(),function(n,r){var i=e(r).find('input[type="radio"][value="'+t[n]+'"]');if(i.length>0){i.attr("checked","checked")}})};watchButtons=function(){s.on("click","a.add",function(n){if(tr=findTr(n)){var r=document.getElementById(t+"_table").getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;if(r==i){return false}var s=getRadioValues();var o=e(tr).closest("table").find("tbody");var a=u.clone(true,true);a.appendTo(o);if(r==i-1){e(".add").removeClass("btn-success").addClass("disabled")}renameInputRow(a,r,true);setRadioValues(s);resizeWin();resetChosen(a)}return false}.bind(this));s.on("click","a.remove",function(n){if(tr=findTr(n)){tr.remove();var r=document.getElementById(t+"_table").getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;if(e(".add").hasClass("disabled")){e(".add").removeClass("disabled").addClass("btn-success")}}resizeWin();return false}.bind(this))};resetChosen=function(t){t.find("select").removeClass("chzn-done").show();e.each(t.find("select"),function(e,t){t.id=t.id+"_"+(Math.random()*1e7).toInt()});t.find(".chzn-container").remove();e("select").chosen({disable_search_threshold:10,allow_single_deselect:true})};getTrs=function(){return s.find("tbody tr")};renameInputs=function(){var e,t,n,r,i,s=getTrs();for(r=0;r<s.length;r++){renameInputRow(s[r],r,false)}};renameInputRow=function(t,n,r){var i=/\[[0-9]\]/;var s=/\[\]/;chx=e(t).find('input[type="radio"], input[type="checkbox"]');e.each(chx,function(t,o){if(r){o.name=o.name.replace(i,"["+n+"]")}else{if(o.name.match(s)===null){o.name+="["+n+"]";shortName=o.name.split("][");shortName=shortName[shortName.length-2]}else{o.name=o.name.replace(s,"["+n+"]");o.name+="[]";shortName=o.name.split("][");shortName=shortName[shortName.length-3]}}id=o.id.split("_");if(id.length===4&&r){id[id.length-2]=shortName+t;id[id.length-1]=n}else{id[id.length-1]=shortName+t;id.push(n)}o.id=id.join("_");label=e(this).next("label");label.attr("for",o.id)})};build=function(){var t,n,i,o,a,f,l,c,h;n=JSON.decode(e(r).val());if(typeOf(n)==="null"){n={}}i=s.find("tbody tr");o=Object.keys(n);a=o.length===0||n[o[0]].length===0?true:false;f=a?1:n[o[0]].length;for(var p=1;p<f;p++){t=i.clone();t.insertAfter(i);resetChosen(t)}renameInputs();l=getTrs();for(p=0;p<f;p++){e.each(o,function(t,r){e(l[p]).find('*[name*="'+this+'"]').each(function(t,i){c=e(i).attr("type");if(c==="radio"||c==="checkbox"){h=typeof n[r][p]==="object"?n[r][p]:[];if(h.contains(i.value)){e(i).attr("checked","checked")}}else{e(i).val(n[r][p]);if(e(i).prop("tagName")==="SELECT"){e(i).trigger("liszt:updated")}}})})}u=i;if(a){i.remove()}};findTr=function(e){var t=e.target.getParents().filter(function(e){return e.get("tag")==="tr"});return t.length===0?false:t[0]};store=function(){var t,i,s,u,a={},f=/\[[0-9]\]/;for(t=0;t<n.length;t++){i=n[t];s=o.find('*[name*="'+i+'"]');a[i]=[];e.each(s,function(t,n){u=e(this).attr("type");if(u==="radio"||u==="checkbox"){var r=parseInt(n.name.match(f)[0].replace("[","").replace("]",""));if(typeof a[i][r]==="undefined"){a[i][r]=[]}if(e(this).attr("checked")==="checked"){a[i][r].push(e(this).val())}}else{a[i].push(e(this).val())}})}r.val(JSON.encode(a));return true};e(document).on("click",'*[data-modal="'+t+'"]',function(t,n){r=e(this).next("input");c=e(this).closest("div.control-group");if(!o){o=c.find("table")}openWindow();return false})}})(jQuery)