<?php

/* ------------------------------------------------------------------------- *
 *  Custom functions
/* ------------------------------------------------------------------------- */

function xtra_child_theme() {

	wp_enqueue_style( 'xtra-child', get_stylesheet_directory_uri() . '/style.css', array( 'xtra' ), '' );

}
add_action( 'wp_enqueue_scripts', 'xtra_child_theme' );

// Add your custom functions here, or overwrite existing ones. Read more how to use:
// http://codex.wordpress.org/Child_Themes

add_action('wp_head', function () {
    if (!is_page('infos')) return;
    ?>
    <script>
    (function () {
      console.log('debug infos actif', location.href);

      const origScrollTo = window.scrollTo;
      window.scrollTo = function () {
        console.group('window.scrollTo');
        console.log('args:', arguments);
        console.trace('stack scrollTo');
        console.groupEnd();
        return origScrollTo.apply(this, arguments);
      };

      const origScrollBy = window.scrollBy;
      window.scrollBy = function () {
        console.group('window.scrollBy');
        console.log('args:', arguments);
        console.trace('stack scrollBy');
        console.groupEnd();
        return origScrollBy.apply(this, arguments);
      };

      if (Element.prototype.scrollIntoView) {
        const origScrollIntoView = Element.prototype.scrollIntoView;
        Element.prototype.scrollIntoView = function () {
          console.group('scrollIntoView');
          console.log('target:', this);
          console.trace('stack scrollIntoView');
          console.groupEnd();
          return origScrollIntoView.apply(this, arguments);
        };
      }

      function patchjQuery() {
        if (!window.jQuery || !jQuery.fn || !jQuery.fn.animate) return;
        const oldAnimate = jQuery.fn.animate;
        jQuery.fn.animate = function () {
          const props = arguments[0] || {};
          if (props.scrollTop !== undefined) {
            console.group('jQuery.animate scrollTop');
            console.log('target set:', this);
            console.log('props:', props);
            console.trace('stack animate');
            console.groupEnd();
          }
          return oldAnimate.apply(this, arguments);
        };
      }

      patchjQuery();
      document.addEventListener('DOMContentLoaded', patchjQuery);
      window.addEventListener('load', patchjQuery);

      window.addEventListener('hashchange', function () {
        console.group('hashchange');
        console.log('new hash:', location.hash);
        console.trace('stack hashchange');
        console.groupEnd();
      });
    })();
    </script>
    <?php
}, 1);