var IsChangelogPage = window.location.href.indexOf("changelog.html") != -1;

if (IsChangelogPage){
    $('.simpletable').each(function () {
        var x = Math.floor(Math.random() * 10000);
        $(this).find('.rntitle').first().addClass('parent').attr('id', x).css('border-bottom','0');
        // add type button
        var TypeFullText = $(this).find("li").first().text();
        var trimType = $.trim(TypeFullText);
        var type = trimType.split(" ").pop();
        var truncated = type.substr(0, 3);
        $(this).find('.rntitle').first().prepend ('<span class="clType ' + type + '">' + truncated + '</span');
        $(this).find('tbody > tr').each(function() {
            $(this).attr('class', ('child-' + x))
        });
    });
}

// if (IsAuthoring){
    $('th.parent')  
        .css("cursor", "pointer")  
        .attr("title", "Click to expand/collapse")  
        .click(function () {  
            var id = $(this).attr('id');
            var childId = 'child-' + id;
            $('.' + childId).toggle('slow');
        });
        
        
    $('.simpletable > tbody > tr[class*="child-"]').toggle();
    $('.-').css({'margin-top':'0'});
    $('section div.-').css({'display' : 'block'});
    $('.simpletable-container').css({'border-bottom' : 'none'});
    $('.wh_topic_toc').hide();

    // Create Changelog Tools from the clToc
    $('.clToc').empty();
    $( ".clToc" ).append( "<h4 id='CLToolsHeading'>Changelog Tools</h4>" +
    "<h5 id='CLToolsPara'>Filter</h5>" +
    "<div class='btn-group' id='FilterButtons' role='group' aria-label='Filter Buttons'>" +
    "<button type='button' class='btn btn-primary' title='Show only Feature release notes' onclick='ShowAllTables(); FilterFeature()'>Features</button>" +
    "<button type='button' class='btn btn-success' title='Show only Enhancement release notes'" +
        "onclick='ShowAllTables(); FilterEnhancement()'>Enhancements</button>" +
    "<button type='button' class='btn btn-danger' title='Show only Defects release notes' onclick='ShowAllTables(); FilterDefect()'>Defects</button>" +
    "<button type='button' class='btn btn-secondary' onclick='ShowAllTables()'>Reset</button>" +
    "</div>" +

    "<h5 id='CLToolsPara'>Folding</h5>" +
    "<div class='btn-group' id='FoldingButtons' role='group' aria-label='Folding Buttons'>" +
    "<button type='button' class='btn btn-primary' title='Show all release notes' onclick='ShowAll()'>" +
        "<i class='bi bi-chevron-double-down'></i> Show All</button>" +
    "<button type='button' class='btn btn-primary' title='Hide all release notes' onclick='HideAll()'>" +
        "<i class='bi bi-chevron-double-up'></i> Hide all</button>" +
    "<button type='button' class='btn btn-primary' data-html='true' title='Show hidden release notes, Hide visible release notes' onclick='ToggleParents()'>" +
        "<i class='bi bi-toggles'></i> Toggle all</button>" +
    "</div>" +
    "");
    $('.simpletable > tbody > tr > td').css({'padding-left':'40px'});
// }

function FilterFeature(){
    $('.rntitle').each(function () {
        var index = $(this).index();
        var text = $(this).closest('.simpletable').find('li:first').text();
        var n = text.indexOf('Feature');
        if (n === -1){
            // $(this).remove();
            $(this).closest('table').hide();
        }
    });
}

function FilterEnhancement(){
    $('.rntitle').each(function () {
        var index = $(this).index();
        var text = $(this).closest('.simpletable').find('li:first').text();
        var n = text.indexOf('Enhancement');
        if (n === -1){
            // $(this).remove();
            $(this).closest('table').hide();
        }
    });
}

function FilterDefect(){
    $('.rntitle').each(function () {
        var index = $(this).index();
        var text = $(this).closest('.simpletable').find('li:first').text();
        var n = text.indexOf('Defect');
        if (n === -1){
            // $(this).remove();
            $(this).closest('table').hide();
        }
    });
}

function ShowAllTables(){
    $('table').show();
}

function ToggleParents(){
    $('th.parent').each(function (index) {
        var id = $(this).attr('id');
        var childId = 'child-' + id;
        $('.' + childId).toggle('slow');
    })
}

function ShowAll(){
    $('th.parent').each(function (index) {
        var id = $(this).attr('id');
        var childId = 'child-' + id;
        $('.' + childId).animate( { "opacity": "show", top:"150"} , 1000 );
    })
}
function HideAll(){
    $('th.parent').each(function (index) {
        var id = $(this).attr('id');
        var childId = 'child-' + id;
        $('.' + childId).animate( { "opacity": "hide", top:"150"} , 800 );
    })
}
