$(document).ready(function() {
    var sjs = SimpleJekyllSearch({
        searchInput: document.getElementById('search-input'),
        resultsContainer: document.getElementById('results-container'),
        json: '/search.json',
        searchResultTemplate: '<a class="dropdown-item" href="{url}">{title}</a>',
        noResultsText: '<a class="dropdown-item" href="#">Nessun risultato</a>',
        exclude: ["https://blog.unitoo.it", "https://blog.unitoo.pw"]
    });

    $(".morphext-bounce-in").Morphext({
        animation: "bounceIn",
        speed: 2500
    });

    $(".morphext-bounce-left").Morphext({
        animation: "zoomInLeft",
        speed: 4500
    });

    $(".morphext-bounce-up").Morphext({
        animation: "bounceInUp",
        speed: 6500
    });

    var startMusic = function() {
        var radio_player = document.getElementById("radio-player");

        if (radio_player.paused) {
            radio_player.play();
        }
    }

    var askForRadio = function() {
        $("body").overhang({
            custom: true,
            type: "confirm",
            primary: "#f39200",
            accent: "#ffc56e",
            yesColor: "white",
            yesMessage: "<i class='fas fa-heart text-danger'></i> Adoro!",
            noMessage: "<i class='fas fa-times-circle'></i> Magari un'altra volta!",
            noColor: "white",
            message: "Ti piacerebbe ascoltare la radio mentre navighi sul sito?",
            callback: function (value) {
                if (value) {
                    createCookie("radio-autoplay", "true", 31);
                    _paq.push(['trackGoal', 2]);
                    startMusic();
                } else {
                    createCookie("radio-autoplay", "false", 31);
                }
            }
        });
    };

    var radio_autoplay = readCookie("radio-autoplay");
    if (radio_autoplay == null) {
        setTimeout(askForRadio, 8000);
    } else if (radio_autoplay == 'true') {
        var radio_player = document.getElementById("radio-player");
        $("#radio-autoplay-button").prop("checked", true);

        radio_player.autoplay = radio_autoplay;
    }

    $("#radio-autoplay-button").on("change", function(e) {
        eraseCookie("radio-autoplay");
        createCookie("radio-autoplay", $(e.currentTarget).is(":checked"), 31);
    });
});
