var ee_affiliate_name = 'brandonkelly',
	external_re = new RegExp('^https?://(?!'+document.location.hostname+')'),
	external_ee_re = new RegExp('^(https?://(secure\\.|www\\.)?expressionengine.com\\/)([^#]*)(#([\\w_\\.]+))?$'),
	ee_affiliate_re = new RegExp('^index\\.php\\?affiliate='+ee_affiliate_name),
	links = document.getElementsByTagName('a');

for (var i=0; i<links.length; i++) {
	var link = links[i];

	// is this an external link?
	if (link.href.match(external_re)) {
		if (! link.target) link.target = '_blank';

		// if this is a link to expressionengine.com
		// but not already an affiliate link, convert it one
		var href = link.href,
			match = href.match(external_ee_re);
		if (match && ! match[3].match(ee_affiliate_re)) {
			link.href = match[1]+'index.php?affiliate='+ee_affiliate_name
			          + (match[3] ? '&page=/'+match[3] : '')
			          + (match[5] ? '&anchor='+match[5] : '');
		}

		// track outbound links
		//link.onclick = function(){
		//	pageTracker._trackPageview('/Outbound/'+href);
		//};
	}
};

