var plstart = new Date();
window.onload=function()
{
	var plend = new Date();

	// call function to rewrite all external facing href links
	addLinkerEvents();

	var plload = plend.getTime() - plstart.getTime();
	
	// determine the thresholds
	if(plload<2000) lc = "Very Fast";
	else if (plload<5000) lc = "Fast";
	else if (plload<10000) lc = "Medium";
	else if (plload<30000) lc = "Sluggish";
	else if (plload<45000) lc = "Slow";
	else lc = "Very Slow";
	var fn = document.location.pathname;
	if( document.location.search)
	fn += document.location.search;

	try {
		pageTracker._trackEvent("Page Load",lc+" Loading Pages",fn,Math.round(plload/1000));
	}
	catch(err) { }
}

function addLinkerEvents()
{
	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 = ["areastartups.com","atlanta.areastartups.com","austin.areastartups.com","boston.areastartups.com","chicago.areastartups.com","dallas.areastartups.com","dc.areastartups.com","denver.areastartups.com","detroit.areastartups.com","houston.areastartups.com","losangeles.areastartups.com","miami.areastartups.com","minneapolis.areastartups.com","newyork.areastartups.com","philadelphia.areastartups.com","phoenix.areastartups.com","portland.areastartups.com","raleigh.areastartups.com","sandiego.areastartups.com","sanfrancisco.areastartups.com","sanjose.areastartups.com","seattle.areastartups.com","london.areastartups.com","paris.areastartups.com","toronto.areastartups.com","vancouver.areastartups.com"];
	
	// List of file extensions on your site. Add/edit as you require
	var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js"];
	
	var section = String(window.location);
	if (section.indexOf('atlanta.areastartups.com') > -1) section = 'Atlanta';
	else if (section.indexOf('austin.areastartups.com') > -1) section = 'Austin';
	else if (section.indexOf('boston.areastartups.com') > -1) section = 'Boston';
	else if (section.indexOf('chicago.areastartups.com') > -1) section = 'Chicago';
	else if (section.indexOf('dallas.areastartups.com') > -1) section = 'Dallas';
	else if (section.indexOf('dc.areastartups.com') > -1) section = 'DC';
	else if (section.indexOf('denver.areastartups.com') > -1) section = 'Denver';
	else if (section.indexOf('detroit.areastartups.com') > -1) section = 'Detroit';
	else if (section.indexOf('houston.areastartups.com') > -1) section = 'Houston';
	else if (section.indexOf('losangeles.areastartups.com') > -1) section = 'Los Angeles';
	else if (section.indexOf('miami.areastartups.com') > -1) section = 'Miami';
	else if (section.indexOf('minneapolis.areastartups.com') > -1) section = 'Minneapolis';
	else if (section.indexOf('newyork.areastartups.com') > -1) section = 'New York';
	else if (section.indexOf('philadelphia.areastartups.com') > -1) section = 'Philadelphia';
	else if (section.indexOf('phoenix.areastartups.com') > -1) section = 'Phoenix';
	else if (section.indexOf('portland.areastartups.com') > -1) section = 'Portland';
	else if (section.indexOf('raleigh.areastartups.com') > -1) section = 'Raleigh';
	else if (section.indexOf('sandiego.areastartups.com') > -1) section = 'San Diego';
	else if (section.indexOf('sanfrancisco.areastartups.com') > -1) section = 'San Francisco';
	else if (section.indexOf('sanjose.areastartups.com') > -1) section = 'San Jose';
	else if (section.indexOf('seattle.areastartups.com') > -1) section = 'Seattle';
	else if (section.indexOf('london.areastartups.com') > -1) section = 'London';
	else if (section.indexOf('paris.areastartups.com') > -1) section = 'Paris';
	else if (section.indexOf('toronto.areastartups.com') > -1) section = 'Toronto';
	else if (section.indexOf('vancouver.areastartups.com') > -1) section = 'Vancouver';
	else section = 'WWW';

	for(var i=0; i<as.length; i++)
	{
		var flag = 0;
		var tmp = as[i].getAttribute("onclick");

		// IE6-IE7 fix (null values error) with thanks to Julien Bissonnette for this
		if (tmp !== null) {
			tmp = String(tmp);
			if (tmp.indexOf('urchinTracker') > -1) continue;
			if (tmp.indexOf('_trackPageview') > -1) continue;
			if (tmp.indexOf('_trackEvent') > -1) continue;
			if (tmp.indexOf('addthis') > -1) continue;
			if (tmp.indexOf('trackClick') > -1) continue;
		}

		// Tracking outbound links off site - not the GATC
		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++;
			}
		}
		
		if (flag == extTrack.length && as[i].href.indexOf("mailto:") == -1){
			as[i].onclick = function(){
				var splitResult = this.href.split("//");
//				pageTracker._trackPageview('/outgoing/' +splitResult[1]) + ";" +((tmp !== null) ? tmp+";" : "");
				pageTracker._trackEvent('Outbound Clicks',section,splitResult[1]) + ";" +((tmp !== null) ? tmp+";" : "");
			};
		}

		for (j=0; j<extDoc.length; j++) {
			if (as[i].href.indexOf(extTrack[0]) != -1 && as[i].href.indexOf(extDoc[j]) != -1) {
				as[i].onclick = function(){
					var splitResult = this.href.split(extTrack[0]);
//					pageTracker._trackPageview('/downloads' +splitResult[1])+";"+((tmp !== null) ? tmp+";" : "");
					pageTracker._trackEvent('Downloads',section,splitResult[1]) + ";" +((tmp !== null) ? tmp+";" : "");
				};
				break;
			}
		}

		if (as[i].href.indexOf("mailto:") != -1) {
			as[i].onclick = function(){
				var splitResult = this.href.split(":");
//				pageTracker._trackPageview('/mailto/' +splitResult[1])+ ";"+((tmp !== null) ? tmp+";" : "");
				pageTracker._trackEvent('Mail To',section,splitResult[1]) + ";" +((tmp !== null) ? tmp+";" : "");
			};
		}
	}
}