/****************************************************
     Author: Brian J Clifton
     Url: http://www.advanced-web-metrics.com/scripts
     This script is free to use as long as this info is left in
     
     Combined script for tracking external links, file downloads and mailto links
     
     All scripts presented have been tested and validated by the author and are believed to be correct
     as of the date of publication or posting. The Google Analytics software on which they depend is 
     subject to change, however; and therefore no warranty is expressed or implied that they will
     work as described in the future. Always check the most current Google Analytics documentation.

     Thanks to Nick Mikailovski (Google) for intitial discussions & Holger Tempel from webalytics.de
     for pointing out the original flaw of doing this in IE.

****************************************************/
// Only links written to the page (already in the DOM) will be tagged
// This version is for ga.js (last updated Jan 15th 2009)

//  Amended for Exeter City Council
//  Tony Lock - July 2010
//  Update to use addEventListener functionality as per
//  gatag.js script from www.goodwebpractices.com

function addLinkerEvents() {

  if (document.getElementsByTagName) {

    var as = document.getElementsByTagName("a");
    // List of local sites that should not be treated as an outbound link. Include at least your own domain here
    var extTrack = ["www.exeter.gov.uk","www.exeterdev.gov.uk","localhost"];
    
    //List of file extensions on your site. Add/edit as you require
    //Not required for ECC
    //var extDoc = [".doc",".xls",".pdf"];
    
    /*If you edit no further below this line, Top Content will report as follows:
      /external/url-of-external-site
      /downloads/filename
      /mailto/email-address-clicked
    */

    for(var i=0; i<as.length; i++) {
    
      try {
        
          // ECC bespoke content from Goss has pages
          // where the content is changed by AJAX javascript
          // This script needs to tag those events to ensure this script is run again
          var script = (as[i].onclick == null) ? '' : String(as[i].onclick);
          if (script.indexOf("iCM.DefaultPanel.ChangePanel") != -1) {
                  //alert (script);
            startListening(as[i], "click", changePanel);
          }
                
          // Tracking outbound links off site - not the GATC
          var flag = 0;
          for (var j=0; j<extTrack.length; j++) {          
            if (as[i].href.indexOf(extTrack[j]) == -1 && as[i].href.indexOf('google-analytics.com') == -1 ) {
              flag++;
            }
          }
          
          // Exclude any hrefs without http:// etc
          if (as[i].protocol != ':') {
	          if (as[i].protocol == "mailto:") {
	            startListening(as[i], "click", trackMailto);
	          } else if (as[i].href.indexOf("CHttpHandler.ashx") != -1){
	            startListening(as[i], "click", trackDownloads);
	          } else if (flag == extTrack.length){
	            startListening(as[i], "click", trackExternal);
	          }      
        	}
        
      
      }
  
      catch(e) {
        //alert (e);
        continue;
      }
      
    }
    
  }
    
}

//  MS:  IE uses attachEvent
//  W3C: Mozilla, Opera etc. use addEventListener

function startListening (obj,evnt,func) {

        //alert ('Listening: ' + obj);
        
        // For IE ensure that duplicates functions are not executed
        // by detaching the function (whether it exists or not)
        
        if (obj.addEventListener) {
            obj.addEventListener(evnt,func,false);
        } else if (obj.attachEvent) {
                if (obj.detachEvent) {
                    obj.detachEvent("on" + evnt,func);
                }
                obj.attachEvent("on" + evnt,func);
        }
}

function trackMailto (evnt) {

        var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
        var mailto = "/mailto/" + href.substring(7);
        //alert (mailto);
        if (typeof(_gaq) == "object") _gaq.push(['trackPageview', mailto]);
}

//  MS:  IE has access to the variable    evnt
//  W3C: Mozilla, Opera etc. use        this

function trackExternal (evnt) {

        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }

        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        lnk = "/external/" + e.hostname + lnk;
        //alert (lnk);
        if (typeof(_gaq) == "object") _gaq.push(['trackPageview', lnk]);
}

function trackDownloads (evnt) {

        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }

        download = "/downloads/" + e.title;
        download = download.replace(/ : this link.*window/i, '');
        //alert (download);
        if (typeof(_gaq) == "object") _gaq.push(['trackPageview', download]); 
}

function changePanel(evnt) {
  
  // To allow time for iCM Change Panel code to run and load new content ...
  //alert ('changePanel');
  setTimeout("addLinkerEvents();",1000);
  
}
