/*******************************************************
 * This code is Copyright (C) 2006 POP and may not
 * be used without explicit written permission from POP
 *******************************************************/
us.popdt.analytics = {
	/**
	 * Container for GA Configuration information.
	 * Note: you must put all possible live domains in the domains array
	 */
	config: {
		enabled: true,
		domains: [								// A list of ALL POSSIBLE internal domains
			location.hostname,					// Current page domain
			us.popdt.config.ROOT.getHostname(),	// Config domain
			'www.steppenwolf.org',
			'steppenwolf.org',
			'blog.steppenwolf.org'
		],
		params: {
			_uacct: "UA-331898-1",
			_udn: "steppenwolf.org",
			_userv: 2
		}
	},
	/**
	 * List of file suffixes to track as downloads
	 */
	dlSuffix: ["mov","mp3","wmv","wav","rm","doc","xls","zip","pdf","sit","sitx","tgz"],
	

	/**
	 * Starts all aspects of GA tracking.
	 * "this" scopes to "window", not us.popdt.analytics.
	 */
	init: function() {
		us.popdt.analytics.build();
		
		// create variables for GA tracking
		var p = us.popdt.analytics.config.params;
		for (var i in p) {
			window[i] = p[i];
		}
		
		// Call GA Tracker (included as seperate script element).
		if (typeof urchinTracker != "undefined") {
			urchinTracker();
		}
	},
	build: function() {
		var links = this.collectLinks();
		for (var i=0; i<links.length; i++) {
			this.attachLinkTracker(links[i]);
		}
	},

	/**
	 * Collects all links to track on page
	 * @return {Array} Array of link elements to track
	 */
	collectLinks: function() {
		// Find all A tags
		var a = $d.get('a', 'area');
		var links_to_track = [];
		for (var i=0; i < a.length; i++) {
			if (this.isOutbound(a[i]) || this.isDownload(a[i]) || this.isEmail(a[i]) || this.isJS(a[i])) {
				links_to_track.push(a[i]);
			}
		}
		return links_to_track;
	},
	
	/**
	 * Attaches clicktracker to each link OR returns the string that would be
	 * tracked it the tracker were attached to the link
	 * @param {Object} oLinkElement A link element to attach the clicktracker to
	 * @param {Boolean} bList Whether or not to return a string representation instead of tracking.
	 */
	attachLinkTracker: function(oLinkElement, bList) {
		if (bList === true) {
			return this.getTrackerString(oLinkElement);
		} else {
			us.popdt.event.add(oLinkElement,"click",us.popdt.analytics.trackClick);
		}
	},

	/**
	 * Attached to each outbound link. "this" scopes to the "a" object.
	 */
	trackClick: function() {
		var o = us.popdt.analytics.getTrackerString(this);
		urchinTracker(o);
	},
	
	/**
	 * Get the string to pass to GA from an A element
	 * @param {Object} a An A or Area HTML Element
	 * @return {String} A string for to pass to GA for custom click-tracking
	 */
	getTrackerString: function(a) {
		switch (true) {
			case this.isOutbound(a):
				return '/outbound/' + this.removeProtocol(a.href);
				break;
			case this.isDownload(a):
				return '/download/' + this.removeProtocol(a.href).replace(a.href.getHostname(),'').replace(/^\//,'');
				break;
			case this.isEmail(a):
				return '/email/' + this.removeProtocol(a.href);
				break;
			case this.isJS(a):
				return '/javascript/' + this.removeProtocol(a.href).replace(a.href.getHostname(),'').replace(/^\//,'');
				break;
		}
	},
	/**
	 * UTILITY: Remove Protocol from link
	 * @param {String} sHref
	 * @return {String} String without the beginning protocol
	 */
	removeProtocol: function(sHref) {
		if (sHref.indexOf('mailto') == 0) {
			return sHref.replace(/^\w+\:/,"");
		} else {
			return sHref.replace(/^\w+\:\/\//,"");
		}
	},
	/**
	 * UTILITY: Check if an a's href is an OUTBOUND URL
	 * @param {Object} a Link or Area element
	 * @return {Boolean}
	 */
	isOutbound: function(a) {
		var is_out = false;
		// if URL domain is not in our domain list and it begins with 'http', it's external
		if ( (!this.config.domains.contains(a.href.getHostname())) && (a.href.indexOf('http') == 0) ) {
			is_out = true;
		}
		return is_out;
	},
	/**
	 * UTILITY: Check if an a's href is an DOWNLOAD URL
	 * @param {Object} a Link or Area element
	 * @return {Boolean}
	 */
	isDownload: function(a) {
		var suff = a.href.split("?")[0];			// remove query strings
		suff = suff.split(".");
		suff = suff[suff.length - 1];				// isolate the suffix
		if (this.dlSuffix.indexOf(suff) > -1) {		// find out if it's a trackable suffix
			return true;
		} else {
			return false;
		}
	},
	/**
	 * UTILITY: Check if an a's href is an EMAIL ADDRESS
	 * @param {Object} a Link or Area element
	 * @return {Boolean}
	 */
	isEmail: function(a) {
		if (a.href.indexOf('mailto:') == 0) {
			return true;
		} else {
			return false;
		}
	},
	/**
	 * UTILITY: Check if an a is designated as a JS LINK
	 * @param {Object} a Link or Area element
	 * @return {Boolean}
	 */
	isJS: function(a) {
		return $d.hasClass(a, 'ga_jslink');
	},

	
	/**
	 * DEBUGGING: Prints out a list of all links being tracked
	 */
	printTracked: function() {
		var links = this.collectLinks();
		for (var i=0; i<links.length; i++) {
			dbug.log(this.attachLinkTracker(links[i], true));
		}
	}
}
if (us.popdt.analytics.config.enabled == true) {
	us.popdt.event.add(window, "load", us.popdt.analytics.init);
}