

var permissionMapping = [];
permissionMapping["PermissionCodes-Mtg_Manage"] = "mtg";

var exists = function(array, target) {

    if (!array || array.length == 0 || !target)
        return false;

    target = target.toLowerCase();

    var result = false;

    array.forEach(function(e) {
        
        var matchExists = (permissionMapping[e] && permissionMapping[e].toLowerCase() == target) || e.toLowerCase() == target;			
        
        if (matchExists) {
            result = true;
            return;
        }
    });

    return result;
}

var toggleVisiblity = function(attributeName, array) {
    $("*[" + attributeName + "]").each(function() {
        var attributeValue = $(this).attr(attributeName);
        var displayValue = exists(array, attributeValue) ? "inline" : "none";
        $(this).css("display", displayValue);
    })
}

// $.getJSON("/Token.json", function(user) {
// 	toggleVisiblity("data-product", user.permissions);
// });

// $.ajax( { url: "/Token.json", cache: false, success: function(user) {
// 	toggleVisiblity("data-product", user.permissions);
// 	// if (user.isAuthenticated) $("#btnLogout").show();
// }});

var userShouldSeeAllContent = function(user) {
    
if (!user || !user.permissions)
    return false;

var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/')).toLowerCase();

// PermissionCodes-APPrintPreviewDocumentsOnly

var displayAll = [];				
displayAll["/securityadministration"] = ["PermissionCodes-WK_WEBHELP_ACCESS"];

// Example:
// displayAll["/your_directory_here"] = ["x", "y", "z"];

// if the current directory is in the displayAll list, we need to check to see
// whether the user has a matching permission...

if (displayAll[dir]) {

    // get the permissions that all all content to be visible...
    var permissions = displayAll[dir];
    
    // Check to see if the current user has any matching permissions...
    for (i = 0; i < permissions.length; i++)
        for (u = 0; u < user.permissions.length; u++)
            if (permissions[i].toLowerCase() == user.permissions[u].toLowerCase())
                return true;
}

return false;
}

$.ajax( { url: "/Token.json", cache: false, success: function(user) {

if (userShouldSeeAllContent(user)) {
    
    // user has one of the magic permissions, so show everyting with a "data-product" attribute...
    
    $("*[data-product]").each(function() {
        $(this).css("display", "inline");
    });
    
}
else {
    
    // user does not have a SHOW ALL permission, so use their permissions to determine what should be shown...
    
    toggleVisiblity("data-product", user.permissions);
}

if (user.isAuthenticated) $("#btnLogout").show();
}});


