

$(document).ready(function(){
	// create function to redirect users to the new registration page
	$("a[href*='register']").attr("href",function(){
		var sRegex = new RegExp(/https:\/\/register.spartanrace.com\/register\/\?event=([0-9]+)/i);
		var sHref = this.href + "";
		var aMatch = sRegex.exec(sHref);
		if(aMatch[1]){
			return ("./register.php?eventID=" + aMatch[1]);
		}
	});

   $(".register_button").parent().click(function(){
		
		
		
		var sType = $(this).html();
		var sTypeRegex = new RegExp (/.*RESULTS.*/i);
		var bResults = sTypeRegex.test(sType);
		sType = bResults ? "Results Button" : "More Info Button";
		
		var sState = $(this).prev('a').html();
		var sDate = $(this).parent().html();
		var iImg = sDate.indexOf(">");
		sDate = sDate.substr(iImg + 1);
		var iA = sDate.indexOf("<a");
		sDate = $.trim(sDate.substr(0, iA));
		var sEvent = $.trim(sState) + " " + $.trim(sDate);
		// _gaq.push(['_trackEvent', 'Registration Button', sEvent, 'Home Page']); // comment out async code in favor of traditional code
		
		// if the state or date are still null, then it's probably the event on the top right, use a regex to get that info
		if( sState==null || sDate==null ){
			var sText = $(this).parent().parent().prev().html() == "" ? $(this).parent().parent().prev().prev().html() : $(this).parent().parent().prev().html();
			raceRegex = new RegExp(/^.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*$/i);
			aMatches = raceRegex.exec(sText);
			
			if(aMatches){
				var sState = $.trim(aMatches[2]).length == 2 ? $.trim(aMatches[1]) : $.trim(aMatches[1]) + " " + $.trim(aMatches[2]);
				var sMonth = $.trim(aMatches[3]);
				var sDay = $.trim(aMatches[4]);
				sDay = sDay.substr(0,sDay.length - 2)
				var sYear = $.trim(aMatches[5]);
				var sDate = sMonth + " " + sDay + ", " + sYear;
				var dDate = new Date(Date.parse(sDate));
				var sDate = (dDate.getMonth() + 1) + "/" + dDate.getDate();
				var sEvent = (sState + " " + sDate);
				var sType = "Registration Button";
			} else {
				raceRegex = new RegExp(/^.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*<cufontext>(.+)<\/cufontext>.*$/i);
				aMatches = raceRegex.exec(sText);
				var sState = $.trim(aMatches[1]);
				var sMonth = $.trim(aMatches[2]);
				var sDay = $.trim(aMatches[3]);
				sDay = sDay.substr(0,sDay.length - 2)
				var sYear = $.trim(aMatches[4]);
				var sDate = sMonth + " " + sDay + ", " + sYear;
				var dDate = new Date(Date.parse(sDate));
				var sDate = (dDate.getMonth() + 1) + "/" + dDate.getDate();
				var sEvent = (sState + " " + sDate);
				var sType = "Registration Button";
			}
		}
		
		// pageTracker._trackEvent(sType, sEvent, 'Home Page'); // this is to track both types of buttons (registration and results)
		if(!( bResults || sState==null || sDate==null )){  // this is to track only registration buttons
			//var image = new Image(1,1);
			//image.src = "http://www.googleadservices.com/pagead/conversion/1032666616/?label=lHHeCMiC_gEQ-Pu07AM&amp;guid=ON&amp;script=0";
			pageTracker._trackEvent(sType, sEvent, 'Home Page'); 
		}
		

		
		return;
   });
});

