window.DTLSiteFeatureEnabled = true;
window.load_adobe_analytics = {
    usingLaunch: true,
};
window.staticContentRoot = "https://cdn.staticsfly.com/febridge/builds/febridge-20180925-131-1f41e73f-58d5d/";
window.SatelliteLibURL = "//assets.adobedtm.com/528526b3f782/ddfab9399b13/launch-1480b4cd5321.min.js";
window.BaseADTMD = {
    noClobber: true,
    env: 'PROD',
    session: {
        partner: {
            id: 'SFLY',
            subId: 'WEB',
        },
        visitorDetails: {
            visitorType: 'prospect',
            customerId: ''
        }
    },
    page: {
        pageInfo: {
            siteExperience: 'd.web'
        }
    },
};
window.$omnitureDTL = {

// Get session information that's reported to omniture.
// returns a JS object containing:

// session.visitorDetails.customerId
// session.visitorDetails.visitorType
// session.visitorDetails.visitorId
// session.partner.id
// session.partner.subId
// page.pageInfo.storeType
// page.pageInfo.siteExperience
// page.pageInfo.siteSection

// Gets session.partner.id, session.partner.subid, session.visitorDetails.customerId from getSessionUserInfo.
// visitorType is determined by whether the customer is logged in or not.
// visitorId is pulled from browser session.
// page.pageInfo.storeType is shutterfly or tinyprints
// page.pageInfo.siteExperience is d.web, m.web, or t.web and is determined by user device type (isMobile, isTable)
// page.pageInfo.siteSection must be passed in, it's "store" or "photobooks" or something.

// Removes all variables in list from the DTL layer
// propertyList is a list of names for example [ "event", "session.partner" ]
clearDTLVars: function(propertyList) {

    if (typeof (propertyList) === 'undefined' ||
        typeof(window.SFLY_DTL) === 'undefined') {
        window.SFLY_DTL = {};
        return;
    }

    function traceContext(context, searchObject) {
        var propertyName= context,
            delimiterPosition = context.indexOf('.');
        var tracedObject = searchObject;

        if (delimiterPosition > -1) {
            propertyName = context.slice(0, delimiterPosition);
        }

        //validate context
        if (searchObject.hasOwnProperty(propertyName) == false) 
            return tracedObject;
        

        if (context.indexOf('.') > -1) {
            if (typeof (searchObject[propertyName]) == "object") {
                tracedObject[propertyName] = traceContext(context.slice(delimiterPosition + 1, context.length), searchObject[propertyName]);
            }
        } else {
            delete searchObject[context];
        }

        return tracedObject;
    }

    if (Array.isArray(propertyList)) {
        propertyList.forEach(function(context) {
            traceContext(context, window.SFLY_DTL);
        });
        return;
    }

    //path will never be executed due to type requirement but that may change that
    if (typeof(propertyList) === 'string') {
        traceContext(propertyList, window.SFLY_DTL);
        return;
    }
},

// Recursively & destructively merge properties obj2 onto obj1
_merge: function(obj1, obj2) {

    if (typeof(obj2) === 'undefined')
        return obj1;

    if (typeof(obj1) === 'undefined')
        return obj2;

    for (var p in obj2) {
        // Property in destination object set; update its value.
        if (obj2[p] && obj2[p].constructor == Object) {
            if (typeof(obj1[p]) === 'undefined' || obj1[p].constructor !== Object)
                obj1[p] = obj2[p];
            else
                obj1[p] = window.$omnitureDTL._merge(obj1[p], obj2[p]);
        } else {
            obj1[p] = obj2[p];
        }
    }

    return obj1;
},

// Initialize omniture DTL.
setupOmnitureDTLTracking: function(appServerUrl, callback) {
     callback(true);
},

// Used to load user session info but that (except visitorid) comes in from OmnitureDTLServlet.java now.
loadOmnitureSession: function(appServerUrl, onSuccess, onFailure) {

    if (typeof(window.$omnitureDTL.sessionInfo) === 'undefined') {

          window.$omnitureDTL.sessionInfo = window.$omnitureDTL.clone(window.BaseADTMD);
          window.$omnitureDTL.sessionInfo.session.visitorDetails.visitorId =
            (window.$omnitureDTL.sessionInfo.session.visitorDetails.visitorType === 'signedIn') ?
              '' : window.$omnitureDTL.getVisitorId();
    }

    onSuccess(window.$omnitureDTL.sessionInfo);
    return;
},

clone: function(obj) {
    // Handle the 3 simple types, and null or undefined
    if (null == obj || "object" != typeof obj) return obj;

    // Handle Date
    if (obj instanceof Date) {
        var copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }

    // Handle Array
    if (obj instanceof Array) {
        var copy = [];
        for (var i = 0, len = obj.length; i < len; i++) {
            copy[i] = window.$omnitureDTL.clone(obj[i]);
        }
        return copy;
    }

    // Handle Object
    if (obj instanceof Object) {
        var copy = {};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = window.$omnitureDTL.clone(obj[attr]);
        }
        return copy;
    }

    throw new Error("Unable to copy obj! Its type isn't supported.");
},

