/* Основной JavaScript */
document.addEventListener("DOMContentLoaded", function() {
    // Код для sticky-to-top кнопки
    function stickyToTop() {
        var doc = document.documentElement;
        var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
        var stickyEl = document.getElementById('sticky-to-top');

        if (top > 300) {
            stickyEl.classList.add('sticky-to-top');
        } else {
            stickyEl.classList.remove('sticky-to-top');
        }
    }

    stickyToTop();
    window.addEventListener('scroll', function() {
        stickyToTop();
    });

    // Валидация телефона в форме
    const cbox = document.querySelectorAll('input[name="phone"].uk-input');
    for (let i = 0; i < cbox.length; i++) {
        cbox[i].addEventListener('keydown', checkPhone);
    }

    function checkPhone(e) {
        let value = this.value.replace(/\s+/g, '');
        let isBackspace = e.key === 'Backspace';
        if ((e.key.length === 1 && /^[^\d\s\+\-\)\(]+$/.test(e.key)) || (!isBackspace && value.length === 16)) {
            e.preventDefault();
            return false;
        }
    }
});

// UIkit компоненты и инициализация
if (typeof UIkit !== 'undefined') {
    UIkit.icon.add({
        "zondicons--phone": '<svg xmlns="http:\/\/www.w3.org\/2000\/svg" viewBox="0 0 20 20" width="20" height="20"><path d="M20 18.35V19a1 1 0 01-1 1h-2A17 17 0 010 3V1a1 1 0 011-1h4a1 1 0 011 1v4c0 .56-.31 1.31-.7 1.7L3.16 8.84c1.52 3.6 4.4 6.48 8 8l2.12-2.12c.4-.4 1.15-.71 1.7-.71H19a1 1 0 01.99 1v3.35z"\/><\/svg>',
        "bootstrap--envelope": '<svg xmlns="http:\/\/www.w3.org\/2000\/svg" fill="currentColor" viewBox="0 0 16 16" width="20" height="20"><path d="M0 4a2 2 0 012-2h12a2 2 0 012 2v8a2 2 0 01-2 2H2a2 2 0 01-2-2V4zm2-1a1 1 0 00-1 1v.217l7 4.2 7-4.2V4a1 1 0 00-1-1H2zm13 2.383l-4.758 2.855L15 11.114v-5.73zm-.034 6.878L9.271 8.82 8 9.583 6.728 8.82l-5.694 3.44A1 1 0 002 13h12a1 1 0 00.966-.739zM1 11.114l4.758-2.876L1 5.383v5.73z"\/><\/svg>',
        "ionicons-sharp--location-sharp": '<svg xmlns="http:\/\/www.w3.org\/2000\/svg" viewBox="0 0 512 512" width="20" height="20"><path d="M256 32C167.67 32 96 96.51 96 176c0 128 160 304 160 304s160-176 160-304c0-79.49-71.67-144-160-144zm0 224a64 64 0 1164-64 64.07 64.07 0 01-64 64z"\/><\/svg>'
    });
}