 var RFTWresults = new RFTWdekiInit();

$wpjQ(document).ready(function(){
	var getData = {};
	// if no leaguetype and season passed in, find it.
	if (!rftwMR.lt){
		getData.lt=RFTWresults.lt;
		$wpjQ.getJSON("http://"+ RFTWresults.jsonurl + "/wp-content/themes/rftw/json.php?seasons&callback=?",{},
				function(data){
					RFTWresults.objSeasons=data;
					getData.s = RFTWresults.objSeasons[RFTWresults.lt][0];
					$wpjQ.getJSON("http://"+ RFTWresults.jsonurl + "/wp-content/themes/rftw/json.php?meet_results&callback=?",getData,function(data){ processAllMeets(data); });
				});
	}
	else{
		$wpjQ.getJSON("http://"+ RFTWresults.jsonurl + "/wp-content/themes/rftw/json.php?meet_results&callback=?",rftwMR,function(data){ processAllMeets(data); });
	}
});

function processAllMeets(data) {
	var output = "";
	var season;
	var pagelinks;
	if (!rftwMR.s){
		season =  RFTWresults.objSeasons[RFTWresults.lt][0];
		pagelinks=RFTWresults.lt;
	}
	else{
		season =  rftwMR.s;
		pagelinks =rftwMR.lt;
	}

	output += "<table class='api-data all-meets' width='440'>";
    output += "<caption class='w440'>" + season + " " + RFTWresults.pagelinks[pagelinks].replace(/_/,' '); + " Results " + "</caption>";    
    output += "<thead class='all-meets'>";
    output += "<tr>";
   	output += "<th class=''>Date</th>";
   	output += "<th class=''>League</th>";
   	output += "<th class=''>Div</th>";
   	output += "<th class=''>Teams</th>";
   	output += "<th class=''>Result</th>";
   	output += "</tr>";
   	output += "</thead>";
   	output += "<tbody class='all-meets'>";
	$wpjQ.each(data, function() {
		output += processEachMeet(this);
	});
	output += "</tbody>"
	output += "</table>";
	$wpjQ("#all_meets").html(output);
}

function processEachMeet(meet) {

	var output = "";
	output += "<tr>"
	output += "<td>" + meet.meet_date_display + "</td>";
	output += "<td>" + "<a href='" + getLeagueHref(meet.league_name_link)  + "'>" + meet.league_abbrev + "</a></td>";
	output += "<td class='division'>";
	if (meet.division && meet.division != "n/a") {
		output += meet.division;
	} else {
		output += "&mdash;"
	}
	output += "</td>";
	output += printTeams(meet.teams, meet.league_name_link,meet.meet_type);
	if (meet.meet_type.toLowerCase() == 'dual meet'){
		output += printScores(meet.teams, meet.meet_id);
	}
	else{
		output += "<td class='last division'>";
		output += "<a href='"+ Deki.BaseHref + "/Results_Statistics/Meet_Results?meetId=" + meet.meet_id + "&type=" + RFTWresults.lt + "'>Results</a>";
		output += "</td>";
		
	}
	output += "</tr>";
	return output;
}

function printTeams(teams, league, meettype) {
	var output = "";
	output += "<td class='division'>";
	var count = 1;
	if (meettype.toLowerCase() == 'dual meet'){
		$wpjQ.each(teams, function() {
			output += "<a href='" + getLeagueHref(league) + "/" + this.team_name_link +  "'>" + this.team_abbrev + "</a>";
			if (count < teams.length) {
				output += " : ";
			}
			count++;
		});	
	}
	else{
		output += "<a href='" + getLeagueHref(league) + "'>" + meettype + "</a>";
	}
	output += "</td>";
	return output;
}

function printScores(teams, meetId) {
	var output = "";
	output += "<td class='last division'>";
	var count = 1;
	$wpjQ.each(teams, function() {
		if (this.points_scored && this.points_scored > 0) {
			var round_points = (this.points_scored*10)%10 == 0 ? Math.round(this.points_scored) : Math.round(this.points_scored*10)/10;
			output += "<a href='"+ Deki.BaseHref + "/Results_Statistics/Meet_Results?meetId=" + meetId + "&type=" + RFTWresults.lt + "'>" + round_points + "</a>";
		} else {
			output += "&ndash;";
		}
		if (count < teams.length) {
			output += " : ";
		}
		count++;
	});	
	output += "</td>";
	return output;
}
function getLeagueHref(leagueNameLink) {
	return Deki.BaseHref + "/" + RFTWresults.pagelinks[RFTWresults.lt] + "/" + leagueNameLink;
}


