///////
// gaAddons.js
// 	Author: StÃ©phane Hamel - <shamel67@gmail.com> - http://immeria.net
//  Contributors:
//	- Andy Edmonds - http://alwaysbetesting.com
//	- Damon Gudaitis - http://www.damongudaitis.com/
//	
///////
// License: Version: MPL 1.1
// The contents of this file are subject to the Mozilla Public License Version
// 1.1 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// Changelog:
//  v2.2 - Mar. 20nd 2009 - Fix issue with tracking download docs on current domain
//  v2.1 - Jan. 22nd 2009 - Set bUseEventForOutbout and bUserEventForDownload to toggle use of Events or Page Views
// 	v2.0 - Jan. 2009 - Use Google Analytics Events to track downloads and external links
//  v1.0 - Inspired by the work of Justin Cutroni - Google Analytics Short Cut - http://gashortcut.com/
///////

// Indicate each file extension that needs to be tracked, fileTypes is the regular expression that matches downloadable files
var fileTypes = new RegExp(/\.(docx*|xlsx*|pptx*|exe|zip|pdf|xpi)$/i);
var bUseEventForOutbound = true; // Set to false to use trackPageView for outbount links
var bUseEventForDownload = true; // Set to false to use trackPageView for downloads

///////
/// 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(fileTypes.test(hrefs[l].pathname))
	        if (hrefs[l].hostname == location.host) 
	            startListening(hrefs[l], "click", trackDocuments);
	        else 
	            startListening(hrefs[l], "click", trackExternalLinks);
}

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;
    bUseEventForDownload ? pageTracker._trackEvent("download", "click", (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname) : pageTracker._trackPageView("/download/" + (evnt.srcElement) ? "/" + evnt.srcElement.pathname : this.pathname);
}

function trackExternalLinks(evnt){
    if (typeof pageTracker != "object") 
        return;
    var elmnt = evnt.srcElement;
    if (elmnt) {
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
        bUseEventForOutbound ? pageTracker._trackEvent("outbound", "click", elmnt.hostname + "/" + elmnt.pathname + elmnt.search) : pageTracker._trackPageView("/outbound/" + elmnt.hostname + "/" + elmnt.pathname + elmnt.search);
    }
    else 
        bUseEventForOutbound ? pageTracker._trackEvent("outbound", "click", this.hostname + this.pathname + this.search) : pageTracker._trackPageView("/outbound/", this.hostname + this.pathname + this.search);
}
/// EOF ///

/*
if(/\.google\..+[?&]start=(\d+)/i.match(document.referrer.toString())) {
	if(typeof pageTracker == "object")
		pageTracker._trackEvent("Google SEO", "Page" + page, document.location.pathname, page);
}
*/
/*
 * var ref = document.referrer.toString();
if(ref.indexOf(".google.") > -1  && ref.indexOf("q=") > -1 ) {
	if(ref.indexOf("start") > -1) {
		if(ref.split("?").length) {					
			var arParams = ref.split("?")[1];							
			arParams = arParams.split("&");
			for(var i=0;i<arParams.length;i++) {
				if(arParams[i].indexOf("start=") > -1 ) {
						ssLogPageNum(1+arParams[i].split("=")[1]/10);
				}
			}
		} 
	} else {
		ssLogPageNum(1);
	}
}

function ssLogPageNum(page) {
	if(pageTracker != 'undefined') pageTracker._trackEvent("Google SEO", "Page" + page, document.location.pathname, page);
}
*/