/* Minification failed. Returning unminified contents.
(283,20-21): run-time error JS1195: Expected expression: >
(283,56-57): run-time error JS1004: Expected ';': )
(287,18-19): run-time error JS1195: Expected expression: >
(287,42-43): run-time error JS1004: Expected ';': )
(377,26-27): run-time error JS1100: Expected ',': =
 */
document.querySelector("#nav-toggle").addEventListener("click", function () {
    this.classList.toggle("active");
});

/*Open and close mobile navigation*/
var navtoggle = 0;
$("a#nav-toggle").click(function () {
    closesearch();
    if (navtoggle == 0) {
        $("div#mobilenav").fadeIn();
        $("div#nav").addClass("active");
        $("div#navigation").addClass("active");
        $("div#mobilenavoverlay").fadeIn();
        $('.fa-search').css('display', 'none');
        navtoggle++
    }

    else {
        $("div#mobilenav").fadeOut();
        $("div#nav").removeClass("active");
        $("div#navigation").removeClass("active");
        $("div#mobilenavoverlay").fadeOut();
        $('.fa-search').css('display', 'block');
        navtoggle--
    }
});
$(window).scroll(function () {
    if ($(window).scrollTop() > 10 && !$('div#mobilenav').is(':visible')) {
        $("a#nav-toggle").fadeOut();
    } else {
        $("a#nav-toggle").fadeIn();
    }
});


/*Cycle 2 - next and previous controls*/
$("a#next-slide").click(function () {
    $('.cycle-slideshow').cycle('next');;
});
$("a#previous-slide").click(function () {
    $('.cycle-slideshow').cycle('prev');;
});


/*Document icon show hide basket
$( "#show-download-basket" ).click(function() {
    if ($("span.open-state").hasClass( "active-icon" )){
        $("span.open-state").removeClass( "active-icon" );}
    else {
            $("span.open-state").addClass( "active-icon" );}
});

var deeperElements = $(".deeper");
if(deeperElements != undefined)
{
    for(var i = deeperElements.length - 1; i >= 0; i--)
    {
        expandnav(deeperElements[i]);	
    }
}*/




/* touchwipe */
$("#banner").touchwipe({
    wipeLeft: function () { $('#banner .cycle-slideshow').cycle('next') },
    wipeRight: function () { $('#banner .cycle-slideshow').cycle('prev'); },
    wipeUp: function () { return false },
    wipeDown: function () { return false },
    min_move_x: 50,
    min_move_y: 50,
    preventDefaultEvents: false
});



/*show or hide content nav*/
$("span.open-state").click(function () {
    if ($(this).next().hasClass("expanded")) {
        $(this).next().addClass("closedimportant");
        $(this).next().removeClass("expanded");
    }
    else {
        $(this).next().removeClass("closedimportant");
        $(this).next().css("height", "auto");
        $(this).next().addClass("expanded");
        $(this).parent("li").css('height', 'auto');
    }
});

/*factsheets*/
$('#library-filter input[type="checkbox"]').on('ifClicked', function (event) {

    if ($('#listview').attr('checked')) {
        $('#listview').removeAttr('checked')
    }
    else {
        $('#listview').attr('checked', 'checked')
    }
    $(this).closest('form').submit()
})
$("#library-filter .search-input").click(function () {
    $(this).closest('form').submit()
});


$(document).ready(function () {

    $(".col-xs-3 span.open-state").next().addClass("closedimportant");
    $(".col-xs-3 .current span.open-state").next().removeClass("closedimportant");
    $(".col-xs-3 .current").parents().removeClass("closedimportant");
    $(".col-xs-3 .current ul li").children("ul").addClass("closedimportant");
    $(".col-xs-3 .current > span").addClass("active-icon");
    $(".col-xs-3 .current > ul").addClass("expanded");
    $(".col-xs-3 .current").parents().children("span").addClass("active-icon");
    $(".col-xs-3 .current").parents().children("ul").addClass("expanded");

    $(".content-nav li.current").each(function () {
        $(this).css("height", "auto");
        $(this).parents().css('height', 'auto');
        $(this).addClass("expanded");
        $(this).prev("span").addClass("active-icon")
    });


    $(".open-important").removeClass("closedimportant").addClass('expanded');


});

