/* requires core.js */
ADCENTRIC.lang.namespace("ADCENTRIC.tracking");
ADCENTRIC.env.tracking = ADCENTRIC.env.tracking || { };

ADCENTRIC.tracking.EventPixelDispatcher = function() {
	var witness_event_queue = new Array();
	var queue_processing = false;
	var pixel = new Image();

	function process_queue() {
		queue_processing = true;
		pixel.onload = function() {
			pixel = new Image();
			if (witness_event_queue.length > 0) {
				process_queue();
			} else {
				queue_processing = false;
			}
		};
		pixel.src = witness_event_queue.shift();
	}

	function maybe_process_queue() {
		if (!queue_processing) {
			process_queue();
			return true;
		}
		return false;
	}

	function cache_bust(url) {
		var qindex = url.indexOf("?");
		var random = "r=" + Math.random();

		if (qindex < 0) {
			random = "?" + random;
		} else if (qindex != (url.length - 1)) {
			random = "&" + random;
		}
		return(url + random);
	}

	return {
		dispatch: function(event_pixel_url, dont_cache_bust) {
			var url = event_pixel_url;

			if (!dont_cache_bust) {
				url = cache_bust(url);
			}
			witness_event_queue.push(url);
			return maybe_process_queue();
		}
	};
};