//	Javascript to tag file downloads and external links in Google Analytics.
//	To use, place reference to this file should be placed at the bottom 
//    of all pages, just above the Google Analytics tracking code.
//	All outbound links and links to non-html files
//    should now be automatically tracked.
//  (BTW: Use 2 space tab size for proper viewing)
//
//	(Nov 11) UPDATE: Now accounts for human errors on hyperlinks 
//    when developers put too many slashes in the
//	  url.
//
//  (Nov 28) UPDATE: Now can track more than one profile.
//
//  (Dec 6) UPDATE: Now works in all browsers. Tracks code for WIKI
//
//  (Jan 29, 2008) UPDATE: Found many logic errors. Replanned/Redid logic 
//    for reporting.
//    Also merged this file with general GA code for tracking html pages.
//
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	Created by: 	Colm McBarron, colm.mcbarron@iqcontent.com
//  Modified by Sean Neilan @ metristpartners.com for ok-labs.com
//	Last updated: 	January 9, 2007
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

// Track the loaded page in all browsers.
if (typeof window.location != "undefined")
{
	host_name = window.location.hostname;
	path_name = window.location.pathname;
	search_query = window.location.search;
	anchor_hash = window.location.hash;
	
	if (!host_name.match(/^\www\./))
	{
		host_name = "www." + host_name;
	}
	
	// Trim /'s at end of url
	//  (Do not do on homepage)
	if (path_name != "/")
	{
		path_name = path_name.replace(/\/$/, "");
	}
	
	// If there is no beginning slash, add one
}

if (typeof document.location != "undefined")
{
	host_name = document.location.hostname;
	path_name = document.location.pathname;
	search_query = document.location.search;
	anchor_hash = document.location.hash;

	if (!host_name.match(/^\www\./))
	{
		host_name = "www." + host_name;
	}
	
  // Trim /'s at end of url
  //  (Do not do on homepage)
	if (path_name != "/")
	{
		path_name = path_name.replace(/\/$/, "");
	}
}

_uff = false; // reset Google Analytics
// Log click into Wiki profile_uacct = "UA-1818088-1";urchinTracker(path_name + search_query + anchor_hash);
//alert(path_name + search_query + anchor_hash);
_uff = false; // reset Google Analytics_udn = "ok-labs.com";
// Log click into Combined profile_uacct = "UA-1818088-3";
// To maintain absolute consistency across all reports, they _all_ must have a beginning slashurchinTracker("/" + host_name + path_name + search_query + anchor_hash);
//alert("/" + host_name + path_name + search_query + anchor_hash);

// Get the target of a link.
// Works across browsers.
function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

// Attach event listener to any object.
// Works across browsers.
function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return  functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true; 
}

var hrefs = document.getElementsByTagName("a");

for (var l = 0; l < hrefs.length; l++)
{
	// Add a page tracker to all anchors.
	attachEventListener(hrefs[l], "click", trackfiles, false);
}

