var regionFromCookie;
$(function () {
    regionFromCookie = Cookies.get("_region");
    currentFilteredRegion = regionFromCookie;
    filterContent(regionFromCookie);
});

$(document).on('regionChanged', function (evt, information) {
    // close hamburger menu after selecting a region from dropdown
    if (Modernizr.mq('(max-width: 992px)')) {
        if ($(".navBar").hasClass("mobile")) {
            $(".navbar-toggle").trigger("click");
        }
    }
    filterContent(information);   
});

$(document).on('CmsContentRefreshed', function () {
    //Region filter variable from _LayoutScripts{Client}.cshtml
    if (typeof currentFilteredRegion !== 'undefined') {
        regionFromCookie = currentFilteredRegion;
    } else {
        regionFromCookie = Cookies.get("_region");
    }

    filterContent(regionFromCookie);
});

function filterContent(information) {
    var hideOnAllClass = $(".hideOnAll");
    //This should be a single class but changing everything in dev+stage+prod is a pain
    var allRegions = $("div[data-atisParent='true'] div[id$='Section'],div[data-atisParent='true'] div[class$='Section']");
    //allRegions.hide();
   
    if (information && information != null) {
        information = information.toUpperCase();
        if (information != "ALL") {
            hideOnAllClass.show();
            $("#" + information + "Section, ." + information + "Section").show();
            allRegions.filter(":not(#" + information + "Section, ." + information + "Section)").hide();
        } else {
            allRegions.show();
            hideOnAllClass.hide();
        }
    } else {
        allRegions.show();
        hideOnAllClass.hide();
    }

    // Hide Personlization login text section when going to homepage while already logged in
    if ($(".loginSection form").length == 0) {
        if ($("#Home_LandingPage .post-content").length > 0) {
            $("#Home_LandingPage .post-content").show();
        }
    }
    $(document).trigger('contentFiltered');
};
