linkTracking = {
	init : function() {},

	track : function(event, anchor) {
		var href = jQuery(anchor).attr('href');
		
		/**
		 * This is for the nivo slider that Gen Art uses. The slider jquery plugin injects A tags for navigation
		 * but doesn't set the HREF attribute on the tags...this angers jumplinks most fiercely. So, kill ze script.
		 */
		if (jQuery(anchor).hasClass('nivo-control')) {
			return true;
		}

		// is it mailto?  if so, pop out
		if (href.indexOf('mailto:') != -1)
			return true;

		// check to see if the anchor is contained within a mTracker module
		if (typeof(mTracker) != 'undefined' && mTracker && mTracker.getModules) {
			var modules = mTracker.getModules();
			for (var idx = 0; idx < modules.length; idx++) {
				var moduleName = modules[idx];
				var anchorModuleParent = jQuery(anchor).parents('#' + moduleName.replace(/([^a-z0-9_-])/g, '\\$1'));
				if (anchorModuleParent.length > 0) {
					var intref = moduleName.replace(/-/g, '_');

					// if it contains a jumplink already, extract the vars and jump
					if (this.containsJumplink(href)) {
						var data = this.extractTrackingVars(href);
						this.jump(anchor, data);
						return true;
					} else if (this.isLocalToSite(href)) {
						// if it's on the same host, add intref and continue on your merry way
						if (href.indexOf('intref') == -1) {
							if (href.indexOf('?') != -1)
								href = href + "&intref=" + intref;
							else
								href = href + "?intref=" + intref;
						}
						jQuery(anchor).attr('href', href);
						return true;
					} else {
						this.jump(anchor, { target : href, loc : intref });
						return true;
					}
				}
			}
		}

		// if it's not a mTracker module, check to see if it's a child of a grounded class
		if (jQuery(anchor).parents('.grounded').length > 0)
			return true;

		// check to see if it's a link on this site (but not a jumplink)
		if (this.isLocalToSite(href) && !this.containsJumplink(href))
			return true;

		// we have no choice now but to jump the link
		var data = this.extractTrackingVars(href);

		if (typeof(data.target) == 'undefined' || !data.target)
			data.target = href;

		this.jump(anchor, data);

		return true;
	},

	isLocalToSite : function(href) {
		// if it starts with a slash, it is local
		if (href.match(/^\//))
			return true;

		// if it doesn't start with something like http:// it's local to the dir
		if (!href.match(/^[a-z]+:\/\//))
			return true;

		// if this is a fully-qualified URI (protocol optional) check to see if it begins with this host
		var re = new RegExp("^([^:]+:)?//" + window.location.hostname + "(/|:|$)", "i");
		if (re.test(href))
			return true;

		return false;
	},

	containsJumplink : function(href) {
		if (href.match(/^\/(common\/)?jumplink\.php/))
			return true;

		return false;
	},

	getQueryStringVar : function(href, varname) {
		var re = new RegExp('[&?]' + varname + '=([^&]+)&?');
		var val = re.exec(href);
		if (val != null)
			val = unescape(val[1]);

		return val;
	},

	extractTrackingVars : function(href) {
		var data = {};
		if (this.containsJumplink(href)) {
			var target = this.getQueryStringVar(href, 'target');
			if (target == null)
				return null;
			data.target = target;
		} else {
			data.target = href;
		}

		var trackingVars = ['loc', 'sponsor', 'intref'];
		for (var tvi = 0; tvi < trackingVars.length; tvi++) {
			var tokenToEval = trackingVars[tvi];
			var val = this.getQueryStringVar(href, tokenToEval);
			if (val != null) {
				if (tokenToEval == 'intref') {
					if (href.indexOf(window.location.hostname) == -1)
						tokenToEval = 'loc';
				}

				if (typeof(data[tokenToEval]) == 'undefined')
					data[tokenToEval] = val;
			}
		}

		return data;
	},

	jump : function(ele, data) {
		var newloc = '/common/jumplink.php?target=' + escape(data.target);

		if (typeof(data.sponsor) != 'undefined' && data.sponsor) {
			_gaq.push(['_trackEvent', 'Jumplink', 'Sponsor', data.sponsor]);
			newloc += '&sponsor=' + escape(data.sponsor);
		}

		if (typeof(data.loc) != 'undefined' && data.loc) {
			_gaq.push(['_trackEvent', 'Jumplink', 'Location', data.loc]);
			newloc += '&loc=' + escape(data.loc);
		}

		_gaq.push(['_trackEvent', 'Jumplink', 'Target', data.target]);

		jQuery(ele).attr('href', newloc);
	}
};

var docready = false;
if (typeof(jQuery) != 'undefined' && jQuery != null) jQuery(document).ready(function() {
	if (docready)
		return;

	docready = true;
	jQuery('a[onclick]').each(function() {
		jQuery.data(this, 'onclick', this.onclick);
		this.onclick = null;
	});
	jQuery('a').live('click', function(event) {
		if (!linkTracking.track(event, this))
			return false;

		// perform onclick/attr
		var ret = true;
		var onclickFunc = jQuery.data(this, 'onclick');
		if (onclickFunc) {
			this.onclick = onclickFunc;
			ret = this.onclick();
			this.onclick = null;
		}

		return ret;
	});
});

