
(function () {
    'use strict';
    if (window._impulseHeadingTiltLoaded) return;
    window._impulseHeadingTiltLoaded = true;

    var instances = [];
    var rafId = null;

    function init(el) {
        if (el._htInited) return;
        var tilt = parseInt(el.getAttribute('data-tilt'), 10) || 0;
        if (tilt <= 0) return;
        el._htInited = true;

        
        instances.push({
            el: el,

            
            maxDeg: Math.max(0, Math.min(50, tilt)),
            tx: 0, ty: 0,        
            cx: 0, cy: 0         
        });

        
        var cs = window.getComputedStyle(el);
        if (cs.perspective === 'none' || cs.perspective === '') {
            el.style.perspective = '1200px';
        }
        el.style.transformStyle = 'preserve-3d';
        el.style.willChange = 'transform';
    }

    function onMove(e) {
        for (var i = 0; i < instances.length; i++) {
            var inst = instances[i];
            var r = inst.el.getBoundingClientRect();
            if (r.width <= 0 || r.height <= 0) continue;

            

            

            
            var px = (e.clientX - r.left) / r.width  - 0.5;
            var py = (e.clientY - r.top)  / r.height - 0.5;

            var MARGIN = 1.0; 
            if (Math.abs(px) > MARGIN || Math.abs(py) > MARGIN) {
                inst.tx = 0; inst.ty = 0;
            } else {
                
                var ix = px * 2, iy = py * 2;
                if (ix >  1) ix =  1; else if (ix < -1) ix = -1;
                if (iy >  1) iy =  1; else if (iy < -1) iy = -1;
                inst.tx = ix; inst.ty = iy;
            }
        }
    }

    function tick() {
        for (var i = 0; i < instances.length; i++) {
            var inst = instances[i];

            
            
            var txC = inst.tx * Math.abs(inst.tx);
            var tyC = inst.ty * Math.abs(inst.ty);
            inst.cx += (txC - inst.cx) * 0.04;
            inst.cy += (tyC - inst.cy) * 0.04;

            
            var cx = inst.cx, cy = inst.cy;
            if (cx >  1) cx =  1; else if (cx < -1) cx = -1; else if (cx !== cx) cx = 0;
            if (cy >  1) cy =  1; else if (cy < -1) cy = -1; else if (cy !== cy) cy = 0;

            

            

            

            
            
            var _swing = inst.maxDeg * 0.35;
            var rotX = (-cy * _swing * 0.12).toFixed(2);
            var rotY = ( cx * _swing).toFixed(2);
            inst.el.style.transform = 'rotateX(' + rotX + 'deg) rotateY(' + rotY + 'deg)';
        }
        rafId = requestAnimationFrame(tick);
    }

    function scan() {

        

        document.querySelectorAll('.impulse-display-text[data-tilt],.impulse-logo-animator[data-tilt]').forEach(init);
    }

    function start() {
        scan();
        document.addEventListener('mousemove', onMove, { passive: true });

        document.addEventListener('mouseleave', function () {
            for (var i = 0; i < instances.length; i++) { instances[i].tx = 0; instances[i].ty = 0; }
        });
        if (!rafId) rafId = requestAnimationFrame(tick);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', start);
    } else {
        start();
    }

    if (typeof MutationObserver !== 'undefined') {
        new MutationObserver(function () {
            scan();
        }).observe(document.body || document.documentElement, { childList: true, subtree: true });
    }
})();
