///////
// gaAddonsPlus.js
// (c) BetterPlace
// Changelog:
//  v2.2 - Mar. 9 2009 -

// Indicate each file extension that needs to be tracked, fileTypes is the regular expression that matches downloadable files
var fileTypes = new RegExp(/\.(docx*|xlsx*|doc*|xls*|ppt*|tar*|bzip*|pptx*|exe|zip|pdf|xpi)$/i);

///////
/// No need to change anything below this line
// Initialize external link and download handlers
if (document.getElementsByTagName)
{
    var hrefs = document.getElementsByTagName('a');
    for (var l = 0, m = hrefs.length; l < m; l++) 
        if (hrefs[l].hostname == location.host && fileTypes.test(hrefs[l].pathname)) 
            startListening(hrefs[l], "click", trackDocuments);
        else 
            startListening(hrefs[l], "click", trackExternalLinks);
}

function getFirstDivWithId(elmnt)
{
	// return the next element with an id set.
	while (elmnt.id == "")
		elmnt = elmnt.parentNode;

	//	alert("id:" + elmnt.id );

	return elmnt.id;
}

function startListening(obj, evnt, func)
{
    if (obj.addEventListener) 
        obj.addEventListener(evnt, func, false);
    else 
        if (obj.attachEvent) 
            obj.attachEvent("on" + evnt, func);
}

function trackDocuments(evnt)
{
    if (typeof(pageTracker) != "object") 
        return;
    pageTracker._trackEvent("download - " + location.pathname, "click", (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname); 
}

function trackExternalLinks(evnt)
{
  	// if pageTracker is not an object exit  
    if (typeof(pageTracker) != "object") 
        return;
    
    var elmnt = evnt.srcElement;
    
    // Catagory includes current path - so we know where peoople are coming from
    var sCatagory = "outbound - " + location.pathname;

	// confirm we have the current Anchor element 
    if (elmnt) 
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
    else
    	elmnt = this;

	//Only continue if this link REALLY goes offsite
	if (elmnt.hostname != location.host )
	{
 		if (((location.pathname == "/" ) || (location.pathname == "/staging/homepage" )) && (  getFirstDivWithId(this.parentNode) != "" ))
			sCatagory = sCatagory + " - " + getFirstDivWithId(this.parentNode);
     
		pageTracker._trackEvent(sCatagory, "click", elmnt.hostname + elmnt.pathname + elmnt.search);
        	
		//alert(sCatagory + ", click, " + this.hostname + this.pathname + this.search); 
	} 
}
/// EOF ///
