/**
 * EE Affiliate link automations?
 *
 * Original concept and code by: Leevi Graham <http://leevigraham.com>
 * Modified by: Brandon Kelly <http://pixelandtonic>
 */

var ee_affiliate_name = 'electricputty',
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);

$('a').each(function(){
	// is this an external link?
	if (this.href.match(external_re))
	{
		if (! this.target) this.target = '_blank';
		// if this is a link to expressionengine.com
		// but not already an affiliate link, convert it one
		var href = this.href,
		match = href.match(external_ee_re);
		if (match && ! match[3].match(ee_affiliate_re)) 
		{
			this.href = match[1]+'index.php?affiliate='+ee_affiliate_name
			+ (match[3] ? '&page=/'+match[3] : '')
			+ (match[5] ? '&anchor='+match[5] : '');
		}
		// track outbound links
		$(this).click(function() {
			pageTracker._trackPageview('/Outbound/'+href);
		});
	}
});
