/**
 * Google Analytics object for supporting cross-domain
 * link tracking as well as custom events and variables.
 *
 * This script should be run AFTER the GA Asynchronous tracking snippet is
 * added to the page and only called after the DOM is ready.
 *
 * All error messages are reported to firebug's console.
 *
 * @author Jesse Bunch (www.getbunch.com)
 */
var objGATracker = {
	
	/**
	 * Array of hostnames to track links to
	 */
	arrHostsToTrack: [],
	
	/**
	 * Should we write log messages? Should be false
	 * when run in the production environment.
	 */
	boolLog: true,
	
	/**
	 * Main initialization routine. Sets up the object and
	 * fires off other init methods to handle the workload.
	 */
	init: function(boolTrackLinks, boolTrackInline, strHostsToTrack) {
		
		// Set the list of hosts
		objGATracker.arrHostsToTrack = strHostsToTrack.split("|");
		objGATracker._doLog(objGATracker.arrHostsToTrack);
		
		// Notify if we don't have any hosts to track
		if (0 === objGATracker.arrHostsToTrack.length)
			objGATracker._doLog("No hosts were set");
		
		// Should we track links?
		if (boolTrackLinks)
			objGATracker.initTrackLinks();
			
		// Should we do inline tracking? (ie events, custom vars, etc)
		if (boolTrackInline)
			objGATracker.initInlineTracking();
			
			
	},
	
	/**
	 * Hooks all the links on the page and fires off a link
	 * track to Google Analytics if 
	 */
	initTrackLinks: function() {
		
		objGATracker._doLog("Link tracking is enabled...");
		
		jQuery("a").live("click", function() {
			
			var elemLink = this;
			var strHostname = elemLink.hostname;			
			
			// Search for a matching hostname
			for(var i in objGATracker.arrHostsToTrack) {

				// Compare....
				if (objGATracker.arrHostsToTrack[i].toLowerCase() == strHostname.toLowerCase()) {
					
					// Match found, track the link
					objGATracker.trackLink(elemLink.href);
					
					// GA redirects so we need to cancel this link
					return false;
					
				}

			}
			
			// No match? Okay, let them proceed.
			return true;

		});
		
	},
	
	/**
	 * Since we'll be tracking several different types of events via links,
	 * let's create a standardized, decoupled interface for tracking those links.
	 * To enable tracking, just set the data-ga-track attribute of your link to one
	 * of the variables below and then set the necessary data parameters as well.
	 * As always, check firebug's console for error messages.
	 */
	initInlineTracking: function() {
		
		objGATracker._doLog("Inline tracking is enabled...");
		
		jQuery("a").live("click", function() {
			
			var jQuerylink = jQuery(this);
			
			switch(jQuerylink.attr("data-ga-track")) {
				
				// ---------------
				
				case "event":
				
					objGATracker._doLog("Tracking event...");
					
					objGATracker.trackEvent(jQuerylink.attr("data-ga-category"), 
												  jQuerylink.attr("data-ga-action"), 
												  jQuerylink.attr("data-ga-label"), 
												  jQuerylink.attr("data-ga-value"));
												
					break;
				
				// ---------------
				
				case "customVar":
				
					objGATracker._doLog("Setting custom variable...");
				
					objGATracker.trackEvent(jQuerylink.attr("data-ga-slot"), 
												  jQuerylink.attr("data-ga-name"), 
												  jQuerylink.attr("data-ga-value"), 
												  jQuerylink.attr("data-ga-scope"));
				
					break;
					
				// ---------------

				case "social":

					objGATracker._doLog("Tracking social interaction...");

					objGATracker.trackEvent(jQuerylink.attr("data-ga-network"), 
												  jQuerylink.attr("data-ga-action"), 
												  jQuerylink.attr("data-ga-target"), 
												  jQuerylink.attr("data-ga-path"));

					break;
					
				// ---------------
				
				case undefined:
				
					// Don't give an error if there's nothing there
					
					break;
					
				// ---------------
				
				default:
				
					objGATracker._doLog("The inline tracking type variable wasn't valid.");
				
					break;
				
			}
			
			// Finished
			return true;
			
		});
		
	},
	
	/**
	 * Tracks an outbound link to the specified URL. Your function should
	 * cancel the default behavior of the links as Google redirects the user
	 * to the link after it has been tracked.
	 */
	trackLink: function(strURL) {
		
		objGATracker._doGAPush(["_link", strURL]);
		
	},
	
	/**
	 * Tracks an event
	 * See: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEventTracking.html
	 */
	trackEvent: function(strCategory, strAction, strLabel, intValue) {
		
		objGATracker._doGAPush(["_trackEvent", strCategory, strAction, strLabel, intValue]);
		
	},
	
	/**
	 * Sets the custom variable using the scope provided.
	 * See: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._setCustomVar
	 */
	setCustomVar: function(intSlot, strName, strValue, intScope) {
		
		// Make sure we don't exceed the 64 byte limit in GA.
		strName = decodeURIComponent(encodeURIComponent(strName).substr(0,32));
		strValue = decodeURIComponent(encodeURIComponent(strValue).substr(0,31));
		
		if (!objGATracker._doGAPush(["_setCustomVar", intSlot, strName, strValue, intScope]))
			objGATracker._doLog("Error setting the custom variable");
		else 
			objGATracker._doLog("Set the custom variable successfully");
		
	},
	
	/**
	 * Tracks the page view for the specified URL.
	 * Note: the URI you pass in should be the pathname only. Use document.location.pathname
	 * See: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview
	 */
	trackPageview: function(strURI) {
	
		if (!objGATracker._doGAPush(["_trackPageview", strURI]))
			objGATracker._doLog('Error tracking the page view');
		else
			objGATracker._doLog('The page view was tracked successfully');
		
	},
	
	/**
	 * Tracks social interactions on the page.
	 * See: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiSocialTracking.html
	 */
	trackSocial: function(strNetwork, strSocialAction, strTarget, strPath) {
	
		objGATracker._doGAPush(["_trackSocial", strNetwork, strSocialAction, strTarget, strPath]);
		objGATracker._doLog('The social interaction was tracked successfully');
		
	},
	
	/**
	 * Sends the array of params to Google Anlaytics
	 */
	_doGAPush: function(arrParams) {
		
		objGATracker._doLog("Pushing to GA...");
		objGATracker._doLog(arrParams);
		
		if (typeof _gaq != "undefined") {
			return _gaq.push(arrParams);
		} else {
			objGATracker._doLog("Google Analytics doesn't appear to be installed.");
			return false;
		}
		
		return false;
		
	},
	
	/**
	 * Writes the specified message to the console, if available.
	 */
	_doLog: function(stringOrObject) {
		
		if (objGATracker.boolLog) {
		
			try {
				if (typeof stringOrObject == "string"){
					console.log("GA Log: " + stringOrObject);
				} else {
					console.log(stringOrObject);
				}
			} catch(objError) {
				// do nothing; console doesn't exist.
			}
			
		}
		
	}
	
};

















