//use strict;

//import * as Farapp from './fara.js';
let homeRE = new RegExp(/(https?:\S+\/$|index.html$)/);

if (homeRE.test(window.location.href)) {
    window.addEventListener('load', () => {
        let nextimage = 0;
        let header = document.querySelector('header');
        slider();
        
        function slider() {
            /*** Fade in??*/
            //opacity = 1;

        let images = [
                'img/fara_background.jpg',
                //'img/fara_bg_2.jpg',
                'img/fara_bg_3.jpg',
                'img/fara_bg_4.jpg',
                'img/fara_bg_5.jpg',
                //'img/fara_bg_6.jpg',
                'img/fara_bg_7.jpg',
                'img/fara_bg_8.jpg',
                'img/fara_bg_9.jpg',
                'img/fara_bg_10.png',
                'img/fara_bg_11.jpg',
            ];
            
            if (nextimage >= images.length) {
            nextimage = 0;
        }
        header.setAttribute('style', 'background-image : url("' + images[nextimage++] + '");');
        setTimeout(slider, 2000);
       }
    });
}
/**
 * Apply active class to selected link...
 * @todo Inefficient!!
 */
window.addEventListener('focus', () => {
    let navlinks = Array.from(document.querySelectorAll('#links a'));

    navlinks.forEach(element => {
        let ref = element.href.substr(element.href.lastIndexOf('/'), element.href.length);
        
        if (window.location.href.includes(ref)) {
            element.setAttribute('class', 'active');
        }
        if (homeRE.test(window.location.href)) {
            navlinks[0].setAttribute('class', 'active');
        }
    });
});
/**
 * Activate background on scroll for nav only
 * if not small screen
 */
window.addEventListener('scroll', event => {
    let nav = document.querySelector('#nav');
    let links = document.querySelector('#links');

    if (window.pageYOffset !== 0) {
        nav.classList.add('bg-white', 'border');
        nav.classList.remove('text-gray');
        links.classList.add('nav-dark');
        links.classList.remove('nav-light');
    }
    else {
        if (!document.querySelector('header').classList.contains('itin')) {
            nav.classList.add('text-gray');
            links.classList.remove('nav-dark');
            links.classList.add('nav-light');
        }
        nav.classList.remove('bg-white', 'border');
    }
});

window.addEventListener('load', () => {
    /**
    * Show horizontal nav on mobile
    */
    let menu = document.querySelector('i');

    menu.addEventListener('click', () => {
        document.querySelector('.side-nav').classList.remove('d-hide');
        document.querySelector('.side-nav').classList.remove('slide-out');
        document.querySelector('body').classList.add('opaque');
    });

    let close = document.querySelector('#close-nav');

    close.addEventListener('click', () => {
        document.querySelector('.side-nav').classList.add('d-hide');
        document.querySelector('body').classList.remove('opaque');
    })
})
/**
 * Open / close modal for videos
 * and apply video source
 */
window.addEventListener('load', () => {
    let vidLinks = Array.from(document.querySelectorAll('a[href="#open-vid"]'));
    let source = document.querySelector('source');
    let video = source.parentNode;
    /**
     * Dynamically change video source
     * Note naming convention...
     */
    vidLinks.forEach(a => {
        let aNodes = a.childNodes;
        
        a.addEventListener('click', () => {
            aNodes.forEach(n => {
                if (n.src) {
                    let vStr = n.src
                    .substr(n.src.lastIndexOf('/')+1, n.src.length)
                    .replace(/thumb_(.*)\.\w+$/, 'vid_$1.mp4');
                    
                    source.src = `assets/video/${vStr}`;
                    
                    video.load();
                }
            });
            
            document.querySelector('#mod-vid').classList.add('active');
        });
    });

    let close = document.querySelectorAll('#mod-vid a[href="#close"]')[1];

    close.addEventListener('click', () => {
        document.querySelector('#mod-vid').classList.remove('active');
    });
})