// ========== DYNAMIC PROFILING
// This code provides the functionality for webhelp that relies on InfoDev's Dynamic Profiling.

var IsAuthRN 	        = window.location.href.indexOf("Authoring/ReleaseNotes") != -1;
var IsEZConfig 	       = window.location.href.indexOf("ExpereEZConfig") != -1;
var IsEZConfigTest  = window.location.href.indexOf("EZConfigTest") != -1;
var IsOPPSA 		     = window.location.href.indexOf("OPPSA") != -1;
var IsAudRev 	        = window.location.href.indexOf("AudRevTest") != -1;
var IsOAuth 	         = window.location.href.indexOf("OAuth") != -1;
var IsReqUG 	         = window.location.href.indexOf("RequirementsEditor") != -1;
// var searchOut                  = window.location.href.indexOf("/out/") != -1;
var IsDDTest             = window.location.href.indexOf("Test/DefaultData") != -1;

if ( IsAuthRN || IsEZConfig || IsEZConfigTest || IsOPPSA || IsAudRev || 
    IsReqUG || IsOAuth || IsDDTest) {

    var currentPageName = getBaseName(window.location.href) + ".html";  // NEW CODE 2019-08-21


    // @ts-ignore
    var audience = getURLParameter('audience');
    // @ts-ignore
    var rev = getURLParameter('rev');
    // console.log("audience = " + audience + ", rev = " + rev);
    $('[data-audience]').css('display', 'none');
    $('[data-rev]').css('display', 'none');


    if( audience !== 'installed' && audience !== 'hosted'){
       
    }

    else {
        
        if ( audience == 'installed' ) {
            $('[data-audience=installed]').css('display', 'inline');
            $('[data-audience=hosted]').remove(); 
        }
        else if ( audience == 'hosted' ) {
            $('[data-audience=hosted]').css('display', 'inline'); 
            $('[data-audience=installed]').remove();
        }

        $('*').each(function() {
            var curAud = ($(this).data('audience'));
            if ($(this).is('table')) {
                $(this).css('display', 'inline-table');
            } else {
                // $(this).css('display', 'inline-block');
            }
        });

        // Place correct variable syntax in links only if domain is from WKFS
        $('a').each(function(){
            var oHref = $(this).attr('href');
            var HrefSplit = oHref.split('/'); //Split the href text into sections split by forward slash
            var HrefDomain = HrefSplit[2]; //Get the 2nd instance of the sections

            oHref = oHref + "?";

            // If the href domain is from our server...
            if (HrefDomain == null || HrefDomain == "documentation.wolterskluwerfs.com") {
                // console.log("audience = " + audience);
                if( audience == null ){
                    // console.log("audience is null");
                }
                else{
                    // console.log("audience is NOT null");
                    var tHref = oHref + "audience=" + audience;
                }
                
                $(this).attr('href', tHref);
            }
        })
        sessionStorage.setItem('audience', audience);
        
    }

    if (rev === undefined) {
        $('*').each(function() {
            var curRev = ($(this).data('rev'));
            if (curRev != undefined){
                $(this).closest('.wh_main_page_toc_accordion_header').remove();
                $(this).closest('.wh_main_page_toc_accordion_entries').remove();
                $(this).closest('.wh_main_page_toc_entry').remove();
            }
        });
    } else {
        // Place correct variable syntax in links
        $('a').each(function(){
            var oHref = $(this).attr('href');
            var HrefSplit = oHref.split('/'); //Split the href text into sections split by forward slash
            var HrefDomain = HrefSplit[2]; //Get the 2nd instance of the sections

            // If the href domain is from our server...
            // If the href "#ezconfig_changelog-entries__simpletable_qsk_fzl_fhb" can be identified as on the same page as the URL location: 
            // https://dev-documentation.wolterskluwerfs.com/ExpereEZConfig/PackagesAndPackets/ChangeLog/ezconfig_changelog-entries.html
            if (HrefDomain == null || HrefDomain == "documentation.wolterskluwerfs.com") {
                var tHref = oHref ;
                var AudVariableForCombo;
                var RevVariableForCombo;
                var VariableCombo;
                // console.log("tHref = " + tHref);
                // console.log("audience = " + audience);
                // console.log("rev = " + rev);

                if (audience === "" || audience === null || audience === undefined || audience === "null") {
                    AudVariableForCombo = 1
                }
                else{
                    AudVariableForCombo = 3
                }

                if (rev === "" || rev === null || rev === undefined || rev === "null") {
                    RevVariableForCombo = 5
                }
                else{
                    RevVariableForCombo = 8
                }

                VariableCombo = AudVariableForCombo + RevVariableForCombo;
                // console.log("AudVariableForCombo = " + AudVariableForCombo);
                // console.log("RevVariableForCombo = " + RevVariableForCombo);
                // console.log("VariableCombo = " +VariableCombo);

                switch(VariableCombo){
                    case 6:  //aud IS null, rev IS null
                        // console.log("aud IS null, rev IS null");
                        var tHref = tHref;
                        break;
                    case 8:  //aud is NOT null, rev IS null
                        // console.log("aud is NOT null, rev IS null");
                        var tHref = tHref;
                        break;
                    case 9:  //aud IS null, rev is NOT null
                        // console.log("aud IS null, rev is NOT null");
                        var tHref = "?rev=" + rev;
                        break;
                    case 11:  //aud is NOT null, rev is NOT null
                        // console.log("aud is NOT null, rev is NOT null");
                        var tHref = tHref + "&rev=" + rev;
                        break;
                }
            }
            $(this).attr('href', tHref);
        });



        // Show all data-rev that are less than or equal to rev
        $('*').each(function() {
            var curRev = ($(this).data('rev'));

            var revString = rev.toString();
            var result = revString.substr(0,5) + "0" + revString.substr(5);

            
            if (curRev != undefined) {
                // ===== MODIFY rev IF NECESSARY =====
                //Get variables of build numbers for sorting
                var year = rev.substr(0,4);
                var relMaj = rev.substr(5,2);
                if (relMaj.includes(".")){
                    var relMin = rev.substr(7,1);
                    var build = rev.substr(9,4);
                }
                else {
                    var relMin = rev.substr(8,1);
                    var build = rev.substr(10,4);
                }
                
                //if Release-Major contains only one number add a 0 to the front
                if (relMaj.includes(".")){
                    relMaj = relMaj.slice(0,-1);
                    relMaj = "0" + relMaj;
                    // rev = year + "." + relMaj + "." + relMin + "." + build;
                    rev = year +  relMaj +  relMin + build;
                }
                var revNoDots = parseInt(rev.split(".").join(""));



                 // ===== MODIFY CURREV IF NECESSARY =====
                var curRevYear = curRev.substr(0,4);
                var curRevMaj = curRev.substr(5,2);
                if (curRevMaj.includes(".")){
                    var curRevMin = curRev.substr(7,1);
                    var curRevbuild = curRev.substr(9,4);
                }
                else {
                    var curRevMin = curRev.substr(8,1);
                    var curRevbuild = curRev.substr(10,4);
                }
                
                //if Release-Major contains only one number add a 0 to the front
                if (curRevMaj.includes(".")){
                    curRevMaj = curRevMaj.slice(0,-1);
                    curRevMaj = "0" + curRevMaj;
                    curRev = curRevYear + "." + curRevMaj + "." + curRevMin + "." + curRevbuild;
                }
                var curRevNoDots = parseInt(curRev.split(".").join(""));
                // $(this).attr('rev',curRevNoDots);


                if (curRevNoDots <= revNoDots) {
                        if ($(this).is('section') || $(this).is('p')) {
                            $(this).css('display', 'block');
                        } else if ($(this).is('li')){
                            ($(this).css('display', 'list-item'));
                        } else if ($(this).is('tr')){
                                ($(this).css('display', 'table-row'));
                        } else {
                            $(this).css('display', 'inline-block');
                        }
                    }
                }
        });

        $(".wh_main_page_toc_accordion_header").find("span:empty").closest('.wh_main_page_toc_accordion_header').remove;
        // $("a").find("display:none").closest('.wh_main_page_toc_accordion_header').remove();
        $("a[style$='display: none;']").closest('.wh_main_page_toc_accordion_header').remove();

        sessionStorage.setItem('rev',rev);
    }



    //Remove all non-displayed sections
    $('section').each(function(){
        if($(this).css('display') == 'none'){
            $(this).remove();
        }
    });

    
    //remove all href variables if the href contains the page name
    $('a').each(function(){
        if ($(this).parents('#wh_publication_toc')){
            //Do nothing. This ensures that left nav links still contain the url variables.
        }
        else{
            var currentHref = $(this).attr('href');
            if (currentHref.indexOf(currentPageName) == -1){ 
                 //does NOT contain page name INCLUDING EXTENSION
            }
            else{
                var variablesToRemove = "?audience=" + audience + "&rev=" + rev;
                var newHref = currentHref.replace(variablesToRemove, '');
                $(this).attr('href', newHref);
            }
        };
        
    });


    // hide topic toc
    $('#wh_topic_toc').css('display', 'none');
    // make topic body wider
    // $('#wh_topic_body').css('width', '100%');
    $('#wh_topic_content').css('margin-left', '40px');
    $('#wh_topic_content').css('margin-right', '40px');

    

};




function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}



// FUNCTION IS NEW CODE 2019-08-21
function getBaseName(url){
    if(!url || (url && url.length === 0)) {
        return "";
        }
    var index = url.lastIndexOf("/") + 1;
    var filenameWithExtension = url.substr(index);
    var basename = filenameWithExtension.split(/[.?&#]+/)[0];

    // Handle '/mypage/' type paths
    if(basename.length === 0) {
    url = url.substr(0,index-1);
    basename = getBaseName(url);
    }
    return basename ? basename : ""; 
}