//
//	>>>>> Taglinks.js
//	Javascript to tag file downloads and external links in Google Analytics
//
//	 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	Created by: 		Colm McBarron, colm.mcbarron@iqcontent.com
//	Modified by: 	Sean Neilan,  www.metristpartners.com  20-June-2007
//                                       summarize downloads into categories, to compensate for lack of data  when using IE
//	Last updated: 	21-June-2007
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

var hrefs = document.getElementsByTagName("a"); // get all the anchors into this array

if (navigator.appName == "Microsoft Internet Explorer") // if the browser is IE
{

for (l = 0; l < hrefs.length; l++) // for all of the links
{
	try // we have this try block just to make sure..
	{
		link_path = hrefs[l].pathname;
		host_path = hrefs[l].hostname;
			if (host_path == "www.justmfg.com" || host_path == "justmfg.com") // If the hostname of the link is www.justmfg.com or justmfg.com
			{
				// If the extension is pdf, attach justmfg_trackpdf(evt) to the link to report the click to UrchinTracker as /justmfg.com/pdf
				if (link_path.match(/pdf/))
				{
					addtrackerlistener(hrefs[l], "justmfg_trackpdf");
				}
				
				// If the extension is dxf, attach justmfg_trackdxf(evt) to the link to report the click to UrchinTracker as /justmfg.com/dxf
				if (link_path.match(/dxf/))
				{
					addtrackerlistener(hrefs[l], "justmfg_trackdxf");
				}
			}
			else // and if it's not justmfg.com
			{
				if (host_path.match(/online\.justmfg\.com/)) // If the hostname of the link is online.justmfg.com
				{
					if (!link_path.match(/asp/)) // and if it's NOT an ASP extension
					{
						// If the extension is pdf, attach onlinejustmfg_trackpdf(evt) to the link to report the click to UrchinTracker as /online.justmfg.com/pdf
						if (link_path.match(/pdf/))
						{
							addtrackerlistener(hrefs[l], "onlinejustmfg_trackpdf");
						}
						
						// If the extension is dxf, attach onlinejustmfg_trackdxf(evt) to the link to report the click to UrchinTracker as /online.justmfg.com/dxf
						if (link_path.match(/dxf/))
						{
							// if it's an "R14 DXF" file, then do the special function
							if (link_path.match(/CAD/))
							{
								addtrackerlistener(hrefs[l], "onlinejustmfg_trackcaddxf");
							}
							else // it must be just a normal dxf file otherwise
							{
								addtrackerlistener(hrefs[l], "onlinejustmfg_trackdxf");
							}
							// this code is totally nuts
							// darn it microsoft!
						}
					}
				}
				else // if the hostname is justsinks or something we don't know about
				{
					// if the hostname is justsinks.com, attach justsinks_track(evt) to the link to report the click to UrchinTracker as /offsite/justsinks.com
					if (host_path.match(/justsinks\.com/))
					{
						addtrackerlistener(hrefs[l], "justsinks_track");
					}
					else // if the domain isn't justsinks.com, justmfg.com or online.justmfg.com, attach offsite_track(evt) to the link to report the click to UrchinTracker as /offsite/
					{
						addtrackerlistener(hrefs[l], "offsite_track");
					}
				}
			}
	}
	catch(err) { }
}
}

if (navigator.appName == "Netscape") // if the browser is firefox
{
try
{
	for (var l = 0; l < hrefs.length; l++) // for all of the links
	{
		link_path = hrefs[l].pathname;
		if (location.host == hrefs[l].hostname)
		{
			if (link_path.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|dxf)$/))
			{
				addtrackerlistenerfirefox(hrefs[l]);
			}
		} 
		else 
		{
			addtrackerlistenerfirefox(hrefs[l]);
		}
	}
}
	catch(err) { }
}

function addtrackerlistener(obj, report) // this function takes two variables,
// the first variable is the object to attach an event to
// the second variable is the type of report to urchintracker
{
	if (report == "justmfg_trackpdf")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', justmfg_trackpdf, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', justmfg_trackpdf);
		}
	}
	
	if (report == "justmfg_trackdxf")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', justmfg_trackdxf, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', justmfg_trackdxf);
		}
	}
	
	if (report == "onlinejustmfg_trackpdf")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', onlinejustmfg_trackpdf, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', onlinejustmfg_trackpdf);
		}
	}
	
	if (report == "onlinejustmfg_trackdxf")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', onlinejustmfg_trackdxf, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', onlinejustmfg_trackdxf);
		}
	}
	
	if (report == "onlinejustmfg_trackcaddxf")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', onlinejustmfg_trackcaddxf, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', onlinejustmfg_trackcaddxf);
		}
	}
	
	if (report == "justsinks_track")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', justsinks_track, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', justsinks_track);
		}
	}
	
	if (report == "offsite_track")
	{
		if (obj.addEventListener)
		{
			obj.addEventListener('click', offsite_track, true);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent("on" + 'click', offsite_track);
		}
	}
}

function justmfg_trackpdf(evt)
{
	file_path = "/PDF/unknown_pdf";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function justmfg_trackdxf(evt)
{
	file_path = "/DXF/unknown_dxf";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function onlinejustmfg_trackpdf(evt)
{
	file_path = "/online.justmfg.com/unknown_pdf";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function onlinejustmfg_trackdxf(evt)
{
	file_path = "/online.justmfg.com/DXF/unknown_dxf";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function onlinejustmfg_trackcaddxf(evt)
{
	file_path = "/online.justmfg.com/DXF/CAD/unknown_dxf";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function justsinks_track(evt)
{
	file_path = "/offsite/justsinks.com";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function offsite_track(evt)
{
	file_path = "/offsite/domain.com/unknown";
	_uacct = "UA-1076869-1";
	urchinTracker(file_path);
}

function addtrackerlistenerfirefox(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackfiles, true);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackfiles);
	}
}

function trackfiles(array_element) // this code is for firefox
{
	file_path = "";
	if (this.hostname == "www.justmfg.com" || this.hostname == "justmfg.com") // If it's an internal link to one of the above filetypes
	{
		file_path = this.pathname + this.search; // Then report the click without the domain
	}
	else
	{
		if (this.hostname == "online.justmfg.com") // If it's a link to our subdomain
		{
			if (!this.pathname.match(/asp/)) // And if it's not an ASP extension
			{
				file_path = "online.justmfg.com" + this.pathname + this.search; // Then call urchinTracker to report the click
			}

		}

		else

		{
			file_path = "offsite/" + this.hostname + this.pathname + this.search; // If it's an offsite link, report it as offsite/
		}
	}
	if (file_path != "")
	{
		_uacct = "UA-1076869-1";
		urchinTracker(file_path);
	}
}
