//	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 Wilmette Chamber.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 (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(/\/$/, "");
	}
}

var pageTracker = _gat._getTracker("UA-8499306-1");
pageTracker._setDomainName("wilmettechamber.org");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview(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.
  */
 	// --- 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(/wilmettechamber\.org/)) // If link to Wilmette Chamber.com
	{
		if (obj.pathname.match(/\.(pdf|tar|gz|sha1|md5|txt|zip)$/)) // If match nonhtml file
		{
			host_name  = ""; // Log hostname for PDF links on main 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;
	  }
		// Submit url_report to combined profile
		pageTracker._trackPageview(url_report);  
	}
	
	// --- End reporting code for Combined profile --------------------------------
}
