//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Written by Sean Neilan for Metrist Partners
//  Based on idea by Colm McBarron
//	Last updated: 	January 9, 2007
//  Taglinks for keefersrestaurant.com version707
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

// 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-11360415-1");
pageTracker._setDomainName("keefersrestaurant.com");
pageTracker._setAllowLinker(true);
//pageTracker._trackPageview(path_name + search_query + anchor_hash);  // this is the first automatic call to Google Analytics when the page is loaded // commented out AJC 1/8/2010
//alert("1: "+path_name + search_query + anchor_hash);

function Taglinks(ua, domain, allowLinker) {
    this.pageTracker = _gat._getTracker(ua);
    this.pageTracker._setDomainName(domain);
	this.pageTracker._setAllowLinker(allowLinker);
	this.trackPageView = trackPageView;     //  gives the object an instance of trackPageView with which it can call Google 
	this.gaTrackEvent = gaTrackEvent;         //
	//alert("A: "+ua+domain+allowLinker);
}

// what is this and why is it here? 
// Bad naming -- relying on UpperCase/LowerCase to differentiate!
function trackPageView(url) {
    this.pageTracker._trackPageview(url);//alert
	//alert("B: "+url);
}

function gaTrackEvent(eventCategory, eventAction, eventLabel) {
	//alert("J1: " + eventLabel);
	// //this.pageTracker._initData();
	this.pageTracker._trackEvent(eventCategory, eventAction, eventLabel);
	//alert("J2: " + eventLabel);
}

// now we are back to code that executes in Taglinks -- 
var taglinks = new Taglinks("UA-11360415-1", "keefersrestaurant.com", true);
taglinks.trackPageView(path_name + search_query + anchor_hash);  // This is calling the Taglinks Function that calls Google Analytics -- second call to Google Analytics!
//alert("C: "+path_name + search_query + anchor_hash);

// Get the target of all links, track the offsite links and onsite downloads.
// 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
	var url_report = "No Report";
	
	// Get reference to link that was clicked on.
	var obj = getEventTarget(array_element);
	
	//Get reference for a java object
	
	// 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 = "";
	
 	
  // See use cases document for info on how this all works.
	if (obj.hostname.match(/keefersrestaurant\.com/)) // If link to the current website.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 
			
			search_uri = ""; 					 // Don't log search uri for internal PDF links on combined
																 //   account.
			anchor_hash = ""; // Don't log hash for internal pdf links 
			
			url_report = "pageView"; // report url
		}
	}
	else // If offsite (or an email)  (or a javascript call)
	{
		if (obj.href.match(/mailto/)) // Log and label mailto links 
	  {	  	
	  	// 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 = "pageView"; // report url
	  }
	  else if (obj.href.match(/javascript/))   // a javascript executed
	    {
			host_name  = "/javascript/" //    + obj.hostname; // Log/label hostname for external
																							 //   links on.
			path_name  = obj.onclick;	//gets the javascript funciton					 	 
			search_uri = "";   							 
			anchor_hash = "";
			//alert("javascript");
			//alert("jfull: "+ path_name);
			//path_name = path_name.substr(2,10); // substring method does not work because path_name is not a string.
			//alert("jx: "+ path_name);
			//j_obj = j_obj.substring(jobj.indexOf("'"));
			//j_obj = j_obj.substring(0, jobj.indexOf("'"));
			url_report = "event";  // event report
		}
	  else
		{
			host_name  = "/offsite/" + obj.hostname; // Log/label hostname for external
																							 //   links on 
			path_name  = obj.pathname; 						 	 // Log pathname  
			search_uri = obj.search;   							 // Log search uri 
			anchor_hash = obj.hash;                  // Log hash for external links 
			
			url_report = "pageView"; // report url
		}
	}
	
	if (url_report.match("pageView") != null)
	{
		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;
	  }
		
		taglinks.trackPageView(url_report); // Call Google Analytics from inside Taglinks
		//alert("O: "+url_report);

	}
	else if (url_report.match("event") != null)
	{
		// Report event to Google Analytics
		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;
		}
		
		// call track event for javascript objects
		//alert("J0: "+url_report);
		taglinks.gaTrackEvent("Javascript", "OnClick", url_report);
		//alert("J3: "+url_report);
	}
	  
	// --- End reporting code 
}

