var $root = $('html, body');
function infoAggiornate(e) {
    swal({
        type: 'success',
        text: e,
        timer: 1500,
        showConfirmButton: false
    })
}
$(document).ready(function () {
    if ($("#contattiForm").length > 0) {
        // needs for recaptacha ready
        grecaptcha.ready(function () {
            // do request for recaptcha token
            // response is promise with passed token
            $('#contattiForm').parsley().on('form:error', function () {
                swal({
                    title: "Errore",
                    text: "Per procedere รจ necessario completare tutti i campi obbligatori",
                    type: "error",
                    showConfirmButton: true
                });
            }).on('form:submit', function () {
                $('#contattiForm').find('button[type=submit]').prop('disabled', true);
                grecaptcha.execute('6LcUAvgqAAAAAAKjRdjM-Wcwm64SnwxLXWebNk1B', {action: 'homepage'})
                    .then(function (token) {
                        // add token to form
                        document.getElementById('g-recaptcha-response').value = token;
                        form = $('#contattiForm');
                        $.ajax({
                            url: "https://www.trigraf.it/assets/ajax/invia.php",
                            type: "POST",
                            data: form.serialize(),
                            success: function (data) {
                                if (data.status == "success") {
                                    infoAggiornate(data.message);
                                    setTimeout(function () {
                                        window.location = 'https://www.trigraf.it/'
                                    }, 2000);
                                }
                                else if (data.status == "error") {
                                    swal({
                                        title: "Errore",
                                        text: data.message,
                                        type: "error",
                                        showConfirmButton: true
                                    });
                                    $('#contattiForm').find('button[type=submit]').prop('disabled', false);
                                    return false;
                                }
                            },
                        });
                    });
                return false;
            });
        });
    }
    $( '.fancy' ).fancybox({
        thumbs : {
            autoStart : true,
            axis: "y" // Vertical (y) or horizontal (x) scrolling
        },
        // Enable keyboard navigation
        keyboard: true,
        slideShow: {
            autoStart: false,
            speed: 3000
        }
    });
    // Load more data
    $('.load-more').click(function(){
        var close =$(this).closest('.row');
        var row = Number(close.find('#row').val());
        var allcount = Number(close.find('#all').val());
        var rowperpage = 8;
        row = row + rowperpage;
        if(row <= allcount){
            $(".portfolio-wrapper").isotope({
                layoutMode : 'masonry'
            });
            close.find("#row").val(row);
            $.ajax({
                url: 'https://www.trigraf.it/assets/ajax/getimmagini.php',
                type: 'post',
                data: {row:row,quante:rowperpage},
                beforeSend:function(){
                    close.find(".load-more").text("Caricamento...");
                },
                success: function(response){
                    // Setting little delay while displaying new content
                    setTimeout(function() {
                        // appending posts after last post with class="post"
                        newItems = $(response).appendTo(close.find('.portfolio-grid'));
                        newItems.imagesLoaded(function(){
                            close.find(".portfolio-grid").isotope('appended', newItems );
                        });
                        var rowno = row + rowperpage;
                        // checking row value is greater than allcount or not
                        if(rowno > allcount){
                            // Change the text and background
                            close.find('.load-more').hide();
                            //$('.load-more').css("background","darkorchid");
                        }else{
                            close.find(".load-more").text("Visualizza altri");
                        }
                        $( '.fancy' ).fancybox({
                            thumbs : {
                                autoStart : true,
                                axis: "y" // Vertical (y) or horizontal (x) scrolling
                            },
                            // Enable keyboard navigation
                            keyboard: true,
                            slideShow: {
                                autoStart: false,
                                speed: 3000
                            }
                        });
                    }, 100);
                }
            });
        }else{
            close.find('.load-more').text("Caricamento...");
            // Setting little delay while removing contents
            setTimeout(function() {
                // Reset the value of row
                close.find("#row").val(0);
                // Change the text and background
                close.find('.load-more').text("Visualizza altri");
            }, 2000);
        }
    });
    /* Check if the users browser supports these features */
    const enhance = 'querySelector' in document && 'addEventListener' in window && 'classList' in document.documentElement;
    /* If the users browser browser supports the features we need, remove the no-enhance class from the html element and execute our video JS */
    if(enhance) {
        document.documentElement.classList.remove('no-enhance');
        /* Find all video molecules and instantiate a new instance of the Video class */
        const videos = document.querySelectorAll('.js-video');
        Array.from(videos).forEach(function(video) {
            const videoEl = new Video(video);
        });
    }
});
class Video {
    constructor(video) {
        this.videoContainer = video;
        this.video = this.videoContainer.querySelector('.js-video-video');
        this.playButton = this.videoContainer.querySelector('.js-video-play-button');
        this.pauseButton = this.videoContainer.querySelector('.js-video-pause-button');
        this.prefersReducedMotion();
        this.addEventListeners();
    }
    prefersReducedMotion() {
        /* If the users browser supports reduced motion and the user has set it to true, remove the autoplay attribute from the video and pause it */
        if(matchMedia('(prefers-reduced-motion)').matches) {
            this.video.removeAttribute('autoplay');
            this.pauseVideo();
        }
    }
    playVideo() {
        this.video.play();
        /* Set the play button as pressed so it's hidden and the pause button is displayed instead */
        this.playButton.setAttribute('aria-pressed', 'true');
        this.pauseButton.setAttribute('aria-pressed', 'false');
    }
    pauseVideo() {
        this.video.pause();
        /* Set the pause button as pressed so it’s hidden and the play button is displayed instead */
        this.playButton.setAttribute('aria-pressed', 'false');
        this.pauseButton.setAttribute('aria-pressed', 'true');
    }
    addEventListeners() {
        this.playButton.addEventListener('click', function() {
            this.playVideo();
            /* Focus the pause button so keyboard users can immediately pause the video without having to tab away and back again */
            this.pauseButton.focus();
        });
        this.pauseButton.addEventListener('click', function() {
            this.pauseVideo();
            /* Focus the play button so keyboard users can immediately play the video without having to tab away and back again */
            this.playButton.focus();
        });
    }
}