/* requires core.js */
/* requires tracking/tracking.js */
ADCENTRIC.lang.namespace("ADCENTRIC.tracking");
ADCENTRIC.env.tracking['flash_cookie_swf_url'] = 'http://s3.amazonaws.com/media.adcentriconline.com/flash_cookie/adc_cookie.swf';

/*
 * ADCENTRIC.tracking.CreativeReachCookie is used to measure CreativeReach, a proprietary AdCentric
 * metric representing reach as ascertained by a Flash-based session "cookie," arguably less likely to
 * have been emptied or third-party-refused by modern browsers.
 *
 * EXAMPLE USAGE:
 *
 * //
 * // inline somewhere, from tag js...
 * //
 * // ADC_EVENT[ADC_REACH_EVENT] is the AdCentric Event URL for CreativeReach (by convention the label
 * // of this event is 'ADC_REACH_EVENT'):
 * //
 * ADCENTRIC.env.creatives['__ADC_ID'].adcCreativeReach = ADCENTRIC.tracking.CreativeReachCookie('ADC_EVENT[ADC_REACH_EVENT]');
 * ADCENTRIC.env.creatives['__ADC_ID'].adcCreativeReach.setup();
 * // nothing more to do...
 */
ADCENTRIC.tracking.CreativeReachCookie = function(witness_flash_pixel_url) {
	function write_container(src) {
		try {
			var adc_f_cookie_cont = document.createElement('div');
			adc_f_cookie_cont.style.visibility = '';
			adc_f_cookie_cont.style.width = "1px";
			adc_f_cookie_cont.style.height = "1px";
			adc_f_cookie_cont.style.position = 'absolute';
			adc_f_cookie_cont.style.display = '';
			adc_f_cookie_cont.innerHTML = src;
			document.body.appendChild(adc_f_cookie_cont);
		} catch (err) { /* ignore exceptions, document.body probably not ready yet */ }
	}

	return {
		flash_cookie_swf : ADCENTRIC.tracking.config['flash_cookie_swf_url'],
		witness_flash_pixel_url : witness_flash_pixel_url,

		to_string: function() {
			var flash_tag = ADCENTRIC.util.FlashTag(this.flash_cookie_swf, '1', '1');
			flash_tag.set_flashvars('creativeReachTag=' + this.witness_flash_pixel_url + '%3fnumber%3d' + Math.random() + '%25adc_fc_user_id%3d');
			return flash_tag.to_string();
		},
		setup: function() {
			var that = this;
			var write_div = function() { write_container(that.to_string()); };
			ADCENTRIC.util.Events.add_window_onload(write_div);
		}
	};
};