/*show or hide content grey*/
$("a.open-grey").click(function () {
    if ($(this).parent().parent().next().hasClass("expanded")) {
        $(this).parent().parent().next().css("height", "0");
        $(this).parent().parent().next().css("padding", "0");
        $(this).parent().parent().next().removeClass("expanded");
    }
    else {
        $(this).parent().parent().next().css("height", "auto");
        $(this).parent().parent().next().css("padding-top", "15px")
        $(this).parent().parent().next().addClass("expanded");
    }
});

/*Change expand icon*/
$("span.open-state").click(function () {
    if ($(this).hasClass("active-icon")) {
        $(this).removeClass("active-icon");
    }
    else {
        $(this).addClass("active-icon");
    }
});

/*Alternate article background colours on mobile devices*/
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
    $(".newsarticle").each(function () {
        if ($(this).prev().hasClass("box-grey")) {
            $(this).removeClass("box-grey");
            $(this).addClass("box-blue");
        }
        else if ($(this).prev().hasClass("box-blue")) {
            $(this).removeClass("box-blue");
            $(this).addClass("box-grey");
        }
    });
}

/*Re-style select elemets*/
$(document).ready(function () {
    customSelectInit();
})

/* Resize article preview sections to match the height of the largest block */
$(document).ready(function () {
    var maxHeight = -1;

    $('.newsarticle').each(function () {
        maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
    });

    $('.newsarticle').each(function () {
        $(this).height(maxHeight);
    });
});

/* Style file upload inputs */
$(document).ready(function () {
    $("input[type=file]").nicefileinput();
});

/* Your local headway - Swith between local and national */
$(document).ready(function () {
    $('.local-headway .tab:nth-child(1)').click(function () {
        $("#headway-local-results").addClass("active");
        $(this).addClass("active");
        $("#headway-national-results").removeClass("active");
        $(".local-headway .tab:nth-child(2)").removeClass("active");
    });
    $('.local-headway .tab:nth-child(2)').click(function () {
        $("#headway-local-results").removeClass("active");
        $(this).addClass("active");
        $("#headway-national-results").addClass("active");
        $(".local-headway .tab:nth-child(1)").removeClass("active");
    });
});

/* Open & Close Search Section */
$(document).ready(function () {
    $('div.search-wrapper a.close').click(function () {
        closesearch();
    });
    $('.fa-search').click(function () {
        $('.search-wrapper').css('width', '500px').css('opacity', '1');
        $('.search-wrapper form input[type="text"]').css('width', '400px').css('padding', '10px');
        $('.search-wrapper form button').css('display', 'block');
        $('.search-wrapper').addClass('isActive');
    });
    $(window).scroll(function () {
        var searchTop = ($('.search-wrapper').offset().top);
        if ($(window).scrollTop() > searchTop && $('.search-wrapper').hasClass('isActive')) {
            closesearch()
        }
    });
});

function closesearch() {
    $('div.search-wrapper a.close').parent().css('width', '0').css('opacity', '0');
    $('div.search-wrapper a.close').next().children('input').css('width', '0').css('padding', '10px 0');
    $('div.search-wrapper a.close').next().children('button').css('display', 'none');
    $('.search-wrapper').removeClass('isActive');
}

function customSelectInit() {
    console.log("custom select init");
    $(".custom-select").each(function () {
        $(this).wrap("<span class='select-wrapper'></span>");
        $(this).after("<span class='holder'></span>");
    });
    $(".custom-select").change(function () {
        var selectedOption = $(this).find(":selected").text();
        $(this).next(".holder").text(selectedOption);
    }).trigger('change');
}

