
jQuery( document ).ready(function($) {

$( document ).on( 'click', '.customize_multi_add_field', travel_insight_pro_customize_multi_add_field )
    .on( 'change', '.customize_multi_single_field', travel_insight_pro_customize_multi_single_field )
    .on( 'click', '.customize_multi_remove_field', travel_insight_pro_customize_multi_remove_field )



/********* Multi Input Custom control ***********/
$( '.customize_multi_input' ).each(function() {
    var $this = $( this );
    var multi_saved_value = $this.find( '.customize_multi_value_field' ).val();
    if (multi_saved_value.length > 0) {
        var multi_saved_values = multi_saved_value.split( "|" );
        $this.find( '.customize_multi_fields' ).empty();
        var $control = $this.parents( '.customize_multi_input' );
        $.each(multi_saved_values, function( index, value ) {
            $this.find( '.customize_multi_fields' ).append( '<div class="set"><input type="text" value="' + value + '" class="customize_multi_single_field" /><span class="customize_multi_remove_field"><span class="dashicons dashicons-no-alt"></span></span></div>' );
        });
    }
});

function travel_insight_pro_customize_multi_add_field(e) {
    var $this = $( e.currentTarget );
    e.preventDefault();
        var $control = $this.parents( '.customize_multi_input' );
        $control.find( '.customize_multi_fields' ).append( '<div class="set"><input type="text" value="" class="customize_multi_single_field" /><span class="customize_multi_remove_field"><span class="dashicons dashicons-no-alt"></span></span></div>' );
        travel_insight_pro_customize_multi_write( $control );
}

function travel_insight_pro_customize_multi_single_field() {
    var $control = $( this ).parents( '.customize_multi_input' );
    travel_insight_pro_customize_multi_write( $control );
}

function travel_insight_pro_customize_multi_remove_field(e) {
    e.preventDefault();
    var $this = $( this );
    var $control = $this.parents( '.customize_multi_input' );
    $this.parent().remove();
    travel_insight_pro_customize_multi_write( $control );
}

function travel_insight_pro_customize_multi_write( $element) {
    var customize_multi_val = '';
    $element.find( '.customize_multi_fields .customize_multi_single_field' ).each(function() {
        customize_multi_val += $( this ).val() + '|';
    });
    $element.find( '.customize_multi_value_field' ).val( customize_multi_val.slice( 0, -1 ) ).change();
}       


    // icon picker
    $('.travel-insight-pro-icon-picker').each( function() {
        $(this).iconpicker( '#' + this.id );
    } );

});

jQuery(document).ready(function($) {
    // Sortable sections
    jQuery( 'ul.travel-insight-pro-sortable-list' ).sortable({
        handle: '.travel-insight-pro-drag-handle',
        axis: 'y',
        update: function( e, ui ){
            jQuery('input.travel-insight-pro-sortable-input').trigger( 'change' );
        }
    });


    /* On changing the value. */
    jQuery( "body" ).on( 'change', 'input.travel-insight-pro-sortable-input', function() {
        /* Get the value, and convert to string. */
        this_checkboxes_values = jQuery( this ).parents( 'ul.travel-insight-pro-sortable-list' ).find( 'input.travel-insight-pro-sortable-input' ).map( function() {
            return this.value;
        }).get().join( ',' );

        /* Add the value to hidden input. */
        jQuery( this ).parents( 'ul.travel-insight-pro-sortable-list' ).find( 'input.travel-insight-pro-sortable-value' ).val( this_checkboxes_values ).trigger( 'change' );

    });
});
