	var RFTWresults = new RFTWdekiInit();
	var objLeegs = {"hs" : "High Schools","HS" : "High Schools", "summer" : "Summer Pools"};

$wpjQ(document).ready(function(){
	var getData = {};
	getData.result = "results_stats";
	getData.lt=RFTWresults.lt;
	$wpjQ.getJSON("http://"+ RFTWresults.jsonurl + "/wp-content/themes/rftw/json.php?seasons&callback=?",{},
			function(data){
				RFTWresults.objSeasons=data;
				printLinksToTopTimes();
				$wpjQ.getJSON("http://" + RFTWresults.jsonurl + "/wp-content/themes/rftw/json.php?" + getData.result + "&callback=?",getData,function(data){ processEventsTopTimes(data); });
			});
});

function processEventsTopTimes(data) {

	var output = "";
	var href="";
	var boysEvents = new Array();
	var girlsEvents = new Array();
	var hrefurl = Deki.BaseHref + "/Results_Statistics/Top_Times?type=" + RFTWresults.lt;

	$wpjQ.each(data, function() {
		if (this.event_gender == "M") {
			boysEvents.push(this);
		}
		if (this.event_gender == "F") {
			girlsEvents.push(this);
		}
	});
	var maxSize = Math.max(boysEvents.length, girlsEvents.length);
	var leegtype=objLeegs[RFTWresults.lt];
	
	output += "<table class='api-data events-top-times' width='440'>";
    output += "<caption class='w440'>Latest and Season Top Times for " + leegtype + "</caption>";    
    output += "<thead class='events-top-times'>";
    output += "<tr>";
   	output += "<th class=''>Boys</th>";
   	output += "<th class=''>Girls</th>";
   	output += "</tr>";
   	output += "</thead>";
   	output += "<tbody class='events-top-times'>";
	
	var i = 0;
	for (i=0; i<=maxSize-1; i++) {
		if (i%2==0) {
			output += "<tr class='even'>";
		} else {
			output += "<tr class='odd'>";
		}	
		output += "<td>";
		if (i<=boysEvents.length-1) {
			var displayName = boysEvents[i].display.replace("Boys ", "");
			displayName = displayName.replace("x ", "");
			
			href = hrefurl;
			href += "&event=" + boysEvents[i].stroke_link;
			href += "&length=" + boysEvents[i].swim_length;
			href += "&gender=" + boysEvents[i].event_gender;
			if ( boysEvents[i].agegroup){
				href += "&agegroup=" + boysEvents[i].agegroup;
			}
			output += "<a href='" + href + "'>" + displayName + "</a>";
		}
		output += "</td><td>";

		if (i<=girlsEvents.length-1) {
			var displayName = girlsEvents[i].display.replace("Girls ", "");
			displayName = displayName.replace("x ", "");
			
			href = hrefurl;
			href += "&event=" + girlsEvents[i].stroke_link;
			href += "&length=" + girlsEvents[i].swim_length;
			href += "&gender=" + girlsEvents[i].event_gender;
			if (girlsEvents[i].agegroup){
				href += "&agegroup=" + girlsEvents[i].agegroup;
			}
			output += "<a href='" + href + "'>" + displayName + "</a>";
		}
		output += "</td></tr>";
	}
	
	output += "</tbody></table>";

	$wpjQ("#events_top_times").html(output);
}
function printLinksToTopTimes(){
	// spit out a link for any other leagues... 
	var output="";
	for (var j in RFTWresults.objSeasons){
		if (j.toLowerCase() == "high school"){
			continue;
		}
		if (j != RFTWresults.lt){
			var loc = j.replace(/hs/i,"high school");
			output += "<a href='" + Deki.BaseHref + "/Results_Statistics?type=" + j + "'>";
			output += loc.substring(0,1).toUpperCase() + loc.substring(1);
			output += " league top times</a><br>";
		}
	}
	$wpjQ("#alt_top_times").html(output);
}

