///////////////////////////////////////////////////////////////
//    Global Variables
//////////////////////////////////////////////////////////////

const app = {
    siteID: 'bcbstprovider',          // value to determine site is pulled in for analytics
    siteName: 'PROVIDER',             // Used to name items in tagging
    devEnvironments: ['anwas65.bcbst.com', 'provider-dev.bcbst.com'],
    testEnvironments: ['andxtst03', 'provider-test.bcbst.com', 'provider-rlse.bcbst.com'],
    prodEnvironments: ['provider.bcbst.com'],
    hostname: location.hostname,      // Ex: www.bcbst.com
    pathname: location.pathname,      // Ex: /use-insurance/documents-forms
    urlBreadcrumb: '',                // Ex: use insurance > documents forms
    siteSection: '',                  // Ex: use insurance
    sitePage: '',                     // Ex: documents forms
    searchParameter: '',
    searchResultsNumber: ''
}





///////////////////////////////////////////////////////////////
//    FIX: Remove Portal Toolbar PROD/RLSE
//////////////////////////////////////////////////////////////
$(document).ready(function () {
    const width = $('body').width();
    if (app.testEnvironments.includes(app.hostname) || app.prodEnvironments.includes(app.hostname)) {
        // $('.stToolbar').css('display', 'none');
        if (width > 1000) {
            $('.stMainContent').css('padding-top','88px');
            $('.login-form').css('top','91px');
        } else {
            $('.stMainContent').css('padding-top','58px');
        }
    }
});






///////////////////////////////////////////////////////////////
//    Page View Tag
//////////////////////////////////////////////////////////////

// Finding app.urlBreadcrumb, app.siteSection, and app.sitePage variables based on the app.pathname variable
if (app.pathname === '' || app.pathname === '/' || app.pathname === '/index/') {
    app.urlBreadcrumb = 'homepage',
    app.siteSection = 'index',
    app.sitePage = 'homepage';
} else {
    if (app.pathname.indexOf('.') !== -1) {
        app.urlBreadcrumb = app.pathname.substr(1, app.pathname.indexOf('.') - 1).replace(/\//g, ' > ').replace(/-/g, ' ');
    } else {
        app.urlBreadcrumb = app.pathname.substr(1, app.pathname.length).replace(/\//g, ' > ').replace(/-/g, ' ');
    }
    if (app.urlBreadcrumb.indexOf('>') !== -1) {
        app.siteSection = app.urlBreadcrumb.substr(0, app.urlBreadcrumb.indexOf(' >')),
        app.sitePage = app.urlBreadcrumb.substr(app.urlBreadcrumb.indexOf(' > ') + 3, app.urlBreadcrumb.length);
    } else {
        app.siteSection = app.urlBreadcrumb;
        app.sitePage = 'No Sub-Group';
    }
    if (app.sitePage.indexOf('>') !== -1) {
        app.sitePage = app.sitePage.substr(0, app.sitePage.indexOf(' >'));
    }
}

// Sending client IDs to proper environment (only have TEST and PROD)
if (app.prodEnvironments.includes(app.hostname)) {
    //Send to PROD data collection
    cmSetClientID('53240000|' + app.prodEnvironments[0], true, 'data.coremetrics.com', app.prodEnvironments[0]);
    cmSetupCookieMigration(true, true, 'consumer.bcbst.websales.guidewellconnect.com');
} else if (app.devEnvironments.includes(app.hostname) || app.testEnvironments.includes(app.hostname)) {
    //Send to TEST data collection
    cmSetClientID('83240000|' + app.prodEnvironments[0], false, 'testdata.coremetrics.com', app.prodEnvironments[0]);
    cmSetupCookieMigration(true, true, 'consumerapply.stga.bcbst.websales.guidewellconnect.com');
}
cmSetupOther({
    'cm_JSFEAMasterIDSessionCookie': true,
    'cm_TrackImpressions': ''
});


// Finding search results variables on the search page
if (app.pathname.indexOf('/searchresults') > -1) {
    app.searchParameter = $('#returnedOutput').text().split(' ')[1];
    app.searchResultsNumber = $('#returnedOutput').text().split("'")[1];
}


// CORE Pageview tag code 
// **** this is what pulls everything together and analytics into the site
// **** pageInstanceID: depending on the text here, Acoustic will send different client data. For this reason, there is typically only 1 pageInstanceID per domain
// - Search analytics tagging within search wcm code itself to gather the onsiteSearchTerm and onsiteSearchResults
// - primaryCategory comes from the app.siteSection variable determined above 
// - exploreAttributes is where we add any additional attributes/info that digital wants to add. Adding attributes:
//    > create an 'attributes' vaiable and replace the ''. All attributes should be put into a string that gets pulled in by this variable.
//    > Any unused attributes within the attributes are designated with: -_-
//    > Example: Adding info to attribute 6 & 7 would look like: attributes = '-_--_--_--_--_-' + attributeSix + '-_-' + attributeSeven;

digitalData = {
    page: {
        pageInfo: {
            pageID: app.siteName + ':' + cmGetDefaultPageID(),
            onsiteSearchTerm: app.searchParameter,
            onsiteSearchResults: app.searchResultsNumber
        },
        category: {
            primaryCategory: app.siteName + '|' + app.siteSection.toUpperCase()
        },
        attributes: {
            exploreAttributes: '',
            extraFields: ''
        }
    },
    pageInstanceID: app.siteID
};


$(document).ready(function(){

  let hash = window.location.hash; 
  let lowercase = hash.toLowerCase();
  if (lowercase.endsWith("#three-up-links?cm_sp=friendly-_-friendly-_-provider_coverage")) {
    $('html, body').animate({
      scrollTop: $("#three-up-links").offset().top-130
    }, 100);
  }

    ///////////////////////////////////////////////////////////////
    //    PDF Link Analytics
    //////////////////////////////////////////////////////////////

    $('a').on('click', function(event){
        const pdfURL = $(event.target).attr('href');
        if (pdfURL === undefined || pdfURL.indexOf('javascript') > -1) {
            
        } else if (pdfURL.indexOf('.pdf') > -1) {
            const pdfString = pdfURL.substring(pdfURL.lastIndexOf('/'));
            const getTitle = pdfString.slice(1,-4);
            cmCreateElementTag((app.siteName + '|PDF: ' + getTitle).substring(0,35), 'PDF Download');
        }  
    });
});