// Reports all clicks to non-html/external pages.
function trackfiles(array_element)
{
	// If this variable is still false at the end, don't report anything
	// If still true, clicked link is an internal HTML file
	// Email Ken or Avery for use cases document to see exactly what is reported.
	url_report = false;
	
	// Get reference to link that was clicked on.
	var obj = getEventTarget(array_element);
	
	// If clicked object is an image, get link that contains the image
	if (obj.nodeName != "A")
	{
	   obj = obj.parentNode;
	}
	
	// We modify this data and then always
	//   report host_name + path_name + search_uri.
	// If we don't want to report host_name, leave it blank.
	// (Same with other variables.)
	host_name   = "";
	path_name   = "";
	search_uri  = "";
	anchor_hash = "";
	
  /*
  For the rest of this document, we report clicks for two different
    Google Analytics accounts with different reporting logic.
  So, we calculate the click for each account separately according to its logic.
  See the use cases document to see how different this logic is.
  */
  // --- Reporting code for Corporate Profile ----------------------------------
  if (obj.hostname.match(/ok\-labs\.com/))
  {
		if (obj.pathname.match(/\.(pdf|tar|gz|sha1|md5|txt|zip)$/)) // If non html file
		{
			host_name   = ""; // Don't log hostname for internal PDF links on corp account
			search_uri  = ""; // Don't log search uri for internal PDF links on corp accont
			path_name   = obj.pathname; // Log path for internal PDF links on corp account
			anchor_hash = ""; // Don't log hash for internal PDF links on corp account
			
			url_report = true; // report url
		}
		
		if (obj.hostname.match(/wiki/)) // If link to wiki.ok-labs.com
		{
			host_name   = "/wiki/"; // Label hostname for wiki links on corp account.
			path_name   = obj.pathname; // Log pathname for wiki links on corp account.
			search_uri  = obj.search; // Log search uri for wiki links on corp account.
			anchor_hash = obj.hash; // Log hash for wiki links on corp account
			
			url_report = true; // report url
		}
  }
 	else // If offsite (or an email)
	{
		if (obj.href.match(/mailto/)) // Log and label mailto links on corp account
	  {	  	
	  	// We aren't really logging a normal link.
	  	//   It's an email address. So, just stick the
	  	//   entire email in host_name & change everything else
	  	//   to blank. This will log the email
	  	host_name = obj.href; // Take the entire mailto link & log it
	  	path_name = "";
	  	search_uri = "";
	  	anchor_hash = "";
	  	
	  	url_report = true; // report url
	  }
	  else
	  {
		  host_name  = "/offsite/" + obj.hostname; // Log and label offsite links on corp account.
		  path_name  = obj.pathname; // Log path name on offsite links on corp account.
		  search_uri = obj.search; // Log search uri for offsite links on corp account.
		  anchor_hash = obj.hash; // Log hash for offsite links on corp account
		  
		  url_report = true; // report url
	  }
	}
 	
 	// If we should report the url
 	if (url_report == true)
 	{
 		url_report = host_name + path_name + search_uri + anchor_hash;
 		
	 	// Replace all double //'s with single /'s
	  while (url_report.match(/\/\//) != null)
	  {
	    url_report = url_report.replace(/\/\//g, "/");
	  }
	  
	  // Trim /'s at end of url
	  url_report = url_report.replace(/\/$/, "");
	  
	  // If there is no beginning slash, add one
	  if (url_report.match(/^\//) == null)
	  {
	  	url_report = "/" + url_report;
	  }
	  
    _uff = false; // reset google analytics
    
    // Submit url_report to Corporate profile
		_uacct = "UA-1818088-1";
		urchinTracker(url_report);
		//alert(url_report);
 	}
 	
 	// --- End reporting code for corporate profile  -----------------------------
 	
 	// --- Begin reporting code for Combined profile -----------------------------
	
	// We modify this data and then always
	//   report host_name + path_name + search_uri.
	// If we don't want to report host_name, leave it blank.
	// (Same with other variables.)
	host_name  = "";
	path_name  = "";
	search_uri = "";
	
  // Reset our lil' reporting mechaschismo here..
 	url_report = false;
 	
  // See use cases document for info on how this all works.
	if (obj.hostname.match(/ok\-labs\.com/)) // If link to ok-labs.com
	{
		if (obj.pathname.match(/\.(pdf|tar|gz|sha1|md5|txt|zip)$/)) // If match nonhtml file
		{
			if (obj.hostname.match(/wiki/))
			{
				host_name  = "wiki.ok-labs.com"; // Log hostname for PDF links on combined account.
			}
			else
			{
				host_name  = "www.ok-labs.com"; // Log hostname for PDF links on combined account.
			}
			
			path_name  = obj.pathname; // Log pathname for internal PDF links on combined account.
			
			search_uri = ""; 					 // Don't log search uri for internal PDF links on combined
																 //   account.
			anchor_hash = ""; // Don't log hash for internal pdf links on combined account
			
			url_report = true; // report url
		}
	}
	else // If offsite (or an email)
	{
		if (obj.href.match(/mailto/)) // Log and label mailto links on combined account
	  {	  	
	  	// We aren't really logging a normal link.
	  	//   It's an email address. So, just stick the
	  	//   entire email in host_name & change everything else
	  	//   to blank. This will log the email
	  	host_name = obj.href; // Take the entire mailto link & log it
	  	path_name = "";
	  	search_uri = "";
	  	anchor_hash = "";
	  	
	  	url_report = true; // report url
	  }
	  else
		{
			host_name  = "/offsite/" + obj.hostname; // Log/label hostname for external
																							 //   links on combined account.
			path_name  = obj.pathname; 						 	 // Log pathname for wiki links 
																							 //   on combined account.
			search_uri = obj.search;   							 // Log search uri for wiki links
																							 //   on combined account.
			anchor_hash = obj.hash;                  // Log hash for external links on
																							 //   on combined account.
		  
		  url_report = true; // report url
		}
	}
	
	if (url_report == true)
	{
		url_report = host_name + path_name + search_uri + anchor_hash;
		
		// Replace all double //'s with single /'s.
	  while (url_report.match(/\/\//) != null)
	  {
	    url_report = url_report.replace(/\/\//g, "/");
	  }
	  
	  // Trim /'s at end of url.
	  url_report = url_report.replace(/\/$/, "");
	  
	  // If there is no beginning slash, add one
	  if (url_report.match(/^\//) == null)
	  {
	  	url_report = "/" + url_report;
	  }
		
		_uff = false; // reset google analytics (GA)
		// line required by GA to report to ok-labs.com & wiki.ok-labs.com
		_udn = "ok-labs.com";
		
		// Submit url_report to combined profile
		_uacct = "UA-1818088-3";
		urchinTracker(url_report); // Report it again.
		//alert(url_report);
	}
	
	// --- End reporting code for Combined profile --------------------------------
}