$(document).ready(function () {

    $('#event-cal-tab').click(function () {
        if ($('.cal-wrapper').hasClass('active')) {
            $('.cal-wrapper').removeClass('active');
        }
        else {
            $('.cal-wrapper').addClass('active');
        }
    });

    $('.backbutton').click(function (e) {
        e.preventDefault();
        window.history.back();
    });

    $('div.emailaddress input, div.email input, input#Email').removeAttr('type').attr('type', 'email');
    $('div.phonenumber input, div.telephone input, input#PhoneNumber').attr('pattern', '[0-9]*');
});



function fullWidthImagePanel() {
    if (window.innerWidth >= 992) {
        $('.fullWidthImagePanel .img').css('height', $('.fullWidthImagePanel .dsc').outerHeight())
    }
}
fullWidthImagePanel()
$(window).on('resize', fullWidthImagePanel)


function fixYoutubeEmbeds(isPopup) {

    var iframes = document.querySelectorAll("iframe");

    var optanonCookie = decodeURIComponent(document.cookie.split('; ')
        .find(row => row.startsWith('OptanonConsent=')));

    var groupSection = optanonCookie
        .split('&')
        .find(x => x.startsWith("groups"))
        
    if (groupSection != null) {
       var cookieValue = groupSection.split('=')[1]
            .split(',')
    }
    else {
        cookieValue = null;
    }    
    
    if (iframes.length) {

        var msg = document.getElementById("noCookieMessage");
        if (msg) {
            msg.remove();
        }

        if (cookieValue != null) {
            if (!cookieValue[3] || cookieValue[3].endsWith(':0')) {
                iframes.forEach(function (iframe) {                   

                    if (iframe.src.includes("youtube") || (iframe.getAttribute("data-src") && iframe.getAttribute("data-src").includes("youtube"))) {
                        
                        var div = createCookieMessage();


                        iframe.parentNode.prepend(div);
                        iframe.style.zIndex = "-1";
                        iframe.hidden = true;
                    }
                })
            }
            else {
                if (msg) {
                    msg.remove();
                }
            }
        } else {
            createCookieMessage();
        }

        
    }
}

function refreshPage() {
    window.location.reload();
}

function createCookieMessage() {
    var div = document.createElement("div");
    div.id = "noCookieMessage"
    div.style = "background-color: #E0E8F0; Padding:20px 25px; width:100%;";
    div.zIndex = "-2";

    //built manually because OneTrust didn't like it being being as one string
    var beforeLinkText = document.createTextNode("Because of your cookies settings you are unable to see this video. If you'd like to view it, please ")

    var aTag = document.createElement("a");
    aTag.href = "/cookies#ot-sdk-cookie-policy";
    aTag.text = "visit our cookies page";
    aTag.target = "_blank"

    var pTag = document.createElement("p");
    pTag.style = "margin-bottom:0; white-space: pre-wrap;";

    //var button = document.createElement("button");
    //button.innerHTML = "Reload Page"
    //button.onclick = refreshPage;

    var afterLinkText = document.createTextNode(", scroll down and click the 'Cookies Settings' button. \n\nEnabling cookies will allow you to view all of the content on our website. After enabling cookies (above link) please ")

    var aTag2 = document.createElement("a");
    aTag2.href = "#";
    aTag2.onclick = refreshPage;
    aTag2.text = "click to refresh";

    var secondLinkText = document.createTextNode(" the page to show the video.")

    pTag.appendChild(beforeLinkText)
    pTag.appendChild(aTag)
    pTag.appendChild(afterLinkText)
    pTag.appendChild(aTag2)
    pTag.appendChild(secondLinkText)

    div.appendChild(pTag);

    return div;
}

function showLoader(text = 'Loading...') {
    const loader = $('#loader');
    const container = loader.find('.loader__container');

    loader.show();
    container.text(text);
}
window.showLoader = showLoader;

function closeLoader() {
    $('#loader').hide();
}
window.closeLoader = closeLoader;

$.validator.unobtrusive.adapters.addBool("mustbetrue", "required");;
