// var revLevel = sessionStorage.getItem('revision');
// var IsChangelogPage = window.location.href.indexOf("changelog.html") != -1;
// var IsAuthoring = window.location.href.indexOf("Expere/Authoring") != -1;
// var IsDGS = window.location.href.indexOf("DGS") != -1;
// var IsWKHosted = window.location.href.indexOf("WK-Hosted-Expere") != -1;

if (IsChangelogPage) {
    $('#wh_publication_toc').remove();
    PrependClToc();
    CreateHeadingParasAndLists();
    CreateNewToc();
    ModifyCSS();
    if (IsAuthoring) {
        sortChangelogNewFeatures();
        sortChangelogNavEnhancements();
        sortChangelogNavDefects();
    }
}

function CreateHeadingParasAndLists() {
    $('.ChangelogNav').append('<p style="color: black"><b>New Features</b></p>' +
        '<ul id="NewFeaturesList"></ul>' +
        '<p style="color: black"><b>Enhancements</b></p>' +
        '<ul id="EnhancementsList"></ul>' +
        '<p style="color: black"><b>Defect Corrections</b></p>' +
        '<ul id="DefectsList"></ul>');
}

function CreateNewToc() {
    $(".simpletable").each(function () {
        var ReleasenoteEqualsDatarev = false;
        if (IsWKHosted || IsDGS
        ) {
            var curRev = $(this).closest('section').attr('data-rev');
            var EqualRevs = versionCompare(revLevel, curRev, true);
            if (curRev === revLevel) {
                ReleasenoteEqualsDatarev = true;
            } else {
                ReleasenoteEqualsDatarev = false;
            }
            if (EqualRevs > -1) {
                var title = $(this).find("th").text();
                var id = $(this).attr("id");
                var TypeFullText = $(this).find("li").first().text();
                var trimType = $.trim(TypeFullText);
                var type = trimType.split(" ").pop();
            }
        } else {
            var title = $(this).find("th").text();
            var id = $(this).attr("id");
            var TypeFullText = $(this).find("li").first().text();
            var trimType = $.trim(TypeFullText);
            var type = trimType.split(" ").pop();
        }
        if (
            (!IsWKHosted && !IsDGS) ||
            (IsWKHosted && ReleasenoteEqualsDatarev) ||
            (IsDGS && ReleasenoteEqualsDatarev)
        ) {
            if (type == "Feature") {
                $('#NewFeaturesList').append("<li>" +
                    "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            }
            if (type == "Enhancement") {
                $('#EnhancementsList').append("<li>" +
                    "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            }
            if (type == "Defect" || type == "Correction") {
                $('#DefectsList').append("<li>" +
                    "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            }
        }
    });
}

function InsertCurrentReleaseTitle() {
    if (IsPnP || IsWKHosted) {
        if (revLevel !== null) {
            $('.ChangelogNav').append('<p><b>Current Release:</b>  ' + revLevel + '</p>');
        } else {
            $('.ChangelogNav').append('<p><b>Current Release:</b>  </p>');
        }
    }
}

function PrependClToc() {
    $('#wh_topic_body').attr("class", "col-lg-8 col-sm-12");

    $('<nav class="clToc col-lg-4 d-none d-lg-block navbar d-print-none"><section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></nav>').insertAfter('#wh_topic_body');
}

function sortChangelogNavDefects() {
    var myDeflist = $('#DefectsList');
    var DefListItems = myDeflist.children('li').get();
    DefListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(DefListItems, function (idx, itm) {
        myDeflist.append(itm);
    });
}

function sortChangelogNavEnhancements() {
    var myEnhlist = $('#EnhancementsList');
    var EnhListItems = myEnhlist.children('li').get();
    EnhListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(EnhListItems, function (idx, itm) {
        myEnhlist.append(itm);
    });
}

function sortChangelogNewFeatures() {
    var myFeatlist = $('#NewFeaturesList');
    var NewFeatureListItems = myFeatlist.children('li').get();
    NewFeatureListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(NewFeatureListItems, function (idx, itm) {
        myFeatlist.append(itm);
    });
}

function ModifyCSS() { $('.ChangelogLink').css({ });
}