// Scrape cookie for visitor id.  The name of the cookie depends on the environment.
getVisitorId: function() {
    if (typeof(window.$omnitureDTL.visitorId) !== 'undefined')
        return window.$omnitureDTL.visitorId;

    var cookie = document.cookie;
    var env = window.BaseADTMD.env;
    if (typeof(env) === 'undefined') {
        return  '';
    }
    env = env.toLowerCase();
    var pattern = 'visitor="';
    if (env !== 'prod')
        pattern = 'visitor_' + env + '="';
    var l = cookie.indexOf(pattern);
    if (l == -1)
        return window.$omnitureDTL.visitorId = '';
    var result = cookie.substring(l + pattern.length);
    result = result.substring(0, result.indexOf('"'));
    return window.$omnitureDTL.visitorId = result;
},

siteFeatures: {},

// call e.g. https://www.beta.shutterfly.com/rest/sitefeature/adobeDTM.siteCatalyst.DTL
// to read the value of the named site feature
getSiteFeature: function(name, onSuccess, onFailure) {
    if (typeof (window.$omnitureDTL.siteFeatures[name]) !== 'undefined')
        onSuccess(window.$omnitureDTL.siteFeatures[name]);
    
},

// Call the callback.  Not needed anymore.
processOmnitureJSPData: function(callback) {
    if (typeof(callback) === 'function')
        callback(); 
},

putDtlVar: function(propertyName, value, doNotClobber) {

    // places value inside of the namespace
    function makeContext(context, parentObject) {
        if (context != '') { // context is not a root
            var objectName = context;
            var returnObject = parentObject;
            var delimiterPosition = context.indexOf('.');

            if (delimiterPosition > -1) {
                objectName = context.slice(0, delimiterPosition);
                parentObject[objectName] = {};
                returnObject = makeContext(context.slice(delimiterPosition + 1, context.length), parentObject[objectName]);
            } else {
                parentObject[objectName] = value;
            }
        } else { //context is point to root
            if (typeof (value) == 'object') { // if the value is an object than return this as the object
                parentObject = value;
            } else {
                console.error('putDtlVar: root object was specified as context but value was not an object.');
            }
        }

        return parentObject;
    }

    var newObject = makeContext(propertyName, {});
    if (doNotClobber) {
        window.SFLY_DTL = window.$omnitureDTL._merge(newObject, window.SFLY_DTL);
    } else {
        window.SFLY_DTL = window.$omnitureDTL._merge(window.SFLY_DTL, newObject);
    }
},

// Main entry point to merge data into SFLY_DTL
putOmnitureJSPData: function(appServerUrl, data) {

    var context = '';
    var noClobber = false;

    if (!!data.context) {
        context = data.context;
        delete data.context;
    }

    if (!!data.noClobber) {
        noClobber = data.noClobber;
        delete data.noClobber;
    }

    // Do not merge together new event data with old event data
    if (typeof(data.event) !== 'undefined') {
        window.$omnitureDTL.clearDTLVars(['event']);
    }

    // With adobe Launch we push the event.
    if (window.load_adobe_analytics &&
        window.load_adobe_analytics.usingLaunch &&
        typeof(_satellite) !== 'undefined' &&
        data && data.event && data.event.eventInfo) {
        window.$omnitureDTL.putDtlVar(context, data, noClobber);
        _satellite.track(data.event.eventInfo.eventName, window.SFLY_DTL);
        return;
    }

    window.$omnitureDTL.putDtlVar(context, data, noClobber);
    if (window.localStorage.getItem('enable_dtl_logging') === 'true') {
        console.log(JSON.stringify(window.SFLY_DTL));
    }
},




removeOmnitureJSPData: function(appServerUrl, jspData) {
    window.$omnitureDTL.clearDTLVars([jspData]);
},

// Send data into bridge
fillDTLBuffer: function(jspData) {
    window.$omnitureDTL.putOmnitureJSPData(jspData);
    // env is just for our internal use, Janay does not want it reported.
    delete window.SFLY_DTL.env;
}

}
