dojo.provide("UI.Util.Log");

(function() {
	var UA            = PPP.config.analytics.google.UA,
		env           = PPP.config.env.toUpperCase()[0],
		lastLoadEvent = null,
		tracker       = null;
	
	_gaq.push(['_setAccount', UA]);
	
	dojo.setObject("UI.Util.Log", {
		event: function(action, label, value) {
			this.categorizedEvent(null, action, label, value);
		},
		
		loadEvent: function(label, offsetOverride) {
			var now  = new Date();
			
			this.categorizedEvent(
				null, 
				"Loading", 
				label,
				typeof offsetOverride == "undefined"  ? (lastLoadEvent ? now - lastLoadEvent : 0) : offsetOverride
			);
			
			lastLoadEvent = now;
		},
		
		/**
		 * Encapsulates GATC event tracking
		 * 
		 * String category  The general event category (e.g. "Videos").
		 * String action    The action for the event (e.g. "Play").
		 * String label     An optional descriptor for the event.
		 * Int    value     An optional value associated with the event. You can see your event values in the Overview, Categories, 
		 * and Actions reports, where they are listed by event or aggregated across events, depending upon your report view. 
		 * 
		 * @see http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEventTracking.html
		 */
		categorizedEvent: function(category, action, label, value) {
			// Debug in non-production
			if ( env != "P" ) {
				console.info(
					"Log event: HUB_" + (category ? category.toUpperCase() + "_" : "") + env, 
					action, 
					label, 
					(value && typeof value.join == "function") ? value.join("; ") : value
				);
			}
			
			this.getTracker()._trackEvent(
				"HUB_" + (category ? category.toUpperCase() + "_" : "") + env, 
				action, 
				label, 
				(value && typeof value.join == "function") ? value.join("; ") : value
			);
		},
		
		page: function(url) {
			if ( env != "P" ) {
				console.info("Log page: ", url || window.location.toString());
			}
			
			_gaq.push(["_trackPageview", url || window.location.toString()]);
		},
		
		getTracker: function() {
			return tracker || (tracker = _gat._getTracker(UA));
		},
		
		setLastLoadEventTime: function(time) {
			lastLoadEvent = time;
		}
	});
})();
