/* This JS file is to be used with sites that are using Google Analytics 
* It enables tracking of external & downloadable URLs so that Google can report on them.
* JS will loop through all links on a page and if they are mailto, external or downloadable links
* then it will attach an onclick event to those links whick in turn invoke the GA pageTracker.
* Place this script just before the GA tracking code.
* More info here:  http://www.google.com/support/googleanalytics/bin/answer.py?answer=55529&ctx=sibling
* Copyright © www.think.eu 2010 [Sam.Nseir] v1.3
*/

var gaLinkTracker = {
	_siteoverlay: false,
	init: function() {
		// check browser support
		if (document.getElementsByTagName) {
			gaLinkTracker._siteoverlay = (document.getElementById("_gasojs") != null)
			// set which file extensions to track as download links
			var regExtn = /\.(?:pdf|doc|xls|ppt|docx|xlsx|pptx|wma|mov|avi|wmv|mp3)($|\&|\?)/i;
			// loop through all <a> tags on the page
			var hrefs = document.getElementsByTagName("a"), chref;
			for (var n = 0; n < hrefs.length; n++) {
				try {
					// reference current href in the loop
					chref = hrefs[n];
					// handle "mailto:" links
					if (chref.protocol == "mailto:") {
						chref._trackUrl = "/~mailto/" + chref.href.substring(7);
						gaLinkTracker.applyEventToANode(chref);
					}
					else if (chref.protocol == "javascript:") {
						chref._trackUrl = "/~javascript/" + chref.href.substring(11);
						gaLinkTracker.applyEventToANode(chref);
					}
					// handle external links
					else if (chref.hostname != location.host) {
						chref._trackUrl = "/~external/" + chref.hostname + "/" + chref.pathname + chref.search;
						gaLinkTracker.applyEventToANode(chref);
					}
					// handle downloadable links
					else if (regExtn.test(chref.pathname + chref.search)) {
						chref._trackUrl = "/~downloads/" + chref.pathname + chref.search;
						gaLinkTracker.applyEventToANode(chref);
					}
				}
				catch (e) { continue; }
			}
		}
	},

	applyEventToANode: function(aNode) {
		if (aNode._trackUrl == null) return;
		aNode._trackUrl = aNode._trackUrl.split("//").join("/");
		if (this._siteoverlay == true) {
			aNode._originalUrl = aNode.href;
			aNode.href = aNode._trackUrl;
			aNode.hostname = document.location.hostname;
			gaLinkTracker.addEvent(aNode, "click", gaLinkTracker.undo);
		} else {
			gaLinkTracker.addEvent(aNode, "click", gaLinkTracker.track);
		}
	},

	// Call GA PageTracker with relevant paths
	track: function(e) {
		e = (e.srcElement) ? e.srcElement : this; while (e.tagName != "A") { e = e.parentNode; }
		if (e._trackUrl != null && typeof (pageTracker) == "object") { pageTracker._trackPageview(e._trackUrl); }
	},
	addEvent: function(obj, evnt, func) {
		if (obj.addEventListener) { obj.addEventListener(evnt, func, false); } else if (obj.attachEvent) { obj.attachEvent("on" + evnt, func); }
	},
	undo: function(e) {
		e = (e.srcElement) ? e.srcElement : this; while (e.tagName != "A") { e = e.parentNode; }
		if (e._originalUrl != null) { e.href = e._originalUrl; }
	},
	preventDefault: function(e) {
		e.cancelBubble = true; e.returnValue = false; if (e.stopPropagation) { e.stopPropagation(); e.preventDefault(); }
	}
}
gaLinkTracker.addEvent(window, 'load', gaLinkTracker.init);


// the following is specific to AnglianWater & RightNow modules

gaLinkTracker.addEvent(window, 'load', gaRightNowFeed);
function gaRightNowFeed() {
	try {
		RNTReader.prototype.onFeedReadyOld = RNTReader.prototype.onFeedReady;
		RNTReader.prototype.onFeedReady = function(arg) {
			this.onFeedReadyOld(arg);
			gaRightNowTrack(this.root);
		}
	}
	catch (e) { }
}
function gaRightNowTrack(domNode) {
	var hrefs = domNode.getElementsByTagName("a"), chref;
	for (var n = 0; n < hrefs.length; n++) {
		try {
			chref = hrefs[n];
			// handle external links only
			if (chref.hostname != location.host) {
				// strip any session info in the link
				chref._trackUrl = "/~external/" + chref.hostname + "/" + chref.pathname.split("/session/")[0] + chref.search;
				gaLinkTracker.applyEventToANode(chref);
			}
		}
		catch (e) { continue; }
	}
}