// changelog-filter.js
var ThisIsAuthoringChanglog 	    = window.location.href.indexOf("Expere/Authoring/ReleaseNotes") != -1;
var thisIsAuthoringChangelogHome    = window.location.href.indexOf("/Expere/Authoring/ReleaseNotes/webhelp/index.html") != -1;
var IsDGSChangelog                  = window.location.href.indexOf("DGS/Change-Log") != -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 DefaultText = "Enter text to start filtering";

if ( ThisIsAuthoringChanglog || (IsWKHostedChangelog && (!IsWKHostedOverview)) || IsDGSChangelog ) {

    // Remove the wh_publication_toc (the left nav)
    $('nav#wh_publication_toc').remove();
    

    //Change width of content to fill page
    $('.wh_topic_content').parent().removeClass('col-lg-9').removeClass('col-md-9');
    

    // Remove  the DDV Changelog entry on the home page
    if (thisIsAuthoringChangelogHome){
        // $('a[href*="/exp_DDV_changelog.html/"').closest('.wh_main_page_toc_entry').hide();
        $('a[href*="exp_DDV_changelog.html"]').each(function() {
            $(this).closest('.wh_main_page_toc_entry').remove();
        });
    }

    // Create input box and buttons
    //Within wh_tools toolbar
    $('<div class="FilterContainer">' + 
        '<strong id="FilterIntro" class="ph">Filter this page</strong>' + 
        '<div id="FilterControls">' +
            '<input type="text" id="filterText" value="' + DefaultText + '" />' + 
            // '<input type="button" id="filterButton" value="Filter" />' + 
            '<input type="button" id="clearButton" value="Clear" />' + 
        '</div>' +
        '</div>').appendTo($(".wh_tools"));
    $('.FilterContainer').css({
        'position':'absolute',
        'right':'300px',
        'margin-top' : '4px',
        'border' : '1px solid rgba(0, 0, 0, 0.2)',
        'border-radius' : '6px',
        'display' : 'flex',
        'align-items' : 'center'
    });
    $('#FilterControls').css({
        'margin-top':'-10px',
        'margin-bottom' : '4px'
    });
    // var $input = $('<input type="button" id="clearButton" value="Clear" /><hr style=" border-color: #d9edf7;" />');
    // $input.appendTo($(".FilterContainer"));


    //Location within TOC div
    // var $input = $('<input type="button" id="clearButton" value="Clear" /><hr style=" border-color: #d9edf7;" />');
    // $input.prependTo($(".ChangelogNav"));
    // var $input = $('<input type="button" id="filterButton" value="Filter" />');
    // $input.prependTo($(".ChangelogNav"));
    // var $input = $('<input type="text" id="filterText" value="Enter text (case-sensitive)" />');
    // $input.prependTo($(".ChangelogNav"));
    // var $IntroText = $('<strong id="FilterIntro" class="ph b">Filter this page</strong>');
    // $IntroText.prependTo($(".ChangelogNav"));
    $('#filterText').css({
        'color' : 'black',
        'font-size' : 'smaller',
        'width' : '160px',
        'padding': '0 6px 0 6px',
        'margin-right' : '5px',
        'border' : 'none'});
    $('#filterButton').css({
        'color' : 'black',
        'font-size' : 'smaller',
        'margin-right' : '2px',
        'border-radius' : '6px',
        'border' : 'none',
        'cursor' : 'pointer'});
    $('#clearButton').css({
        'color' : 'black',
        'font-size' : 'smaller',
        'border-radius' : '6px',
        'border' : 'none',
        'cursor' : 'pointer'});
    $('#FilterIntro').css({
        'color' : '#6d6d6d',
        'margin-right' : '10px',
        'margin-left' : '4px'});


    //Select all of the input box when it gets the focus
    $('#filterText').focus(function() {
        $(this).select();
     });

    // Change the text to uppercase when a key is pressed
    // $('#filterText').keyup(function(){
    //     $(this).val( $(this).val().toUpperCase() );
    //   });

    
    // $('#filterText').keypress(function(e) {
    //     if(e.which == 8 || e.which == 46){
    //         // BACKSPACE or DEL
    //         $("#clearButton").click();
    //         $("#filterButton").click();
    //     }
    //     if(e.which == 13) {
    //         //ENTER
    //         //if the text box is NOT empty, click the filter button
    //         if ( $('#filterText').val() != '' ) {
    //             $("#filterButton").click();
    //         }
    //         else{
    //             $("#clearButton").click();
    //         }
    //     }
    // });


    $('#filterText').keyup(function(e) {      

        if (event.key == "Escape") {
            //show tables
            $('table').each(function (){
                $(this).show();
            });

            //show list items
            $('li').each(function (){
                $(this).show();
            });

            //clear text box
            $('#filterText').val('Enter text (case-sensitive)');
            $('#filterText').select();
        }
        else {
                    // create the regular expression
        var regEx = new RegExp($.map($(this).val().trim().split(' '), function(v) {
            return '(?=.*?' + v + ')';
        }).join(''), 'i');
        
        // select all list items, hide and filter by the regex then show
        $('#NewFeaturesList li').hide().filter(function() {
            return regEx.exec($(this).text());
        }).show();

        $('#EnhancementsList li').hide().filter(function() {
            return regEx.exec($(this).text());
        }).show();
        
        $('#DefectsList li').hide().filter(function() {
            return regEx.exec($(this).text());
        }).show();
        
        $('.rntitle').closest('.simpletable').hide().filter(function() {
            return regEx.exec($(this).text());
        }).show();

        }

    });


    // $("#filterText").on("change keyup paste", function(){
    //     var filterString = $('#filterText').val();

    //     //==== Do the filtering ====

    //     // filter the list items
    //     $('#EnhancementsList>li>a').each(function () {
    //         var index = $(this).index();
    //         var text = $(this).text();
    //         var n = text.includes(filterString);

    //         if (n == false){
    //             // $(this).remove();
    //             $(this).closest('li').hide();
    //         }
    //     });
    //     $('#DefectsList>li>a').each(function () {
    //         var index = $(this).index();
    //         var text = $(this).text();
    //         var n = text.includes(filterString);

    //         if (n == false){
    //             // $(this).remove();
    //             $(this).closest('li').hide();
    //         }
    //     });

    //     // filter the tables
    //     $('.rntitle').each(function () {
    //         var index = $(this).index();
    //         var text = $(this).text();
    //         var n = text.includes(filterString);

    //         if (n == false){
    //             // $(this).remove();
    //             $(this).closest('table').hide();
    //         }
    //     });
    // })


    $('#clearButton').click(function(){
        //show tables
        $('table').each(function (){
            $(this).show();
        });

        //show list items
        $('li').each(function (){
            $(this).show();
        });

        //clear text box
        $('#filterText').val(DefaultText);
        $('#filterText').select();
    });


    
    $('#filterButton').click(function(){
        var filterString = $('#filterText').val();

        //==== Do the filtering ====

        // filter the list items
        $('#EnhancementsList>li>a').each(function () {
            var index = $(this).index();
            var text = $(this).text();
            var n = text.startsWith(filterString);

            if (n == false){
                // $(this).remove();
                $(this).closest('li').hide();
            }
        });
        $('#DefectsList>li>a').each(function () {
            var index = $(this).index();
            var text = $(this).text();
            var n = text.startsWith(filterString);

            if (n == false){
                // $(this).remove();
                $(this).closest('li').hide();
            }
        });

        // filter the tables
        $('.rntitle').each(function () {
            var index = $(this).index();
            var text = $(this).text();
            var n = text.startsWith(filterString);

            if (n == false){
                // $(this).remove();
                $(this).closest('table').hide();
            }
        });



      });




}
