'use strict';

!function($) {
	
	$('.new_txt_slide_button').on( 'click', function () {
		var next_i = $(this).prev('.text_slides').length + 1;
		var new_txt_slide_html = '<li><input name="txt'+next_i+'" class="text_slide_field" type="text" value="" /><i class="fa fa-sort reorder"></i><a href="#" class="remove"><i class="fa fa-times-circle"></i></a></li>';
		$(this).prev('.text_slides').append(new_txt_slide_html);
	});
	
	$('.text_slides').on( 'change','input.text_slide_field', function () {
		var $param_wrapper = $(this).closest('.text_slides_block');
		var $param_target = $param_wrapper.find('.wpb_vc_param_value');
		var sliders_serialized = JSON.stringify( $param_wrapper.find( 'ul.text_slides .text_slide_field' ).serializeArray() );
		$param_target.val( base64_encode( sliders_serialized ) );
	});
	
	$('.text_slides').on( 'click','li a.remove', function () { 
		$(this).closest('li').fadeOut( function () {
			$(this).remove();
			$(".text_slides li input.text_slide_field").eq(0).trigger( "change" );
		});
	});

	$( ".text_slides_block .text_slides" ).sortable({
		update: function( event, ui ) {
			$(".text_slides li input.text_slide_field").eq(0).trigger( "change" );
		}
	});
	
}(window.jQuery);