﻿var gaA_pageTracker = pageTracker;

var gaTrackLinks = function() {
	/**
	* startListening: add a new listner for onclick event, handle Mozilla or IE methods
	* @param {Object} obj HREF object to listen to
	* @param {String} evnt event type (usually "click")
	* @param {Object} func Function to call when evnt is triggered
	*/
	var startListening = function(obj, evnt, func) {
		if (obj.addEventListener)
			obj.addEventListener(evnt, func, false);
		else
			if (obj.attachEvent)
			obj.attachEvent("on" + evnt, func);
	}


	/**
	* trackExternalLink: make GA call when clicking an outbound link, use trackEvent() or trackPageView() methods
	* @param {Object} evnt Object where the event happened
	*/
	var trackExternalLink = function(evnt) {

		var elmnt = evnt.srcElement;
		if (elmnt) {
			while (elmnt.tagName != "A") elmnt = elmnt.parentNode;
			if (/http/.test(elmnt.protocol) || /https/.test(elmnt.protocol)) {
				gaA_pageTracker._trackEvent("Exit link", elmnt.hostname, elmnt.hostname + "/" + elmnt.pathname + elmnt.search);
			}
		}
		else {
			if (/http/.test(this.protocol) || /https/.test(elmnt.protocol)) {
				gaA_pageTracker._trackEvent("Exit link", this.hostname, this.hostname + "/" + this.pathname + this.search);
			}
		}
	}

	/**
	* Initialize 
	*/
	if (document.getElementsByTagName && typeof gaA_pageTracker == "object") {
		var aTags = document.getElementsByTagName('a');
		for (var l = 0, m = aTags.length; l < m; l++) {
			if (aTags[l].hostname != location.hostname) {
				startListening(aTags[l], "click", trackExternalLink);
			}
		}
	}


}

if (window.addEventListener) // Standard
	window.addEventListener('load', function() {
		gaTrackLinks()
	}, false);
else
	if (window.attachEvent) // IE
	window.attachEvent('onload', function() {
		gaTrackLinks()
	});

	