/**
 * tracking.js
 * @version 1.0
 * @author Tharsan Bhuvanendran <tbhuvanendran@360i.com>
 * @copyright 360i
 * 
 * Provides a flexible, easy-to-use global tracking framework for reporting events
 * and page views to third-party analytics services, using sensible defaults that
 * can be easily overridden.
 * 
 * USAGE:
 * 
 * tracking.trackPageView([url], [trackers])
 *   url: Provided explicitly, the 'href' attribute on the context element, or
 *     the current location.
 *   trackers: Provided explicitly, or global variable tracking.defaults.trackers.
 * 
 * tracking.trackEvent([category], [action], [trackers])
 *   category: Provided explicitly, the 'data-tracking-category' attribute on the
 *     context element, the 'href' attribute on the context element, or the current
 *     location.
 *   action: Provided explicitly, or the current location.
 *   trackers: Provided explicitly, or global variable tracking.defaults.trackers.
 */

var TRACKER_GOOGLE_ANALYTICS = 1,
    TRACKER_DOUBLECLICK      = 2,
    TRACKER_COREMETRICS      = 4; // TODO: add support for CoreMetrics tracking

var tracking = {
	/**
	 * Initialize web tracking.
	 * 
	 * @param options A hash of tracker constants to tracker configuration settings.
	 */
	init: function(options) {
		// Include Google Analytics
		if(options.TRACKER_GOOGLE_ANALYTICS) {
			var gaOptions = options.TRACKER_GOOGLE_ANALYTICS;

			if(!window._gaq) {
				window._gaq = window._gaq || [];
				window._gaq.push(['_setAccount', gaOptions.id]);
				window._gaq.push(['_trackPageView']);

				(function() {
				    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
				    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
				})();
			}

			tracking.defaults.trackers |= TRACKER_GOOGLE_ANALYTICS;
		}

		// Include DoubleClick
		if(options.TRACKER_DOUBLECLICK) {
			var dcOptions = options.TRACKER_DOUBLECLICK;

			tracking.settings.doubleclick['id'] = dcOptions.id;

			tracking.defaults.trackers |= TRACKER_DOUBLECLICK;
		}
	},

	/**
	 * Sensible defaults for various configuration settings.
	 * 
	 * debug: Log tracked events firing to the console, if available.
	 * eventCategoryName: The name of the category to use during event tracking.
	 * trackers: A bitwise OR collection of trackers to report to.
	 */
	defaults: {
		debug: false,
		eventCategoryName: "custom-event",
		trackers: 0
	},

	settings: {
		doubleclick: {}
	},

	/**
	 * Track an event, described by a category & action.
	 * When called directly, function arguments are expected to be:
	 *   category, action, trackers
	 * When called as a function event handler, the first argument is an Event
	 * object containing a 'data' hash with expected keys
	 *   category, action, trackers
	 * 
	 * @param category string The name you supply for the group of objects
	 *   you want to track.
	 * @param action   string A string that is uniquely paired with each
	 *   category, and commonly used to define the type of user interaction
	 *   for the web object.
	 * @param trackers int    A bitwise combination of the trackers to report
	 *   this event to.
	 */
	trackEvent: function() {
		var category, action, trackers;

		// assume that this function was called as an event handler, and the
		// first argument is an Event object
		if(typeof(arguments[0]) == 'object') {
			var evt = arguments[0];
			evt.data = evt.data || {};

			category = evt.data.category || this.getAttribute('data-tracking-category') || tracking.defaults.eventCategoryName;
			action = evt.data.action || this.getAttribute('data-tracking-action') || this.href || location.href;
			trackers = evt.data.trackers || tracking.defaults.trackers;

		// expect parameters passed to the function directly
		} else {
			category = arguments[0] || tracking.defaults.eventCategoryName;
			action = arguments[1] || location.href;
			trackers = arguments[2] || tracking.defaults.trackers;
		}

		if(trackers & TRACKER_GOOGLE_ANALYTICS && window._gaq) {
			if(window.console && tracking.defaults.debug)
				console.log("tracking.js: Google Analytics event tracked: category '%s', action '%s'", category, action);

			window._gaq.push(['_trackEvent', category, action]);

		} else if(trackers & TRACKER_GOOGLE_ANALYTICS) {
			if(window.console && tracking.defaults.debug)
				console.log("tracking.js: DoubleClick event tracked: category '%s', action '%s'", category, action);

			var axel = Math.random() + "";
			var a = axel * 10000000000000;

			var i = new Image();
			i.src = 'http://fls.doubleclick.net/activityi;src=' + tracking.settings.doubleclick.id + ';type=' + category + ';cat=' + action + ';ord=1;num=' + a +'?';
		}
	},

	/**
	 * Track a page view.
	 * When called directly, function arguments are expected to be:
	 *   url, trackers
	 * When called as a function event handler, the first argument is an Event
	 * object containing a 'data' hash with expected keys
	 *   url, trackers
	 * NOTE: Only Google Analytics page view tracking is supported.
	 * 
	 * @param url      string The URL for the page to be tracked.
	 * @param trackers int    A bitwise OR of the trackers to report this
	 *   event to.
	 */
	trackPageView: function() {
		var url, trackers

		// assume that this function was called as an event handler, and the
		// first argument is an Event object
		if(typeof(arguments[0]) == 'object') {
			var evt = arguments[0];
			evt.data = evt.data || {};

			url = evt.data.url || this.href || location.href;
			trackers = evt.data.trackers || tracking.defaults.trackers;

		// expect parameters passed to the function directly
		} else {
			url = arguments[0] || location.href;
			trackers = arguments[1] || tracking.defaults.trackers;
		}

		if((trackers & TRACKER_GOOGLE_ANALYTICS) && window._gaq) {
			if(window.console && tracking.defaults.debug)
				console.log("tracking.js: Google Analytics page view tracked: url '%s'", url);

			window._gaq.push(['_trackPageview', url]);
		}
	}
};

