//	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

addEvent(window, "load", trackLinks, false);

// onload event handler: set up (external) link handlers
function trackLinks()
{
	if (document.getElementsByTagName)
	{
		var hrefs = document.getElementsByTagName("a");
		for (var i = 0; i < hrefs.length; i++)
		{
			try
			{
				//protocol, host, hostname, port, pathname, search, hash
				if (hrefs[i].protocol == "mailto:")
				{	// add all mailto links
					addEvent(hrefs[i], "click", trackLink);
				}
				else if (hrefs[i].hostname == location.host)
				{	// add internal links only if they point to a document
					var path = hrefs[i].pathname + hrefs[i].search;
					var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
					if (isDoc)
					{
						addEvent(hrefs[i], "click", trackLink);
					}
				}
				else
				{	// add all external links
					addEvent(hrefs[i], "click", trackLink);
				}
			}
			catch(e)
			{
				continue;
			}
		}
	}
}

// Event handler
function trackLink(evt)
{
	evt = (evt) ? evt : (window.event) ? window.event : "";
	var elt = getTargetElement(evt);
	if (elt)
	{
		while (elt.tagName.toLowerCase() != "a" && elt.parentNode)
		{
			elt = elt.parentNode;
		}
		if (elt.tagName.toLowerCase() == "a")
		{
			var mailto = "mailto:";
			var link;
			if (elt.protocol && elt.protocol == "mailto:")
			{
				link = "/mailto/" + elt.href.substring(mailto.length);
			}
			else
			{
				link = (elt.pathname.charAt(0) == "/") ? elt.pathname : "/" + elt.pathname;
				if (elt.search && elt.pathname.indexOf(elt.search) == -1)
				{
					link += elt.search;
				}
				if (elt.hostname != location.host)
				{
					link = "/external/" + elt.hostname + link;
				}
			} 
			if (typeof(pageTracker) == "object")
			{
				pageTracker._trackPageview(link);
			}
		}
	}
}

// Event helper methods
function addEvent(elt, evtType, func, useCapture)
{
  if (elt.addEventListener)
  {
	 elt.addEventListener(evtType, func, useCapture);
	 return true;
  }
  else if (elt.attachEvent)
  {
	 var result = elt.attachEvent("on" + evtType, func);
	 return result;
  }
}

function getTargetElement(evt)
{
	var elt = null;
	if (evt)
	{
		if (evt.target)
		{
			if (evt.target.nodeType && evt.target.nodeType == 3)
			{
				elt = evt.target.parentNode;
			}
			else
			{
				elt = evt.target;
			}
		}
		else
		{
			elt = evt.srcElement;
		}
	}
	return elt;
}

