(function ($) {
  "use strict";

  $(function () {
    /* Copy to clipboard */
    $(".domain-for-sale-shortcode-selectable").on("click", function (e) {
      e.preventDefault();
      domain_for_sale_copyToClipboard($(this));
      domain_for_sale_SelectText($(this));
      $(this).trigger("focus").select();
      $(".domain-for-sale-after-copy-text").animate(
        {
          opacity: 1,
          bottom: 25,
        },
        300
      );
      setTimeout(function () {
        jQuery(".domain-for-sale-after-copy-text").animate(
          {
            opacity: 0,
          },
          200
        );
        jQuery(".domain-for-sale-after-copy-text").animate(
          {
            bottom: 0,
          },
          0
        );
      }, 2000);
    });
    $(".ta_domain_for_sale_input").on("click", function (e) {
      e.preventDefault();
      /* Get the text field */
      var copyText = $(this);
      /* Select the text field */
      copyText.select();
      document.execCommand("copy");
      $(".domain-for-sale-after-copy-text").animate(
        {
          opacity: 1,
          bottom: 25,
        },
        300
      );
      setTimeout(function () {
        jQuery(".domain-for-sale-after-copy-text").animate(
          {
            opacity: 0,
          },
          200
        );
        jQuery(".domain-for-sale-after-copy-text").animate(
          {
            bottom: 0,
          },
          0
        );
      }, 2000);
    });
    // Live Preview script.
    var is_manage_preview = $('body').hasClass('post-type-dfs_template');
    var is_manage_listing_preview = $('body').hasClass('post-type-domain_listing');
    var preview_box = $('#dfs_preview_box');
    
    if (is_manage_preview || is_manage_listing_preview) {
      var preview_display = $('#dfs_live_preview, #listing_live_preview').hide();
      var action = 'dfs_preview_meta_box';
      var nonce = $('#dfs_metabox_noncedfs_layouts, #dfs_metabox_noncelisting_layouts').val();     
    }
    
    $(document).on('click', '#dfs-show-preview:contains(Hide)', function (e) {
      e.preventDefault();
      var _this = $(this);
      _this.html('<i class="icofont-eye-open" aria-hidden="true"></i>Show Preview');
      preview_box.html('');
      preview_display.hide();
    });
    $(document).on('click', '#dfs-show-preview:not(:contains(Hide))', function (e) {
      e.preventDefault();
      var _data = $('form#post').serialize();
      var _this = $(this);
      var data  = {
        action: action,
        data: _data,
        ajax_nonce: nonce
      };
      console.log('data', data);
      
      $.ajax({
        type: "POST",
        url: ajaxurl,
        data: data,
        error: function (response) {
          console.log( response);
        },
        success: function (response) {

          preview_display.show();
          preview_box.html(response);
          _this.html('<i class="icofont-eye-blocked" aria-hidden="true"></i>Hide Preview');
          $(document).on('keyup change', function (e) {
            e.preventDefault();
            _this.html('<i class="icofont-refresh" aria-hidden="true"></i>Update Preview');
          });
          if (is_manage_preview || is_manage_listing_preview) {
            $("html, body").animate({ scrollTop: preview_display.offset().top - 50 }, "slow");
            $('.tpro-preloader').animate({ opacity: 1 }, 600).hide();

            $('.wp-admin .sp-tpro-pagination li').on('click', function (e) {
              e.preventDefault();
              e.stopPropagation();
              e.stopImmediatePropagation();
              $('.sptpro-pagination-not-work').animate({
                opacity: 1,
                bottom: 25
              }, 300);
              setTimeout(function () {
                jQuery(".sptpro-pagination-not-work").animate({
                  opacity: 0,
                }, 200);
                jQuery(".sptpro-pagination-not-work").animate({
                  bottom: 0
                }, 0);
              }, 2500);
            });
          }
        }
      })
    });
    
    function domain_for_sale_copyToClipboard(element) {
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val($(element).text()).select();
      document.execCommand("copy");
      $temp.remove();
    }
    function domain_for_sale_SelectText(element) {
      var r = document.createRange();
      var w = element.get(0);
      r.selectNodeContents(w);
      var sel = window.getSelection();
      sel.removeAllRanges();
      sel.addRange(r);
    }

    $(".layout.column-layout").each(function () {
      $(this).text($(this).text().replace("_", " "));
    });


     // Apply on note on replace theme option
    var $section = $('.dfs_apply_on');
  var $select = $section.find('select[name="dfs_template_options[dfs_apply_on]"]');
  var $descText = $section.find('.domain-for-sale-desc-text');

  function toggleDescText() {
    if ($select.val() === 'replace_theme') {
      $descText.show();
    } else {
      $descText.hide();
    }
  }

  // Run on load
  toggleDescText();

  // Run on change
  $select.on('change', toggleDescText);

  
  });

  

})(jQuery);
