/**
 * @projectDescription Custom JS Code for Google Analytics
 * @requires Google Analytics - ja.js NOT urchin.js, WP compiled.js | etwp compiler.js
 * @author mjames
 * @version 1.2
 */

/**
 * default namespacing
 */
var newmind = window.newmind || {};
newmind.googleanalytics = window.newmind.googleanalytics || {};

/**
 * when called finds all the links in a page, spinds through them and checks to see if they are external links,
 * if they are then attaches some Google Analytics handler that allows the click through to be registered
 */
newmind.googleanalytics.externallinks = function(){
	//private variables
	
	//private methods
	/**
	 * find all anchors in a page, see if they are "external" links and hookup some event listeners
	 */
	var _attachListeners = function(){
		var aryAnchors = document.getElementsByTagName("a");
		var strHost = document.location.hostname;
		for(var i=0; i < aryAnchors.length; i = i + 1){
			var eAnchor = aryAnchors[i];
			if ((eAnchor.getAttribute("href") && eAnchor.getAttribute("rel") === "external")||((eAnchor.href.search(strHost) === -1 && eAnchor.href.search('mailto:') === -1) && eAnchor.href.search('http://') !== -1)) {
				if (eAnchor.title.indexOf('opens in a') === 0) {
					eAnchor.title += " Link opens in a new window";
				}
				AddEventListener(eAnchor, "click", newmind.googleanalytics.externallinks.trackme);				
			}
		}
	};
	
	var _preventDefault = function(e){
		if (returnFalse === undefined){
			var ev = e || window.event;
			if (ev.preventDefault) {
			    ev.preventDefault();
			} 
			else {
			    ev.returnValue = false;
			}
		}
		else{
			returnFalse(e);
		}
	};
	
	var _addEventListener = function(obj, type, action){
		if(typeof window.addEventListener !== 'undefined')
		{
			//.. gecko, safari, konqueror and standard
			obj.addEventListener(type, action, false);
		}
		else if(typeof document.addEventListener !== 'undefined')
		{
			//.. opera 7
			obj.addEventListener(type, action, false);
		}
		else if(typeof window.attachEvent !== 'undefined')
		{
			//.. win/ie
			obj.attachEvent('on' + type, action);
		}
	};
	
	var _removeEventListener = function(obj, type, action){
		if(typeof window.removeEventListener !== 'undefined')
		{
			//.. gecko, safari, konqueror and standard
			obj.removeEventListener(type, action, false);
		}
		else if(typeof document.removeEventListener !== 'undefined')
		{
			//.. opera 7
			obj.removeEventListener(type, action, false);
		}
		else if(typeof window.detachEvent !== 'undefined')
		{
			//.. win/ie
			obj.detachEvent("on" + type, action);
		}
	};
	
	var _getTarget = function(e){
		if (e.target) {
			targ = e.target;
		}
		else {
			if (e.srcElement) {
				targ = e.srcElement;
			}
			if (targ.nodeType === 3) { // defeat Safari bug
				targ = targ.parentNode;
			}
		}
		return targ;
	};
	

	//public stuff
	return{
		init : function(){
			if(pageTracker !== undefined){ //no point doing anything if there is no GA
				_attachListeners();
			}
		},
		
		trackme : function(e){
			_preventDefault(e);
			var eTarget = _getTarget(e);
			var iLoop = 0;
			while(eTarget.tagName !== "A" && iLoop <= 5){
				eTarget = eTarget.parentNode;
				iLoop = iLoop + 1;
			}
			if (eTarget.href !== undefined) {
				pageTracker._trackPageview("/external/" + eTarget.href);
				var popUpWin = window.open(eTarget.href,'','');
				popUpWin.focus();
			}
			return false;
		},
		
		addEventListener : function(obj, type, action){
			_addEventListener(obj, type, action);
		},
		
		removeEventListener : function(obj, type, action){
			_removeEventListener(obj, type, action);
		}
	};
	
}();

/**
 * some basic tests to ensure the script isn't missing anything
 */

RemoveEventListener = window.RemoveEventListener || newmind.googleanalytics.externallinks.removeEventListener;
AddEventListener = window.AddEventListener || newmind.googleanalytics.externallinks.addEventListener;

if (externalLinks !== undefined) { //if we have the external links code running stop it *note we cant do this in the init as its too late :(
	RemoveEventListener(window, 'load', externalLinks);
}

/** 
 * fire init on load
 */
AddEventListener(window, "load", newmind.googleanalytics.externallinks.init);