/****************************************************
     Author: Brian J Clifton
     Url: http://www.advanced-web-metrics.com
     This script is free to use as long as this info is left in
     
     Combined script for tracking external links, file downloads and mailto links
     
     All scripts presented have been tested and validated by the author and are believed to be correct
     as of the date of publication or posting. The Google Analytics software on which they depend is 
     subject to change, however; and therefore no warranty is expressed or implied that they will
     work as described in the future. Always check the most current Google Analytics documentation.

****************************************************/

// Only links written to the page (already in the DOM) will be tagged
// This version is for ga.js (July 15th 2008)

function addLinkerEvents() {
	var as = document.getElementsByTagName("a");
	// List of local sites that should not be treated as an outbound link.
	var extTrack = ["fondationbombardier.ca","bombardierfoundation.ca","bombardier.net"];
	//List of file extensions on your site
	var extDoc = [".doc",".xls",".exe",".zip",".pdf",".mp3"];
	
	/*If you edit no further below this line, Top Content will report as follows:
    /custom/exit/url-of-external-site
    /downloads/filename
    /mailto/email-address-clicked
	*/
	
	for(var i=0; i<as.length; i++) {
		var flag = 0;
		var current_host = null;
		var tmp = as[i].getAttribute("onclick");
		
		// IE6-IE7 fix (null values error) by Julien Bissonnette
		if (tmp != null) {
		  tmp = String(tmp);
		  if (tmp.indexOf('urchinTracker') > -1 || tmp.indexOf('_trackPageview') > -1) continue;
		}
		
		// Fix Magnet to prevent exit links on <a> tags without href attribute
		if(as[i].href.length > 0) {
    
			// Tracking outbound links
			for (var j=0; j<extTrack.length; j++) {					
				if (as[i].href.indexOf(extTrack[j]) == -1 && as[i].href.indexOf('google-analytics.com') == -1 && as[i].href.toLowerCase().indexOf('javascript:') == -1 && as[i].href.toLowerCase().indexOf('mailto:') == -1) {
					flag++;
				}
				else {
				  current_host = extTrack[j];
				}
			}
			if (flag == extTrack.length){
				var splitResult = as[i].href.split("//");
				as[i].setAttribute("onclick", "pageTracker._trackPageview('/custom/exit/" +splitResult[1]+ "');"+((tmp != null) ? tmp : ""));
			}			

			if (flag != extTrack.length) {
			  // Tracking electronic documents - doc, xls, pdf, exe, zip
				for (var j=0; j<extDoc.length; j++) {
					if (as[i].href.toLowerCase().indexOf(extDoc[j]) != -1) {
						var splitResult = as[i].href.split(current_host);
						as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + "pageTracker._trackPageview('/custom/downloads" +splitResult[1]+ "');");
						break;
					}
				}

			  // Tracking mailto links
				if (as[i].href.indexOf("mailto:") != -1) {
					var splitResult = as[i].href.split(":");
					as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + "pageTracker._trackPageview('/custom/mailto/" +splitResult[1]+ "');");
				}
			}
		}
	}
}

try {
addLinkerEvents();
} catch(err) {}
