// ========== changelog-toc.js ==========

// #region Define Variables
var IsAuthoringChangelog        = window.location.href.indexOf("Expere/Authoring/ReleaseNotes") != -1;
var IsAuthDDVChangelog          = window.location.href.indexOf("exp_DDV_changelog") != -1;
var IsAuthPrevCL                = window.location.href.indexOf("exp_Changelogs.html") != -1;
var IsChangelogPage             = window.location.href.indexOf("change_log_entries.html") != -1;
var IsDDChangelogPage           = window.location.href.indexOf("exp_defaultdata_changelogentries.html") != -1;
var IsDefaultDataChangelog      = window.location.href.indexOf("DefaultData/Changelogs") != -1;
var IsDGSChangelog              = window.location.href.indexOf("DGS/Change-Log") != -1;
var IsDocMgmt                   = window.location.href.indexOf("eOriginal/DocumentMgmt") > 1;
var IsDocumentPortal            = window.location.href.indexOf("edp_change-log-entries") != -1;
var IsDocViewer                 = window.location.href.indexOf("DocViewer/ReleaseNotes") != -1;
var IsEZGatewayChangelogPage    = window.location.href.indexOf("exp_ezgateway_changelogentries.html") != -1;
var IsI32Changlog               = window.location.href.indexOf("I-32/Changelog") != -1;
var IsOPPSA                     = window.location.href.indexOf("OPPSA/Changelog") != -1;
var IsPackAndPackChangelog      = window.location.href.indexOf("PackagesAndPackets/ChangeLog") != -1;
var IsWKHostedChangelog         = window.location.href.indexOf("WK-Hosted-Expere/Change-Log") != -1;
var IsWKHostedOverview          = window.location.href.indexOf("WK-Hosted-Expere/Change-Log/topics/eds_change-log-overview.html") != -1;


var revLevel                    = getURLParameter('rev');
// #endregion

if (IsI32Changlog) {
    PrependClToc();
}


if (IsI32Changlog || 
    IsAuthoringChangelog || 
    IsAuthDDVChangelog || 
    IsDDChangelogPage || 
    IsDocMgmt || 
    IsDocumentPortal || 
    IsEZGatewayChangelogPage || 
    IsPackAndPackChangelog ||
    (IsOPPSA && IsChangelogPage) || 
    (IsWKHostedChangelog && (!IsWKHostedOverview)) || 
    IsDGSChangelog || 
    IsDocViewer) {
    //remove existing TOC
    $('[id*="_ChangelogNavEnhancements"]').empty();
    $('[id*="_ChangelogNavDefects"]').empty();


    //Create section to hold TOC
    if (IsAuthoringChangelog) {
        $('.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>');
        if (IsAuthPrevCL) {
            $('.clToc').remove();
        }
    }

    if (IsAuthDDVChangelog) {
        $('.cl_section').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
    }

    if (IsDDChangelogPage || IsEZGatewayChangelogPage) {
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('article').css('width', '105%');
        $('#exp_defaultdata_changelogentries__section_cwl_vcc_j2b').css('display', 'table-cell');
        $('#exp_defaultdata_changelogentries__section_cwl_vcc_j2b').css('padding-right', '10px');
    }

    if (IsDocumentPortal) {
        $('#wh_publication_toc').remove();
        $('#wh_topic_body').removeClass();
        $('.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>');
        $('.cl_section:eq(0)').css('display', 'table-cell');
        $('.cl_section:eq(0)').css('padding-right', '20px');
        $('.col-lg-7').css('width', '100%');
    }

    if (IsDocViewer) {
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
             $('#wh_publication_toc').remove();
             $('#wh_topic_body').css('width','100%');
    }

    if (IsOPPSA && IsChangelog) {
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('#wh_topic_body').css('width', '70%');
        $('#c13030483__section_ikz_gcn_n2b').css('display', 'table-cell');
        $('#c13030483__section_ikz_gcn_n2b').css('padding-right', '10px');
        $('article').css('width', '110%');
    }

    if (IsPackAndPackChangelog) {
        $('.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>');
        $('#wh_topic_body').css('width', '70%');
        $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('display', 'table-cell');
        $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('padding-right', '10px');
    }

    if ((IsWKHostedChangelog && (!IsWKHostedOverview)) || IsDGSChangelog) {
        $('.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>');
        // $('#concept_vw1_sxj_xnb__section_hdv_3ld_1kb').remove();
        // $('#concept_vw1_sxj_xnb__section_cjg_wxj_xnb').remove();
        // $('#concept_vw1_sxj_xnb__section_ej1_xxj_xnb').remove();
        $('#wh_topic_toc').remove();
        // $('.wh_content_area').css('width', '100%');
        // $('#wh_topic_body').css('width', '70%');
        $('.cl_section:eq(0)').css('display', 'table-cell');
        $('.cl_section:eq(0)').css('padding-right', '20px');
        // $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('padding-right', '10px');
        $('.col-lg-7').css('width', '100%');
    }

    // InsertCurrentReleaseTitle();

    CreateHeadingParasAndLists();

    CreateNewToc();

    if (IsAuthoringChangelog) {
        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 content=$(this).attr("class").replace("page-", "");
        //get IDs of headings
        var EnhHeadingID = $('[id*="_ChangelogNavEnhancements"]').attr('id');
        var DefHeadingID = $('[id*="_ChangelogNavDefects"]').attr('id');

        if (IsDDChangelogPage || 
            IsEZGatewayChangelogPage || 
            IsPackAndPackChangelog || 
            IsWKHostedChangelog || 
            IsDGSChangelog || 
            IsDocumentPortal || 
            IsDocViewer) {
            if ($(this).parent().parent().attr('data-rev') == revLevel) {
                //get title of release note
                var title = $(this).find("th").text();
                var id = $(this).attr("id");
                //is it a new feature, enhancement or defect
                var TypeFullText = $(this).find("li").first().text();
                var trimType = $.trim(TypeFullText);
                var type = trimType.split(" ").pop();
            }
        } else {
            //get title of release note
            var title = $(this).find("th").text();
            var id = $(this).attr("id");
            //is it a new feature, enhancement or defect
            var TypeFullText = $(this).find("li").first().text();
            var trimType = $.trim(TypeFullText);
            var type = trimType.split(" ").pop();
        }



        if (type == "Feature") {
            //move to the Enhancements list
            $('#NewFeaturesList').append("<li>" +
                "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
        }

        if (type == "Enhancement") {
            //move to the Enhancements list
            // var div = document.querySelector('section')
            // if($(this).parent().attr('rev') == revLevel){
            $('#EnhancementsList').append("<li>" +
                "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            // }
            // if (div.closest(section[data-rev]) == revLevel){
            //     $('#EnhancementsList').append("<li>"+
            //         "<a style='color: black' href='#" + id + "'>"+title+"</a></li>");
            // }
        }

        if (type == "Defect" || type == "Correction") {
            //move to the Defects list
            $('#DefectsList').append("<li>" +
                "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
        }

        // $("#autoTOC").prepend("<li>"+
        //     "<a style='color: white' href='#" + id + "'>"+title+"</a></li>");

    });
}

function InsertCurrentReleaseTitle(){
    // Insert "Current Release" title in toc for
    if (IsPackAndPackChangelog || IsWKHostedChangelog) {
        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);
    });
}

