var trackers = [];
if (typeof(googleProfiles) === 'string') {
	// Single GA profile
	trackers.push(_gat._getTracker(googleProfiles));
} else if (typeof(googleProfiles) === 'object') {
	// Multiple GA profiles
	for (var p in googleProfiles) {
		trackers.push(_gat._getTracker(googleProfiles[p]));
	}
}
// Use separate tracker for local logging
trackers.push(_gat._getTracker());

var cTrack;
var ct;

var rMatches;
var originationSite = null;
var crcReferrer = null;
// store referring site in __utmv cookie if we were passed a blank one in the
// query string and we came from a different site
// IE(4.0-8.0) doesn't send the Referer header when javascript changes location, e.g. using _link()
if ((
		(rMatches = document.referrer.match(/^https?:\/\/(?:www\.)?([^\/]+)\//)) ||
		(rMatches = window.location.search.match(/\breferrer=https?:\/\/(?:www\.)?([^\/]+)\//))
	) && rMatches[1] != document.location.hostname.match(/^(?:www\.)?(.+)/)[1]) {
	if (window.location.search.indexOf('__utmv=-') != -1) {
		// got a blank utmv
		originationSite = rMatches[1];
	}
	if (window.location.search.indexOf('__utmv=') != -1) {
		// got any utmv value
		crcReferrer = rMatches[1];
	}
}

for (ct = 0; ct < trackers.length; ct++) {
	cTrack = trackers[ct];
	cTrack._setDomainName("none");
	cTrack._setAllowLinker(true);
	cTrack._setAllowHash(false);
	if (ct == trackers.length - 1) {
		// local tracker
		cTrack._setLocalServerMode();
		if (typeof(utmGifPath) !== 'undefined') {
			cTrack._setLocalGifPath(utmGifPath);
		}
	} else {
		// GA tracker
		cTrack._setRemoteServerMode();
	}
	if (typeof(directKeywords) !== 'undefined') {
		for (var dk in directKeywords) {
			if (typeof(directKeywords[dk]) == "string") {
				cTrack._addIgnoredOrganic(directKeywords[dk]);
			}
		}
	}
	cTrack._initData();
	if (originationSite !== null) {
		cTrack._setCustomVar(1,"Originating_Site",rMatches[1],1);
	}

	if (typeof(trackingURL) === 'string') {
		if (trackingURL.match(/\/form\/complete\?.*\bsubscribe/gi)) {
			cTrack._setCustomVar(2,"Subscriber",document.location.hostname,1);
		}

		if (trackingURL.match(/\/form\/complete\?.*(\bcontact|\bmini|\bassessment)/gi)) {
			cTrack._setCustomVar(3,"Inquirer",document.location.hostname,1);
		}
	}
	
	if (ct != trackers.length - 1) {
		// don't send events to local server (for now, at least)
		if (crcReferrer !== null) {
			cTrack._trackEvent('CRC_Referral', rMatches[1]);
		}
	}
	if (typeof(trackingURL) === 'string') {
		cTrack._trackPageview(trackingURL);
	} else {
		cTrack._trackPageview();
	}
}
// cTrack is local tracker

// use a custom variable for our instance of jquery in case there is
// already another version in use
var $t = jQuery.noConflict(true);
$t(function() {
	var linkSelector = typeof(linkTrackSelector) === 'undefined' ? 'a.crc' : linkTrackSelector;
	$t(linkSelector).click(function() {
		var url = $t(this).attr('href');
		var matches = url.match(/^(.*?)(#[^#]*)?$/);
		var anchor = '';
		if (matches[2] !== undefined) {
			url = matches[1];
			anchor = matches[2];
		}
		// IE doesn't send Referer header with javascript navigation so we have
		// to send it in the query string.  We only care about the domain
		url += (url.indexOf('?') == -1 ? '?' : '&') + 'referrer=' + window.location.protocol + '//' + window.location.host + window.location.pathname;
		url += anchor;
		cTrack._link(url);
		return false;
	});
	if (typeof(phoneTrackSelector) !== 'undefined') {
		var _uGC = function(l,n,s) {
			if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
			var i,i2,i3,c="-";
			i=l.indexOf(n);
			i3=n.indexOf("=")+1;
			if (i > -1) {
				i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
				c=l.substring((i+i3),i2);
			}
			return c;
		}
		var z = _uGC(document.cookie, '__utmz=', ';');
		var phone = _uGC(z, 'utmcid=', '|');
		if (phone != '-') {
			$t(phoneTrackSelector).text(unescape(phone));
		}
	}
	$t(window).unload(function () {
		cTrack._trackPageview('/exit');
	});
});
