jQuery(document).ready( function($) {
    $('#wpfresh5-tabs a.nav-tab').click(function(e){
        e.preventDefault();
        $(this).addClass("nav-tab-active");
        $(this).siblings().removeClass("nav-tab-active");
        var tab = $(this).attr("href");
        $(".tab-inner").not(tab).css("display", "none");
        $(tab).fadeIn();
    });

    $('#wpfresh5-tabs a.nav-tab-inner').click(function(e){
        e.preventDefault();
        var tab = $(this).attr("href");
        current_tab = $('body').find('a[href='+tab+']');
        current_tab.addClass("nav-tab-active");
        current_tab.siblings().removeClass("nav-tab-active");
        $(".tab-inner").not(tab).css("display", "none");
        $(tab).fadeIn();
    });

    try {
        jQuery('.wpfresh5-color-picker').wpColorPicker();
        jQuery('a.wpfresh5pop').webuiPopover({trigger:'hover'});
    }
    catch(err) {
    }
    jQuery('#wpfresh5-wizard1').formalize();
    jQuery('#wpfresh5-wizard2').formalize();
    jQuery('#wpfresh5-wizard3').formalize();
    jQuery('#wpfresh5-wizard4').formalize();

    /* 1.2. WP Image Selector */
    var _custom_media = true,
    _orig_send_attachment = wp.media.editor.send.attachment;

    jQuery(document).on('click', '.wpfresh5_img_upload', function(e) {
        e.preventDefault();
        var send_attachment_bkp = wp.media.editor.send.attachment;
        var button = jQuery(this);
        _custom_media = true;

        wp.media.editor.send.attachment = function(props, attachment){
            // console.log('a')
            if ( _custom_media ) {
            // console.log('b')

                // console.log(attachment)
                parent = button.closest('.file-upload-parent');
                parent.find('.file-upload-url').val(attachment.url)
                jQuery('.file-upload-url').trigger('change');
                parent.find('.file-upload-img').attr('src', attachment.url)
                parent.find('.file-upload-img').show();
            } else {
                return _orig_send_attachment.apply( this, [props, attachment] );
            };
        }

        wp.media.editor.open(button);
        return false;
    });

    jQuery(document).on('click', '.wpfresh5_img_clear', function(e) {
        e.preventDefault();
        var button = jQuery(this);
        parent = button.closest('.file-upload-parent');
        parent.find('.file-upload-url').val('')
        jQuery('.file-upload-url').trigger('change');
        parent.find('.file-upload-img').attr('src', '')
        parent.find('.file-upload-img').hide();
    });

    jQuery(document).on('click', '#wpfresh5-expand-all', function(e) {
        jQuery('#wpfresh5-wizard2 .form-section').show();
    });


    /* Custom Stuff */

    // 1. Content
    jQuery( 'input.wpfresh5_cc_create_cats').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cc_catnames_row' ).show();
        else
            jQuery( '.wpfresh5_cc_catnames_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cc_create_tags').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cc_tagnames_row' ).show();
        else
            jQuery( '.wpfresh5_cc_tagnames_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cc_create_pages').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cc_pagenames_row' ).show();
        else
            jQuery( '.wpfresh5_cc_pagenames_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cc_create_posts').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cc_postnames_row' ).show();
        else
            jQuery( '.wpfresh5_cc_postnames_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cu_underc_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cu_underc_text_row' ).show();
        else
            jQuery( '.wpfresh5_cu_underc_text_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cg_cookiew_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cg_cookiew_text_row' ).show();
        else
            jQuery( '.wpfresh5_cg_cookiew_text_row' ).hide();
    });

    jQuery( 'input.wpfresh5_ci_optimize').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_ci_optimization_row' ).show();
        else
            jQuery( '.wpfresh5_ci_optimization_row' ).hide();
    });

    jQuery( 'input.wpfresh5_ci_watermark').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_ci_watermark_url_row' ).show();
        else
            jQuery( '.wpfresh5_ci_watermark_url_row' ).hide();
    });

    jQuery( 'input[name="wpfresh5_cg_createpage[amazon]"]').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cg_amazoncountry_row' ).show();
        else
            jQuery( '.wpfresh5_cg_amazoncountry_row' ).hide();
    });

    jQuery( 'input.wpfresh5_cf_enable_fonts').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_cf_fonts_row' ).show();
        else
            jQuery( '.wpfresh5_cf_fonts_row' ).hide();
    });

    // 3. Security
    jQuery( 'input.wpfresh5_s_limit_logins').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_s_logins_num_row' ).show();
        else
            jQuery( '.wpfresh5_s_logins_num_row' ).hide();
    });

    jQuery( 'input.wpfresh5_s_ch_login_url').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_s_login_url_row' ).show();
        else
            jQuery( '.wpfresh5_s_login_url_row' ).hide();
    });

    jQuery( 'input.wpfresh5_s_hide_wp').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_s_hidewp_row' ).show();
        else
            jQuery( '.wpfresh5_s_hidewp_row' ).hide();
    });

    $('#wpfresh5_backup_start_backup').click(function(e){
        e.preventDefault(), jQuery.ajax({
            type: "GET",
            dataType: "json",
            url: ajaxurl,
            data: {
                action: "wpfresh5_db_backup_now"
            },
            beforeSend: function() {
                jQuery("#wpfresh5_backup_start_backup").after('<span id="wpfresh5_loading-message"> Loading...</span>')
            },
            success: function(e) {
                console.log(e)
                jQuery("#wpfresh5_db_backup_download_file").show(), jQuery('label[for="wpfresh5_db_backup_download_file"]').show(), jQuery("#wpfresh5_backup_download_file").attr("href", e.FullDownloadPath)
            },
            error: function() {},
            complete: function() {
                jQuery("#wpfresh5_loading-message").remove()
            }
        })
    });

    jQuery( 'input.wpfresh5_s_limit_logins').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_s_logins_num_row' ).show();
        else
            jQuery( '.wpfresh5_s_logins_num_row' ).hide();
    });

    // 4. SEO

    jQuery( 'input.wpfresh5_s_robotstxt').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_s_robotstxt_txt_row' ).show();
        else
            jQuery( '.wpfresh5_s_robotstxt_txt_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sl_enable_business').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sl_business_row' ).show();
        else
            jQuery( '.wpfresh5_sl_business_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sl_enable_rating').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sl_rating_row' ).show();
        else
            jQuery( '.wpfresh5_sl_rating_row' ).hide();
    });

    // 5. Branding
    jQuery( 'input.wpfresh5_bl_custom_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_bl_login_settings_row' ).show();
        else
            jQuery( '.wpfresh5_bl_login_settings_row' ).hide();
    });

    // 6. Social
    jQuery( 'input.wpfresh5_ss_show').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_ss_row' ).show();
        else
            jQuery( '.wpfresh5_ss_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sm_twitter').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sm_twitter_row' ).show();
        else
            jQuery( '.wpfresh5_sm_twitter_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sm_googleplus').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sm_googleplus_row' ).show();
        else
            jQuery( '.wpfresh5_sm_googleplus_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sas_facebook').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sas_facebook_row' ).show();
        else
            jQuery( '.wpfresh5_sas_facebook_row' ).hide();
    });

    jQuery( 'input.wpfresh5_sas_twitter').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_sas_twitter_row' ).show();
        else
            jQuery( '.wpfresh5_sas_twitter_row' ).hide();
    });
    // 7. Others

    jQuery( 'input.wpfresh5_oo_minimize_js').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfres_oo_exclude_js_row' ).show();
        else
            jQuery( '.wpfres_oo_exclude_js_row' ).hide();
    });

    jQuery( 'input.wpfresh5_oo_minimize_css').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfres_oo_exclude_css_row' ).show();
        else
            jQuery( '.wpfres_oo_exclude_css_row' ).hide();
    });

    jQuery( 'input.wpfresh5_ov_scrolltop').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_ov_scrolltop_txt_row' ).show();
        else
            jQuery( '.wpfresh5_ov_scrolltop_txt_row' ).hide();
    });

    jQuery( '#wpfresh5-show-database').click( function( e ) {
        e.preventDefault();
        jQuery('#wpfresh5-database-tables').toggle();
    });

    jQuery("#wload-recommended-plugins").click(function(e){
        e.preventDefault();
        jQuery('#wpfresh5_own_plugins').val( jQuery('#wrecommended-plugins-txt').html());
    });

    /* Instareset */

    jQuery( 'a#wpfs5-more-instareset').click( function( e ) {
        e.preventDefault();
        jQuery('#advanced_instareset').toggle();
    });

    /* MOnetize*/

    jQuery(document).on('click', '.wpfresh5-add-inline-link', function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var parent = jQuery('#wpfresh5-inline-links-table');
        // var num = parent.find('.wpfresh5_num').val();
        num_rows = parent.find( '.wpfresh5_links_row' ).length + 1;
        field1_name = 'wpfresh5_monetize_links['+num_rows+'][keyword]';
        field2_name = 'wpfresh5_monetize_links['+num_rows+'][url]';
        var row = '<tr class="wpfresh5_links_row"><td><input type="text" style="width:100%" name="'+field1_name+'" /></td><td><input type="text" style="width:100%" name="'+field2_name+'" /></td>'+
        '<td><button type="button" class="button button-secondary wpfresh5_remove_link_row"><i class="fa fa-remove"></i></button></td></tr>';
        parent.append(row);
    });

    jQuery(document).on('click', '.wpfresh5_remove_link_row', function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var parent = $this.closest( 'tr' )
        parent.remove();
    });

    jQuery( 'input.wpfresh5_amazon_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_mon_amazon_row' ).show();
        else
            jQuery( '.wpfresh5_mon_amazon_row' ).hide();
    });

    jQuery( 'input.wpfresh5_ebay_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_mon_ebay_row' ).show();
        else
            jQuery( '.wpfresh5_mon_ebay_row' ).hide();
    });

    jQuery( 'input.wpfresh5_clickbank_enabled').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is( ':checked' );
        if( checked )
            jQuery( '.wpfresh5_mon_clickbank_row' ).show();
        else
            jQuery( '.wpfresh5_mon_clickbank_row' ).hide();
    });

    /* Legal */
    jQuery( '.wpfresh5_cookie_page').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var val = $this.val();
        if( val == 'new' ) {
            jQuery( '.cookiew_pagename_row' ).show();
            jQuery( '#cookiew_pageid_row' ).hide();
        }
        else {
            jQuery( '.cookiew_pagename_row' ).hide();
            jQuery( '#cookiew_pageid_row' ).show();
        }
    });
    jQuery( '#generate-cpolicy').click( function( e ) {
        e.preventDefault();
        // console.log('z')
        jQuery('#wpfresh5_save_cookiepol').val( 1 );
        // console.log('a')
        jQuery('#wpfresh5-settings-form').submit();
        // console.log('b')
    });

    jQuery( '.wpfresh5_privacy_page').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var val = $this.val();
        if( val == 'new' ) {
            jQuery( '.privacyp_pagename_row' ).show();
            jQuery( '#privacyp_pageid_row' ).hide();
        }
        else {
            jQuery( '.privacyp_pagename_row' ).hide();
            jQuery( '#privacyp_pageid_row' ).show();
        }
    });
    jQuery( '#generate-ppolicy').click( function( e ) {
        e.preventDefault();
        jQuery('#wpfresh5_save_privacypol').val( 1 );
        jQuery('#wpfresh5-settings-form').submit();
    });


    jQuery( '.wpfresh5_tandc_page').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var val = $this.val();
        if( val == 'new' ) {
            jQuery( '.tandc_pagename_row' ).show();
            jQuery( '#tandc_pageid_row' ).hide();
        }
        else {
            jQuery( '.tandc_pagename_row' ).hide();
            jQuery( '#tandc_pageid_row' ).show();
        }
    });
    jQuery( '#generate-tandc').click( function( e ) {
        e.preventDefault();
        jQuery('#wpfresh5_save_tandc').val( 1 );
        jQuery('#wpfresh5-settings-form').submit();
    });

    jQuery( '#cookiec_email').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is(':checked');
        if( checked )
            jQuery('.cookiew_email_row').show()
        else
            jQuery('.cookiew_email_row').hide()
    });

    jQuery( '#cookiec_url').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is(':checked');
        if( checked )
            jQuery('.cookiew_url_row').show()
        else
            jQuery('.cookiew_url_row').hide()
    });

    jQuery( '#cookiec_phone').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is(':checked');
        if( checked )
            jQuery('.cookiew_phone_row').show()
        else
            jQuery('.cookiew_phone_row').hide()
    });

    jQuery( '#taconlinepayments').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is(':checked');
        if( checked )
            jQuery('.tandc_payment_row').show()
        else
            jQuery('.tandc_payment_row').hide()
    });

    // GDPR
    jQuery( '#euactive').change( function( e ) {
        e.preventDefault();
        var $this = jQuery(this)
        var checked = $this.is(':checked');

        if( checked )
            jQuery('#euurlrow').show()
        else
            jQuery('#euurlrow').hide()
    });

    jQuery('#rtbg-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#gdprfmmsg-def').val(), 'gdprfmmsg' )
    });

    jQuery('#daccess-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#daccess-def').val(), 'damessage' )
    });

    jQuery('#dbreach-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#dbreach-def').val(), 'dbrmessage' )
    });

    jQuery('#drect-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#drect-def').val(), 'drrmessage' )
    });

    jQuery('#unsub1-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#unsub1-def').val(), 'unsnotif' )
    });

    jQuery('#unsub2-default-txt').click( function( e ) {
        e.preventDefault();
        var $this = jQuery(this);
        wpfresh5_tmce_setContent( jQuery('#unsub2-def').val(), 'unsconf' )
    });
});

function wpfresh5_tmce_setContent(content, editor_id, textarea_id) {
  if ( typeof editor_id == 'undefined' ) editor_id = wpActiveEditor;
  if ( typeof textarea_id == 'undefined' ) textarea_id = editor_id;
  if ( jQuery('#wp-'+editor_id+'-wrap').hasClass('tmce-active') && tinyMCE.get(editor_id) ) {
    return tinyMCE.get(editor_id).setContent(content);
  }else{
    return jQuery('#'+textarea_id).val(content);
  }
}