//            ---------------------------------------------------------------------------------------------
//            The UNIVERSAL INTEGRATION ENDPOINT™ (UIE™) - © 2016 THRIVE COMMERCE, LLC - thrivecommerce.com
//            ---------------------------------------------------------------------------------------------
//
//                                                     HOW TO INTEGRATE
//                                                (TIME REQUIRED: 1 MINUTE)
//                                                -------------------------
// • The following one-time copy/paste typically takes 1 minute, DOES NOT REQUIRE YOUR TECH DEPT, and is live immediately.
//
// • Place the script tag below via your tag manager, such that it appears in the <head> section of every page on your site.
//                                     <script src="//integrate.thrive.today"></script>
//
// • Alternatively place it via your CMS or in your site template; if necessary, in the <body> (rather than <head>) section.
//
// • Initially has no impact, so it may be done at any time, even prior to finalizing your contract or Thrive product choices.
//
// • Thrive does all remaining integration work, which is to configure your account so that after your launch date, this
//   intelligent tag dynamically places the appropriate Thrive widgets and other functionalities on your site, invokes Google
//   to appropriately crawl and index the relevant pages, and does all appropriate tracking.
//
//                                                         SYNOPSIS
//                                                         --------
// 1. Bails if it's already been run (e.g., when accidentally included twice)
// 2. Defines some basic functions within a proprietary namespace (object), avoiding 3rd-party library dependencies altogether
// 3. Dynamically generates a pageview UUID & determines the effective URL for the pageview
// 4. Sets a tracking cookie
// 5. Calls the "dt.js" endpoint synchronously - executes any custom DOM Transformations configured for the parent page
// 6. Calls the "am.js" endpoint asynchronously - standard and custom Attribute Maps configured for the parent page

if ( !window[ "Tagac6adGhyaXZlLnRvZGF5" ] ) {
  window[ "Tagac6adGhyaXZlLnRvZGF5" ] = {};
  ( function () {
    var b64_digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    this.i_to_b64 = function(i) {
      var b64 = ''
      while (i>0) {
        b64 = b64_digits[i%64]+b64
        i = Math.floor(i/64)
      }
      return b64
    }
    this.urlsafe_b64_encode = function(object) {
      return btoa(JSON.stringify(object)).replace(/\+/g,'-').replace(/\//g,'_').replace(/\=+$/,'')
    }
    this.generate_uuid = function(msec) {
      var b = ''
      for (var i=0; i<33; i++) { b += String.fromCharCode(Math.floor((Math.random()) * 256)) }
      return ('AAAAAAAAAAAA'+this.i_to_b64(msec)).slice(-12) +
        ('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'+this.urlsafe_b64_encode(b)).slice(-44)
    }
    this.get_cookie = function(key) {
      if ( match = ( new RegExp('(?:^|; )'+encodeURIComponent(key)+'=([^;]*)') ).exec(document.cookie) ) {
        return decodeURIComponent(match[1])
      }
    }
    this.set_cookie = function(key, value, domain, expiration) {
      document.cookie = encodeURIComponent(key)+'='+encodeURIComponent(value)+'; path=/;' +
        (domain ? (' domain='+encodeURIComponent(domain)+';') : '') +
        (expiration ? (' expires='+expiration+';') : '')
      return this.get_cookie(key)
    }
    this.add_script = function(src, async) {
      var new_node = document.createElement('script')
      new_node.src = src
      new_node.async = async
      var selector = "script[src^=\\/\\/integrate\\.thrive\\.today], script[src^=\\/\\/gte\\.thrive\\.today]"
      var uie_spawned_script_tags = document.querySelectorAll(selector)
      var insertion_location
      if (uie_spawned_script_tags.length>0) {
        insertion_location = uie_spawned_script_tags[uie_spawned_script_tags.length-1]
        insertion_location.parentNode.insertBefore(new_node, insertion_location.nextSibling)
      } else {
        document.querySelector('head').appendChild(new_node)
      }
    }

    if ( !this.get_cookie("va6uyAb") ) {
      this.set_cookie(
        "va6uyAb",
        "AAAAAVbtyM9ey6TBNLgnChYCDYI2P65w5b7GAhElphQRPGOCM7xvYgH5",
        (function() {
          var key = "487fecfaae2c4397"
          var top_cookieable_domain = null
          var try_domain = '..' + window.domain
          while ( try_domain.match(/^\.[^\.]*\./) ) {
            try_domain = try_domain.replace(/^\.[^\.]*/, '')
            if ( this.set_cookie(key, '1', try_domain) ) {
              top_cookieable_domain = try_domain
            }
            document.cookie = encodeURIComponent(key)+'=; path=/;'+
              ' domain='+encodeURIComponent(try_domain)+';'+
              ' expires=Thu, 01 Jan 1970 00:00:00 GMT;'
          }
          return top_cookieable_domain
        }).call(window["Tagac6adGhyaXZlLnRvZGF5"]),
        'Wed, 01 Jan 2070 00:00:00 GMT'
      )
    }

    this.pv_uuid = this.generate_uuid( this.pv_msec = ( new Date()).getTime() )
    var effective_url_b64 = this.urlsafe_b64_encode(window.preview_Tagac6adGhyaXZlLnRvZGF5 || window.location.href)
    this.new_nodes = {}
    this.observers = {}
    this.functions = {}

    this.add_script("//integrate.thrive.today/"+this.pv_uuid+'/'+effective_url_b64+'/dt.js', 0)
    this.add_script("//integrate.thrive.today/"+this.pv_uuid+'/'+effective_url_b64+'/am.js', 1)
  } ).call( window[ "Tagac6adGhyaXZlLnRvZGF5" ] )
}
