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

if (IsChangelogPage) {
    $('#wh_publication_toc').remove();
    $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
        '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
    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();
        }
        console.log(IsDGS);
        console.log(ReleasenoteEqualsDatarev);
        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() {
    $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;"><section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
}

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({ });
}