window.onscroll = function () {
    scrollFunc();
};

let intro = document.getElementById("intro");
let nav_fixed = document.getElementById("nav");
let fix = intro.offsetTop;
function scrollFunc() {
    if (window.pageYOffset >= fix) {
        nav_fixed.classList.add("fix");
    } else {
        nav_fixed.classList.remove("fix");
    }
}

// preloader fucntion =============================
// 'use strict';
// $(window).on('load', function () {
//     $(".loader").fadeOut();
//     $("#preloader").delay(400).fadeOut("slow");

// });
// preloader fucntion end =======================


// Header

const selectElement = function (element) {
    return document.querySelector(element);
};

let menu = selectElement('.menu');
let hide_menu = selectElement('.hide_menu');
let link_media = selectElement('.link_media');
let hide_media = selectElement('.hide_media');

let currentLang = selectElement('.current_lang');
let languegs = selectElement('.languages');
let pyramid = selectElement('.pyramid');

let navs = selectElement('.navs_inner');
let burger = selectElement('.hamburg');

let langMobile = selectElement('.langMobile');
let languagesMobile = selectElement('.languagesMobile');

// console.log(burger);

window.onclick = function (e) {


    if (selectElement('.mobile__search').classList.contains('open') && !e.target.closest('.mobile__form')) {
        selectElement('.mobile__search').classList.remove('open');
    }


    if (selectElement('.navs__back').classList.contains('open') && !e.target.closest('.open__link') && !e.target.closest('.nav__group')) {
        selectElement('.navs__back').classList.remove('open');
    }

    if (selectElement('.navs__inner').classList.contains('open') && !e.target.closest('.open__link') && !e.target.closest('.nav__group')) {
        selectElement('.navs__inner').classList.remove('open');
    }
    
    if (selectElement('.hidden__media').classList.contains('open') && !e.target.closest('.nav_open_media')) {
        selectElement('.hidden__media').classList.remove('open');
    }
    

    // if (selectElement('.player_bg').classList.contains('videoShow') && !e.target.closest('.player')) {
    //     selectElement('.player_bg').classList.remove('videoShow');
    //     selectElement('.player__video').pause();
    //     selectElement('.player__video').currentTime = 0;
    // }
    
    // if (selectElement('.modal-photo').classList.contains('active') && !e.target.closest('.modal_gal') && !e.target.closest('.owl-nav')) {
    //     selectElement('.modal-photo').classList.remove('active');
    // }
    
    if (selectElement('.player_bg') != undefined) {
        if (selectElement('.player_bg').classList.contains('videoShow') && !e.target.closest('.player')) {
            selectElement('.player_bg').classList.remove('videoShow');
            selectElement('.player__video').pause();
            selectElement('.player__video').currentTime = 0;
        }
    }
    
    if(selectElement('.modal-photo') != undefined) {
        if (selectElement('.modal-photo').classList.contains('active') && !e.target.closest('.modal_gal') && !e.target.closest('.owl-nav')) {
            selectElement('.modal-photo').classList.remove('active');
        }
    }

}

function sleep(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

selectElement('.loupe').addEventListener('click', function () {
    sleep(2).then(() => {
        selectElement('.mobile__search').classList.toggle('open');
    });
});


selectElement('.new__hamburg').addEventListener('click', function () {
    sleep(2).then(() => {
        selectElement('.navs__back').classList.toggle('open');
    });
});

selectElement('.new__hamburg').addEventListener('click', function () {
    sleep(2).then(() => {
        selectElement('.navs__inner').classList.toggle('open');
    });
});

selectElement('.nav_open_media').addEventListener('click', function () {
    sleep(2).then(() => {
        selectElement('.hidden__media').classList.toggle('open');
    });
});

selectElement('.mobile__close').addEventListener('click', function () {
    sleep(2).then(() => {
        selectElement('.mobile__search').classList.remove('open');
    });
});


document.querySelectorAll('.gallery_video').forEach((e) => {
    e.addEventListener('click', function () {
        sleep(2).then(() => {
            selectElement('.player_bg').classList.toggle('videoShow');
            var vd = selectElement('.player__video');
            while (vd.lastElementChild) {
                vd.removeChild(vd.lastElementChild);
            }

            var source = document.createElement('source');
            //console.log(e)
            //console.log(e.dataset.src)

            source.setAttribute('src', e.dataset.src);

            vd.appendChild(source);
            vd.load();
            vd.play();
        });
    })
});

document.querySelectorAll('.video_item').forEach((e) => {
    e.addEventListener('click', function () {
        sleep(2).then(() => {
            selectElement('.player_bg').classList.toggle('videoShow');
            var vd = selectElement('.player__video');
            while (vd.lastElementChild) {
                vd.removeChild(vd.lastElementChild);
            }

            var source = document.createElement('source');
            //console.log(e)
            //console.log(e.dataset.src)

            source.setAttribute('src', e.dataset.src);

            vd.appendChild(source);
            vd.load();
            vd.play();
        });
    })
});
// }   

document.querySelectorAll('.player__video').forEach((e) => {
    e.addEventListener('ended', function () {
        // sleep(0).then(() => {
        selectElement('.player_bg').classList.remove('videoShow');
        // });
    })
});

// // End Header



