jQuery(function ($){
'use strict';
var cloneIndex={
set: function ($clone, index){
$clone.find(':input[class|="iwjmb"]').each(function (){
var $field=$(this);
var name=$field.attr('name');
/*
if(name&&! $field.closest('.iwjmb-group-clone').length){
$field.attr('name', cloneIndex.replace(index, name, '[', ']', false));
}*/
if(name){
$field.attr('name', cloneIndex.replace(index, name, '[', ']', false));
}
var id=this.id;
if(id){
$field.attr('id', cloneIndex.replace(index, id, '_'));
}});
var $address=$clone.find('.iwjmb-map-goto-address-button');
if($address.length){
var value=$address.attr('value');
$address.attr('value', cloneIndex.replace(index, value, '_'));
}},
replace: function (index, value, before, after, alternative){
before=before||'';
after=after||'';
alternative=alternative||true;
var regex=new RegExp(cloneIndex.escapeRegex(before) + '(\\d+)' + cloneIndex.escapeRegex(after)),
newValue=before + index + after;
return regex.test(value) ? value.replace(regex, newValue):(alternative ? value + newValue:value);
},
escapeRegex: function (string){
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
},
nextIndex: function ($container){
var nextIndex=$container.data('next-index');
$container.data('next-index', nextIndex + 1);
return nextIndex;
}};
function clone($container){
var $last=$container.children('.iwjmb-clone:last'),
$clone=$last.clone(),
$input=$clone.find(':input[class|="iwjmb"]'),
nextIndex=cloneIndex.nextIndex($container);
$input.each(function (){
var $field=$(this);
if($field.is(':radio')||$field.is(':checkbox')){
$field.prop('checked', false);
}else if($field.is('select')){
$field.prop('selectedIndex', -1);
}else if(!$field.hasClass('iwjmb-hidden')){
$field.val('');
}
if($field.hasClass('iwjmb-date')){
var options=$field.data('options');
if(options.lang){
$.datetimepicker.setLocale(options.lang);
}else{
$.datetimepicker.setLocale(iwjmbDateTime.locale_short);
}
$field.datetimepicker(options);
}});
$clone.insertAfter($last);
$clone.trigger('clone_instance', nextIndex);
cloneIndex.set($clone, nextIndex);
$input.trigger('clone', nextIndex);
}
function toggleRemoveButtons($container){
var $clones=$container.children('.iwjmb-clone');
$clones.children('.remove-clone').toggle($clones.length > 1);
$container.find('.iwjmb-input').each(function (){
toggleRemoveButtons($(this));
});
}
function toggleAddButton($container){
var $button=$container.find('.add-clone'),
maxClone=parseInt($container.data('max-clone')),
numClone=$container.find('.iwjmb-clone').length;
$button.toggle(isNaN(maxClone)||(maxClone&&numClone < maxClone));
}
$('#wpbody-content, body.page')
.on('click', '.add-clone', function (e){
e.preventDefault();
var $container=$(this).closest('.iwjmb-input');
clone($container);
toggleRemoveButtons($container);
toggleAddButton($container);
})
.on('click', '.remove-clone', function (e){
e.preventDefault();
var $this=$(this),
$container=$this.closest('.iwjmb-input');
if($container.children('.iwjmb-clone').length < 2){
return;
}
$this.parent().trigger('remove').remove();
toggleRemoveButtons($container);
toggleAddButton($container)
});
$('.iwjmb-input').each(function (){
var $container=$(this);
toggleRemoveButtons($container);
toggleAddButton($container);
$container
.data('next-index', $container.children('.iwjmb-clone').length)
.sortable({
handle: '.iwjmb-clone-icon',
placeholder: ' iwjmb-clone iwjmb-clone-placeholder',
items: '.iwjmb-clone',
start: function (event, ui){
ui.placeholder.height(ui.item.height());
}});
});
});