//	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.
//
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	Created by: 	Colm McBarron, colm.mcbarron@iqcontent.com
//	Last updated: 	12-Feb-2006
//	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//

var hrefs = document.getElementsByTagName("a");
var link_path = "";
for (var l = 0; l < hrefs.length; l++) {
		try {
			var link_path = hrefs[l].pathname;
			if (location.host == hrefs[l].hostname) {
				if (link_path.match(/\.(doc|pdf|xls|ppt|zip|txt|wma|mov|avi|wmv|mp3)$/)) {
					addtrackerlistener(hrefs[l]);
				}
			} else {
				addtrackerlistener(hrefs[l]);
			}
		}
		catch(err) { }
}


function addtrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackfiles, true);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackfiles);
	}
}

function trackfiles(array_element) {

	var file_path = "";
    var targetHostname = "";

	var e = (array_element.srcElement) ? array_element.srcElement : this;

	while (e.tagName != "A") {
		e = e.parentNode;
	}

	var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;

    if (!array_element.currentTarget) { // For Internet Explorer
        // works in IE
        //targetHostName = array_element.srcElement.hostname;
        targetHostName = e.hostname;
        targetHost = array_element.srcElement;

    } else {        // For Firefox
        // works in FF
    	var e = (array_element.currentTarget) ? array_element.currentTarget : this;
    	//while (e.tagName != "A") {
    	//	e = e.parentNode;
    	//}
        targetHostName = array_element.currentTarget.host;
        targetHost = array_element.currentTarget;
    }

    var url_lnk = e.toString() ;
    var cs_Match = url_lnk.search('PAGEID');        // (1) Search if clicked link is to regular CommonSpot (html) page
    var targetHostMatch = url_lnk.search('/');      // (2) Search if clicked link is internal docs url
    var externalLinkMatch = url_lnk.search('@');    // (3) Search if clicked link is external url CommonSpot generated w/ javascript
    var targetLinkHttp = url_lnk.search('http');    // (4) Search if clicked link is external url
      
    var sectionName = window.location.pathname;     // Get URL path     (ex: /meetings/topics/pipeline/index.cfm)
    var sectionArray = new Array();
    sectionArray = sectionName.split("/");          // Split sections into array
    sectionName = "";
    for (i=0; i < sectionArray.length-1; i++)       // loop through array to re-compile section url w/o page file name (last array element).
    {
        sectionName = sectionName + '/' + (sectionArray[i]);
    } 

    //sectionName = sectionName.replace("http://" + location.host, "");
    //if ( sectionName.substring(sectionName.length-1, sectionName.length) == "/" ) {   
    //    sectionName = sectionName.substring(0, sectionName.length-1); }       // Trim off trailing slash "/"

    var parsed_url_lnk = url_lnk.substring(targetHostMatch, url_lnk.length - 3);    // If int. doc link (2) or ext. lnk (3) trim trailing chars:  ');

    //alert ('sectionName = ' + sectionName + '\n\n' + 'sectionArray = ' + sectionArray + '\n\n' + 'url_lnk = ' + url_lnk + '\n\n' + 'cs_Match = ' + cs_Match + '\n\n' + 'targetHostMatch = ' + targetHostMatch + '\n\n' + 'externalLinkMatch = ' + externalLinkMatch + '\n\n' + 'parsed_url_lnk = ' + parsed_url_lnk + '\n\n' + 'targetLinkHttp = ' + targetLinkHttp + '\n\n' + 'location.host = ' + location.host + '\n\n' + 'e.hostname = ' + e.hostname + '\n\n' + 'location.host.search = ' + location.host.search('www.api.org') + '\n\n' + 'window.location.pathname = ' + window.location.pathname );

    // For external links or non html docs (use extracted/parsed link pass to GA)
    if ( cs_Match != -1 || (cs_Match == -1 && externalLinkMatch > 0) ) {
        file_path = parsed_url_lnk; 
        }

    /*
    if (  typeof(array_element.srcElement.hostname) != "undefined" && 
          location.host != array_element.srcElement.hostname && 
          array_element.srcElement.hostname.match('CP___PAGEID')== null ) { */

    // For external links (use extracted/parsed link passing to GA)
    if ( typeof(targetHostName) != "undefined" && (location.host != e.hostname) && ( cs_Match == -1 || (cs_Match > 0 && targetLinkHttp > 0 ) ) && file_path.search('www.api.org') == -1 ) {

        if ( externalLinkMatch > 0 ) {    // CommonSpot Javascript type external link
            file_path = file_path.replace("//", '/');
            if ( file_path.substring(file_path.length-1, file_path.length) == "/" ) {   
                file_path = file_path.substring(0, file_path.length-1);  }       // Trim off trailing slash "/"
       		file_path = sectionName + "/extlinks/" + file_path ;
            file_path = file_path.replace("//", '/');
        }
        else {  // regular CommonSpot external link (manual target='_blank' type)
       		file_path = sectionName + "/extlinks/" + ((targetHost) ? "/" + e.hostname : this.hostname);
            file_path = file_path.replace("//", '/');
        }
	}

    if ( file_path.search("www.api.org") > 0 ) {    // Get rid of user hardcoded url
        file_path = file_path.replace("//www.api.org", "");
    }

    if ( file_path == "" ) {
        file_path = lnk; 
    }

    file_path = file_path.replace("');", '');     // trim off trailing chars:  ');
    file_path = file_path.replace("//", '/');     // replace extraneous leading double slash chars: "//" with single slash

    //alert ('file_path = ' + file_path);
    // urchinTracker(file_path);

    pageTracker._trackPageview(file_path);
}



