/* requires core.js */
/* requires tracking/tracking.js */
ADCENTRIC.lang.namespace("ADCENTRIC.tracking");

/*
 * ADCENTRIC.tracking.SiteStatsPage is an object allowing for tracking of arbitrary
 * events/actions within a page of a site using an AdCentric event pixel, the URL of
 * which is passed in via the SiteStatsPage constructor defined here.
 *
 * Arbitrary events/interactions are tacked onto the specified event pixel via
 * the querystring.  The name of the event being logged is passed into the
 * action method of the returned SiteStatsPage object and is tacked onto the
 * query string as the value of the adc_action event parameter.  Additional
 * arguments can be logged via the action method via the optional param_keyvals
 * argument hash.  If the second argument is { "foo": "bar", "baz": "blah" },
 * then the additional foo=bar&baz=blah parameters will be appended to the querystring.
 * See example below for how this is done.
 *
 * Note that the tag code this is used from is the tag code of a SiteStats
 * campaign creative in AdCentric specifically created for the purposes of
 * tracking a site.  The impressions on said tag are sufficient to generate
 * site statistics at the page-level.  For Ajax, Flash, and other in-page special
 * event tracking, ADCENTRIC.tracking.SiteStatsPage().action() is used.
 *
 * Note also that this is a very similar model to that used by Google Analytics'
 * urchinTracker - in fact, if urchinTracker is defined, SiteStatsPage will take
 * care of calling it as well, passing in at least the same name as that provided
 * to the action method.
 * 
 * EXAMPLE USAGE:
 *
 * In the site-stats tag code, at least ADCENTRIC core and tracking js must
 * be loaded:
 *
 * if ("undefined" === typeof adcAction) {
 *     var adcAction = function(name, params) {
 *         ADCENTRIC.tracking.SiteStatsPage('__ADC_EVENT[SITESTATS]').action(name, params);
 *     }
 * }
 *
 * Then later in the page that includes the tag, for instance:
 * document.getElementById("my_link").onclick = function(e) {
 *     adcAction("my_link_click", { "ageGroup": "18-25", "coolnessFactor": "100" });
 * };
 *
 */
ADCENTRIC.tracking.SiteStatsPage = function(siteevent_url) {
	var dispatcher = ADCENTRIC.tracking.EventPixelDispatcher();
	var global_param_keyvals = { };

	function compute_send_params(new_params) {
		var params = { };

		for (var key in global_param_keyvals)
			params[key] = global_param_keyvals[key];
		if ("undefined" !== typeof new_params) {
			for (var key in new_params)
				params[key] = new_params[key];
		}
		return params;
	}

	return {
		siteevent_url: siteevent_url,
		debug: false,

		set_global_params: function(param_keyvals) {
			global_param_keyvals = param_keyvals;
		},
		get_global_params: function() {
			return global_param_keyvals;
		},
		action: function(name, param_keyvals) {
			var n = String(name), params = "";
			var querystring;
			var send_params = compute_send_params(param_keyvals);

			for (var key in send_params)
				params += "&" + encodeURIComponent(String(key)) + "=" + encodeURIComponent(String(send_params[key]));

			// If Google Analytics' urchinTracker() is loaded, feed it the passed-in 'name' as well:
			if ("undefined" !== typeof urchinTracker)
				urchinTracker(n);

			// we use encodeURI not encodeURIComponent for the name/adc_action, as we would like to
			// allow for slashes ("/") to be included, unencoded, for convenience
			querystring = "adc_action=" + encodeURI(n) + params;
			if (this.debug) {
				alert("ADCENTRIC.tracking.SiteStatsPage: " + querystring);
			}
			return dispatcher.dispatch(this.siteevent_url + "?" + querystring);
		},
		view: function(name, param_keyvals) {
			return this.action("/views/" + String(name), param_keyvals);
		},
		click: function(name, param_keyvals) {
			return this.action("/clicks/" + String(name), param_keyvals);
		},
		hover: function(name, param_keyvals) {
			return this.action("/hovers/" + String(name), param_keyvals);
		}
	};
};
