$( document ).ready(function($) {
    
    $(document).on('click', '.open_external_embed_code', function() {
        const objectid = $(this).attr('data-objectid');

        let string1 = '';
        string1 += "<script type='text/javascript' src='" + af2_formulare_object.strings.plugin_dir_url + "res/external/af2_iframe_data.js'";
        string1 += " data-url='" + af2_formulare_object.strings.site_url + "'";
        string1 += " data-content-id='" + objectid + "'";
        string1 += " data-maps-key='AIzaSyBndbQcPBJHZyoqmdgexoTStZUk53dHRNw'>";
        string1 += "</script>";

        let string2 = '';
        string2 += "<script type='text/javascript' src='" + af2_formulare_object.strings.plugin_dir_url + "res/external/af2_iframe_data_alternate.js'";
        string2 += " data-url='" + af2_formulare_object.strings.site_url + "'";
        string2 += " data-content-id='" + objectid + "'";
        string2 += " data-maps-key='AIzaSyBndbQcPBJHZyoqmdgexoTStZUk53dHRNw'>";
        string2 += "</script>";

        $('#af2_external_modal .af2_modal_content #af2_external_script_1').html(string1);
        $('#af2_external_modal .af2_modal_content #af2_external_script_2').html(string2);

        af2_open_modal('#af2_external_modal');
    });

    $(document).on('click', '.open_popup_embed_code', function() {
        af2_open_modal('#af2_popup_modal');

        $('#af2_popup_modal_ .af2_modal_content #af2_popup_radio_chooser').attr('id', 'af2_popup_radio_chooser_');

        $('#af2_popup_modal_ .af2_modal_content #af2_popup_delay').attr('id', 'af2_popup_delay_');
        $('#af2_popup_modal_ .af2_modal_content label[for="af2_popup_delay"]').attr('for', 'af2_popup_delay_');
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_click').attr('id', 'af2_popup_click_');
        $('#af2_popup_modal_ .af2_modal_content label[for="af2_popup_click"]').attr('for', 'af2_popup_click_');

        $('#af2_popup_modal_ .af2_modal_content #af2_popup_delay_').attr('value', 'af2_popup_choose_div_delay_');
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_delay_').attr('data-hide', 'af2_popup_choose_div_click_');
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_click_').attr('value', 'af2_popup_choose_div_click_');
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_click_').attr('data-hide', 'af2_popup_choose_div_delay_');



        $('#af2_popup_modal_ .af2_popup_choose_div_delay').addClass('af2_popup_choose_div_delay_');
        $('#af2_popup_modal_ .af2_popup_choose_div_delay').removeClass('af2_popup_choose_div_delay');
        $('#af2_popup_modal_ .af2_popup_choose_div_click').addClass('af2_popup_choose_div_click_');
        $('#af2_popup_modal_ .af2_popup_choose_div_click').removeClass('af2_popup_choose_div_click');
        

        const objectid = $(this).attr('data-objectid');
        $('#af2_popup_modal_ #af2_popup_delay_value').attr('data-objectid', objectid);
        $('#af2_popup_modal_ #af2_popup_click_value').attr('data-objectid', objectid);

        $('#af2_popup_modal_ .af2_modal_content #af2_popup_script').html('');
    });

    $(document).on('change', '#af2_popup_radio_chooser_ input:radio', function() {
        const val = $(this).val();
        const otherVal = $(this).attr('data-hide');

        $('.'+val).removeClass('af2_hide');
        $('.'+otherVal).addClass('af2_hide');

        $('#af2_popup_modal_ #af2_popup_delay_value').val('');
        $('#af2_popup_modal_ #af2_popup_click_value').val('');

        $('#af2_popup_modal_ .af2_modal_content #af2_popup_script').html('');
    });

    $(document).on('input', '#af2_popup_delay_value', function() {
        const val = $(this).val();
        const objectid = $(this).attr('data-objectid');
        let shortcode = '[funnelforms id="'+objectid+'" popup_by_delay="true" popup_delay="'+val+'"]';
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_script').html(shortcode);
    });

    $(document).on('input', '#af2_popup_click_value', function() {
        const val = $(this).val();
        const objectid = $(this).attr('data-objectid');
        let shortcode = '[funnelforms id="'+objectid+'" popup_by_click="true" popup_click_class="'+val+'"]';
        $('#af2_popup_modal_ .af2_modal_content #af2_popup_script').html(shortcode);
    });
});