//
// This script was developed by Justin Cutroni
//  By default, this script will track links to the follwoing file 
//  types: .doc, .xls, .exe, .zip and .pdf
//
//
// This variable controls how outbound links will appear
// the GA reports.  By default, external links will appear as
// '/outbound/<URL>' where URL is the URL in the anchor tag.
//
// Reworked logic, added cross domain linking, updated for ga.js
// -JH

//var ew = window.open('about:blank','ErrorWindow'); // debug setup
//var debug = ""; // debug setup
window.onload = listenToClicks;
// The domains[] array is the array of all domains that you wish to do cross-domain tracking across , using the pageTracker._link() function from ga.js


var extIdentifier  = '/outbound/';
var docIdentifier  = '/download';
// listenToClicks should be called once after the DOM is loaded, such as with <body onLoad="listenToClicks();">
function listenToClicks() { 
	var domains    = []; // array of domains to use _link(); on. For www.otherdomain.com you would just use "otherdomain" in the array
	var fileTypes  = [".doc",".xls",".exe",".zip",".pdf"]; // array of file types to track
	var found = []; // flow control variable used later
	
	if (document.getElementsByTagName) {
  		var aTags = document.getElementsByTagName('a'); // get all the <a> tags and put them in hrefs
		for (var i = 0; i < aTags.length; i++) { //loop through hrefs and decide if we want any event handlers associated with each <a> tag
			if (aTags[i]==""){continue;} // if the href is empty go to the next iteration of the href loop
//			if (aTags[i].hostname == location.host) { // if the href in the <a> tag points to the same full domain we're on, we just try to track downloads. use this or the next 2, depending on how you want to treat subdomains
      h = location.host.split("."); l = h.length - 1; // **TO DO change this to use mydomain.com, mydomain.org instead   - use with next line
      if (aTags[i].hostname.indexOf(h[l]) != -1){// if we want to treat sub-domains as the same domain use this line and previous line instead. 
			//if (aTags[i].hostname.indexOf(location.host) != -1 || location.host.indexOf(aTags[i].hostname) != -1){ 
				var path = aTags[i].pathname;
				for (j = 0; j < fileTypes.length; j++){ // loop through the array of document types to track, if we find one then an create event handler and stop looking
					if (path.indexOf(fileTypes[j]) != -1) {startListening(aTags[i],"click",trackDocuments);} // break since we don't need to finish the loop
				}
			} else { // if the href in the <a> tag is not for the domain we're currently on, we either want to track it as an exit link, or use _link(); function to pass cookies
				for (var j = 0; j < domains.length; j++){ // loop through the array of domains, if our href is for one of the domains in our list, we create an event handler to call _link();
					if (aTags[i].hostname.indexOf(domains[j]) != -1)
					{startListening(aTags[i],"click",useLinker); found[i] = 1; break;}
				}
				if (found[i] != 1){startListening(aTags[i],"click",trackExternalLinks);} // if our loop through domains turned up nothing, then found[i] will not be = 1 and we'll track it as an exit link
			}
		}
	}
}

// creates an event handler using browser specific method (addEventListener or AttachEvent) for our 3 functions below.
function startListening (obj,evnt,func) { 
  if (obj.addEventListener) { //debug += obj + "  " + func + "<br /><br />";
    obj.addEventListener(evnt,func,false);
  } else if (obj.attachEvent) { 
    obj.attachEvent("on" + evnt,func);
  }
}

// useLinker:  calls _link(); on the href in the <a> tag in question
function useLinker (evnt) {  //debug += "inside useLinker<br /><br />";
  var lnk;
  if (evnt.srcElement) {
    var elmnt = evnt.srcElement;
    while (elmnt.tagName != "a") {
      var newelmnt = elmnt.parentNode;
      elmnt = newelmnt;
    }
    lnk = "http://" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search;
  } else {
    lnk = "http://" + this.hostname + this.pathname + this.search;
  }

  if (typeof(pageTracker) == "object") { // make sure pageTracker is defined
	  pageTracker._link(lnk);
	  if (evnt.preventDefault) {evnt.preventDefault();}else{evnt.returnValue=false;} //ew.document.write(debug); // prevents the click from causing the default action for <a> tags(following the link)
    return false; //may not need this
	}
}

// trackDocuments:  calls _trackPageview before downloading a file
function trackDocuments (evnt) { //debug += "inside trackDocuments<br /><br />";
  var url = (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname; 
  url = docIdentifier + url;
  if (typeof(pageTracker) == "object") {//debug += "if pageTracker == object<br /><br />";
	  pageTracker._trackPageview(url);//ew.document.write(debug); 
  }
}

// trackExternalLinks:  calls _trackPageview before following an external link
function trackExternalLinks (evnt) { //debug += "inside trackExternalLinks<br /><br />";
  var lnk; 
  if (evnt.srcElement) { 
    var elmnt = evnt.srcElement; 
    while (elmnt.tagName != "a") { 
      var newelmnt = elmnt.parentNode; 
      elmnt = newelmnt; 
    } 
    lnk = extIdentifier +elmnt.hostname + "/" + elmnt.pathname + elmnt.search; 
  } else { 
    lnk = extIdentifier + this.hostname + this.pathname + this.search; 
  } 
  if (typeof(pageTracker) == "object") {//debug += "if pageTracker == object<br /><br />";
	 pageTracker._trackPageview(lnk); //ew.document.write(debug); 
	}
}

