// +------------------------------------------------------------+
// | CLASSES                                                    |
// +------------------------------------------------------------+

function _t_ct_pt(id, ctDomain, aid)
{
    this.version = 'v2';
    this.id      = id;
    this.ctDomain  = ctDomain;
    this.aid     = aid;
    this.key     = null;
    this.referer = encodeURIComponent(window.location.href);
    this.ctutn   = null;
    this.ctutv   = null;
    this.tpid    = null;
    this.rcs_l   = null;
    this.rcs_t   = null;
    this.rcs_d   = null;
    this.rcs_i   = null;

    // Complex pixel values.
    this.action_type        = null;
    this.initial_fire_delay = null;
    this.num_fires_per_view = null;
    this.fire_repeat_sleep  = null;
    this.total_num_fired    = null;

    // +----------------+
    // | Public Methods |
    // +----------------+

    this.get_page_view_tracker = function()
    {
        return this._get_tracking_base_url()
               + 't.gif'
               + '?c='
               + this.id
               + '&r='
               + this.referer
               + '&s=1'
               + '&a='
               + this.aid
               + this._get_ctu_qs()
               + this._get_tpid_qs()
               + this._get_rcs_qs();
    };

    this.set_rcs = function(l, t, d, i)
    {
        this.rcs_l = encodeURIComponent(l);
        this.rcs_i = encodeURIComponent(i);

        this.rcs_t = t;
        if (t.length > 100)
        {
            this.rcs_t = t.substr(0, 97) + '...';
        }

        this.rcs_d = d;
        if (d.length > 250)
        {
            this.rcs_d = d.substr(0, 247) + '...';
        }

        this.rcs_t = encodeURIComponent(this.rcs_t);
        this.rcs_d = encodeURIComponent(this.rcs_d);
    };

    this.set_tpid = function(tpid)
    {
        this.tpid = encodeURIComponent(tpid);
    };

    this.set_complex_fire_values = function(action_type,
                                            initial_fire_delay,
                                            num_fires_per_view,
                                            fire_repeat_sleep)
    {
        this.action_type = action_type;
        this.initial_fire_delay = initial_fire_delay;
        this.num_fires_per_view = num_fires_per_view;
        this.fire_repeat_sleep = fire_repeat_sleep;

        this.total_num_fired = 0;
    }

    this.track_action_complex = function()
    {
        var inst = this;

        if (this.action_type === 'delayed' && this.total_num_fired === 0)
        {
            setTimeout(function() {
                inst.track_action();
            }, this.initial_fire_delay * 1000);

            return;
        }
        else if (this.action_type === 'repetitive')
        {
            if (this.total_num_fired === 0)
            {
                setTimeout(function() {
                    inst.track_action();
                }, this.initial_fire_delay * 1000);
            }
            else if (this.total_num_fired <= this.num_fires_per_view)
            {
                setTimeout(function() {
                    inst.track_action();
                }, this.fire_repeat_sleep * 1000);
            }
        }
    }

    this.is_complex = function () {
        return this.action_type === 'delayed' || this.action_type === 'repetitive';
    }

    this.track_action = function()
    {
        if (this.is_complex() &&
            this.total_num_fired === this.num_fires_per_view)
        {
            return;
        }

        var img;

        img = document.createElement('img');
        img.id = 'tpv';
        img.width = '1';
        img.height = '1';
        img.src = this._cache_bust(this.get_page_view_tracker());
        img.alt = '';

        document.body.appendChild(img);

        this.total_num_fired++;

        if (this.is_complex())
        {
            this.track_action_complex();
        }
    };

    // +-----------------+
    // | Private Methods |
    // +-----------------+

    this._cache_bust = function(url)
    {
        return this._url_adjoin_qs(url, { api_z : Math.random() });
    };

    this._cast_to_string = function(a)
    {
        if (typeof a == 'boolean')
        {
            return a ? '1' : '0';
        }

        if (typeof a == 'object' && !a)
        {
            return '';
        }

        return encodeURIComponent('' + a);
    };

    this._get_ctu_qs = function()
    {
        var qs;

        qs = '';
        if (this.ctutn != null && this.ctutv != null)
        {
            qs = '&' + this.ctutn + '=' + this.ctutv;
        }

        return qs;
    };

    this._get_ct_domain_prefix = function()
    {
        return this.ctDomain + '-';
    };

    this._get_rcs_qs = function()
    {
        var qs;

        qs = '';
        if (this.rcs_l != null && this.rcs_t != null && this.rcs_d != null)
        {
            qs =   '&rcs_l=' + this.rcs_l
                 + '&rcs_t=' + this.rcs_t
                 + '&rcs_d=' + this.rcs_d
                 + '&rcs_i=' + this.rcs_i;
        }

        return qs;
    };

    this._get_tpid_qs = function()
    {
        var qs;

        qs = '';
        if (this.tpid != null && this.tpid.length > 0)
        {
            qs = '&tpid=' + this.tpid;
        }

        return qs;
    };

    this._get_tracking_base_url = function()
    {
        return document.location.protocol
               + '//'
               + this._get_ct_domain_prefix()
               + 't.crowdtwist.com/'
               + this.version
               + '/';
    };

    this._url_adjoin_qs = function(url, table)
    {
        var parts = url.split('?');
        var vars = [];

        if (parts.length == 2)
        {
            var name_value_pairs = parts[1].split('&');
            for (var i = 0; i < name_value_pairs.length; i++)
            {
                var name = name_value_pairs[i].split('=')[0];
                if (name && !table.hasOwnProperty(name))
                {
                    vars.push(name_value_pairs[i]);
                }
            }
        }

        for (var key in table)
        {
            vars.push(key + '=' + this._cast_to_string(table[key]));
        }

        return parts[0] + '?' + vars.join('&');
    };
};

// +------------------------------------------------------------+
// | CLIENT FUNCTIONS                                           |
// +------------------------------------------------------------+

/**
 * Rendered:     2021-02-23 12:30:41
 * Site ID:      2
 * Client ID:    39
 * Cachebuster:  1614049134
 * Num. Actions: 0
 */
