// source --> https://kinderland-weyarn.de/wp-content/plugins/admin-site-enhancements/assets/js/external-permalinks.js 
document.addEventListener('DOMContentLoaded', function() {

   // console.log( phpVars );

   // When External Permalinks is enabled, remove #new_tab from external permalinks and set target to _blank
   if (phpVars.externalPermalinksEnabled) {

      var links = document.getElementsByTagName('a');

      for (var i = 0; i < links.length; i++) {
         var url = links[i].getAttribute('href');
         var target = links[i].getAttribute('target');

         if (url != null) {
            if (url.indexOf('#new_tab') >= 0) {
               url = url.replace('#new_tab', '');
               target = '_blank';
               links[i].setAttribute('href', url);
               links[i].setAttribute('target', target);
               links[i].setAttribute('rel', 'noopener noreferrer nofollow');
            }
         }
      }
   }

});
// source --> https://kinderland-weyarn.de/wp-content/plugins/email-encoder-bundle/assets/js/custom.js 
/* Email Encoder */
/*global jQuery, window*/
jQuery(function ($) {

    'use strict';

    // encoding method
    function rot13(s) {
        // source: http://jsfromhell.com/string/rot13
        return s.replace(/[a-zA-Z]/g, function (c) {
            return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
        });
    }

    /**
     * EMAIL RELATED LOGIC
     */

    // fetch email from data attribute
    function fetchEmail(el) {
        var email = el.getAttribute('data-enc-email');

        if (!email) {
            return null;
        }

        // replace [at] sign
        email = email.replace(/\[at\]/g, '@');

        // encode
        email = rot13(email);

        return email;
    }

    // replace email in title attribute
    function parseTitle(el) {
        var title = el.getAttribute('title');
        var email = fetchEmail(el);

        if (title && email) {
            title = title.replace('{{email}}', email);
            el.setAttribute('title', title);
        }
    }

    // set input value attribute
    function setInputValue(el) {
        var email = fetchEmail(el);

        if (email) {
            el.setAttribute('value', email);
        }
    }

    // open mailto link
    function mailto(el) {
        var email = fetchEmail(el);

        if (email) {
            window.location.href = 'mailto:' + email;
        }
    }
    
    // revert
    function revert(el, rtl) {
        var email = fetchEmail(el);

        if (email) {
           rtl.text(email);
           rtl.removeClass('eeb-rtl');
        }
    }

    // prepare for copying email
    document.addEventListener('copy', function(e){
        $('a[data-enc-email]').each(function () {
            var rtl = $(this).find('.eeb-rtl');

            if (rtl.text()) {
                revert(this, rtl);
            }
        });
        console.log('copy');
    });

    // set mailto click
    $('body').on('click', 'a[data-enc-email]', function () {
        mailto(this);
    });

    // parse title attirbute
    $('a[data-enc-email]').each(function () {
        parseTitle(this);
    });

    // parse input fields
    $('input[data-enc-email]').each(function () {
        setInputValue(this);
    });

});