//	This javascript tags file downloads and external links in Google Analytics.
//	You need to be using the Google Analytics New Tracking Code (ga.js) 
//	for this script to work.
//	To use, place this file on all pages just above the Google Analytics tracking code.
//	All outbound links and links to non-html files should now be automatically tracked.
//
//	This script has been provided by Goodwebpractices.com
//	Thanks to ShoreTel, MerryMan and Colm McBarron
//
//	www.goodwebpractices.com
//	VKI has made changes as indicated below.								
//
//	March 2011 - Kent House alterations for compat. w/ new GA code

jQuery(document).ready(function()
{
        // Initialize external link handlers
        var hrefs = document.getElementsByTagName("a");
        for (var m = 0; m < hrefs.length; m++) {
				// try {} catch{} block added by erikvold VKI
			try{
	                //protocol, host, hostname, port, pathname, search, hash
	                if (hrefs[m].protocol == "mailto:") {
	                        startListening(hrefs[m],"click",trackMailto);
	                } else if (hrefs[m].hostname == location.host) {
	                        var path = hrefs[m].pathname + hrefs[m].search;
				var isDoc = path.match(/\.(?:doc|docx|eps|jpg|jpeg|png|svg|xls|xlsx|ppt|pptx|pdf|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
	                        if (isDoc) {
	                                startListening(hrefs[m],"click",trackDocumentLinks);
	                        }
	                } else {
	                        startListening(hrefs[m],"click",trackExternalLinks);
	                }
			}
			catch(e){
					continue;
			}
        }
});

function startListening (obj,evnt,func) {
	jQuery(obj).bind(evnt,func);
}

function trackMailto (evnt) {
        var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
        var mailto = href.substring(7);
        if (typeof(_gaq) == "object") _gaq.push(['_trackEvent','Mailto','Click mailto:',mailto]);
}

function trackDocumentLinks (evnt) 
{
	var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname == location.host) 
	{
        	if (typeof(_gaq) == "object") 
		{
        		_gaq.push(['_trackEvent','Document','Access hosted document',lnk]);
		}		
	}
	else
	{
		lnk = e.hostname + lnk;
        	if (typeof(_gaq) == "object") 
		{
        		_gaq.push(['_trackEvent','Document','Access external document',lnk]);
		}		
	}
	setTimeout('document.location = "' + lnk + '"', 100);
	return false;
}


function trackExternalLinks (evnt) 
{
	var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
	if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) 
	{
        	if (e.hostname != location.host) lnk = e.hostname + lnk;
        	if (typeof(_gaq) == "object") 
		{
        		_gaq.push(['_trackEvent','External page','Access external page',lnk]);
		}
		return true;
	}
	else
	{
        	_gaq.push(['_trackPageview',lnk]);
	}
	setTimeout('document.location = "' + lnk + '"', 100);
	return false;
}

