// See notes at end of file

var strHost = document.location.host;
var cookieDomain = strHost; //'.north49.com';
if (!document.tagConfigFunc) document.tagConfigFunc = function () { gaTagConfig(true, true, true, false); };

function mainSendData(strPageName) {
	var mainTracker = _gat._getTracker(document.WebPropertyId || 'UA-2083022-1');

	mainTracker._setDomainName(cookieDomain);

	mainTracker._setAllowHash(true);

	document.strTrackPageView = strPageName || document.strTrackPageView || '';

	mainTracker._trackPageview(document.strTrackPageView);
}


function safeAddOnload(func) {
    v = 'v3.1.0 2006-11-22; like:; req:;';
    if (!window.__load_events) {
        var init = function () {
            if (arguments.callee.done) return;
            arguments.callee.done = true;
            if (window.__load_timer) {
                clearInterval(window.__load_timer);
                window.__load_timer = null;
                /*@cc_on @*/
                /*@if (@_win32) document.getElementById("__ie_onload").onreadystatechange = "";/*@end @*/
            }
            for (var i = 0; i < window.__load_events.length; i++) {
                window.__load_events[i]()
            }
            window.__load_events = null
        };
        if (document.addEventListener) {
            document.addEventListener("DOMContentLoaded", init, false)
        }
        /*@cc_on @*/
        /*@if (@_win32) var proto = "src='javascript:void(0)'";if (location.protocol == "https:") proto = "src=//0";document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");var script=document.getElementById("__ie_onload");script.onreadystatechange=function(){if(this.readyState=="complete"){init()}};
		/*@end @*/
        if (/WebKit/i.test(navigator.userAgent)) {
            window.__load_timer = setInterval(function () {
                if (/loaded|complete/.test(document.readyState)) {
                    init()
                }
            },
            10)
        }
        window.onload = init;
        window.__load_events = []
    }
    window.__load_events.push(func)
}
	

/*	Configuration options required:
	1.	Select which external links to listen for
	2.	If document downloads, specify which ones or accept the default list.
*/
	var isDocs = true;
	var isMailto = true;
	var	isExternal = true;
	var isVirtual = true;
	var patternDocslist = 
/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/;


	// Syntax: gaTagConfig([true|false|string of bar-delimited extensions [, true|false [, true|false [,  true|false]]]]) ; If all false, don't bother loading script
function gaTagConfig(docslist, mailto, external, virtual) {
	
	isDocs = (typeof docslist == 'boolean') ? docslist : docslist > '';	// Determine isDocs 
	
	if (typeof docslist == 'string' && docslist > '') 
		patternDocslist = new RegExp('\\.(?:' + docslist + ')($|\\&|\\?)'); // updated by erikvold  added \? in second group:
		
	isMailto = (mailto !== undefined) ? mailto : true;
	isExternal = (external !== undefined) ? external : true;
    isVirtual = (virtual !== undefined) ? virtual : true;

	
	gaAutoTag();
}

function gaAutoTag() {

	if ((isDocs || isMailto || isExternal || isVirtual) && document.getElementsByTagName) {

		// Initialize external link handlers
	        var hrefs = document.getElementsByTagName("a");
	        for (var h = 0; h < hrefs.length; h++) {

					// try {} catch{} block added by erikvold 
				try{
		                //protocol, host, hostname, port, pathname, search, hash
		                if (isMailto && hrefs[h].protocol == "mailto:") {
		                        startListening(hrefs[h],"click",trackMailto);
		                } else if (isDocs && hrefs[h].hostname == location.host) { //BK  080820
		                        var path = hrefs[h].pathname + hrefs[h].search;
								
								var isDoc = path.match(patternDocslist);							
								
		                        if (isDoc) {
		                            startListening(hrefs[h],"click",trackDownloads);
		                        }
		                } else if (isExternal && hrefs[h].hostname  && hrefs[h].hostname !== location.host) {
								startListening(hrefs[h],"mousedown",trackExternalLinks);
		                }
				}  catch(e){
					continue;
				}
	        }
	}
}

function startListening (obj,evnt,func) {
        if (obj.addEventListener) {
                obj.addEventListener(evnt,func,false);
        } else if (obj.attachEvent) {
                obj.attachEvent("on" + evnt,func);
        }
		if(document.cookie.search(/debug(ging)?=(?!no)[^;]*/i) !== -1)
			obj.innerHTML += '.';
}

function trackMailto (evnt) {
        var href = evnt.srcElement ? evnt.srcElement.href : this.href;
        var mailto = "/mailto/" + href.substring(7);
		if (typeof(mainTracker) == "object") mainSendData(mailto); 
}

function trackExternalLinks (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;
		if (e.hostname !== location.host) lnk = "/external/" + e.hostname + lnk; 
		
		mainSendData(lnk);
}

function trackDownloads (evnt) {
        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName !== "A") {
                e = e.parentNode;
        }
		var lnk = '/downloads' + (e.pathname.charAt(0) == "/" ? '' : "/" ) ; 
		lnk += e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
		if (e.hostname !== location.host) lnk = "/external/" + e.hostname + lnk; 
		
		mainSendData(lnk);
}

function createCookie(name,value,days) {
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; domain=" + cookieDomain + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


mainSendData();
safeAddOnload(document.tagConfigFunc);

//	This javascript automatically sends calls to the GA server.
//	It also automatically tags file downloads and external links in Google Analytics.
//	The new Google Analytics Tracking Code (ga.js) is required
//	This file must be loaded as a script on all pages just below the  code that loads GA.js.  
//	See the accompanying example test html file 
//
//	By default all external links, mailto and external downloads are donfigured to be tracked by called gaTagConfig() in this script file 
//  	To chage the default for the whole site, edit the call to 
//	gaTagConfig(false, true, true, false) below 
//	To chage for an existing page, define a new function and assign it to document.tagConfigFunc
//	Please see the accompanying  test html file for an example.

