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

var searchEZConfig 	  = window.location.href.indexOf("ExpereEZConfig") != -1;
var searchOPPSA 		= window.location.href.indexOf("OPPSA") != -1;
var searchAudRev 	   = window.location.href.indexOf("AudRevTest") != -1;
var searchOut             = window.location.href.indexOf("/out/") != -1;

if ( searchEZConfig || searchOPPSA || searchAudRev || searchOut ) {
    var audienceExists = $.getUrlVar('audience');
    var revExists = $.getUrlVar('rev');
    $('[data-audience]').css('display', 'none');
    $('[data-rev]').css('display', 'none');

    // console.log("audience = " + audienceExists + " / rev = " + revExists);
    
    if (audienceExists === undefined) {

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

        $('*').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

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


    if (revExists === undefined) {
    } 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 not from our server...
            if (HrefDomain == null || HrefDomain == "documentation.wolterskluwerfs.com") {
                var tHref = oHref ;
                if (!audienceExists) {
                    var tHref = tHref + "?rev=" + revExists;
                } else {
                    var tHref = tHref + "&rev=" + revExists;
                }
            }
            $(this).attr('href', tHref);
        })

        // Show all data-rev that are less than or equal to revExists
        $('*').each(function() {
            var curRev = ($(this).data('rev'));
            if (curRev <= revExists) {
                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');
                }
            }
            if (curRev > revExists) {
                ($(this).remove());
            }
        });

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

    // 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');
}
