// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the xml file
	$.get("/data/leader-board/weekly.xml",{},function(xml){
      	
	// Build an HTML string
	HTMLOutput = '';
	
	$('date',xml).each(function(i) {
		var fromMonth = $(this).find('from_month').text();
		  if (fromMonth < 10) {
			 fromLocalMonth = '0' + fromMonth;
		  } else {
			 fromLocalMonth = fromMonth;
		  }
		var toMonth = $(this).find('to_month').text();
		  if (toMonth < 10) {
			 toLocalMonth = '0' + toMonth;
		  } else {
			 toLocalMonth = toMonth;
		  }
		var fromDay = $(this).find('from_day').text();
		  if (fromDay < 10) {
			 fromLocalDay = '0' + fromDay;
		  } else {
			 fromLocalDay = fromDay;
		  }
		var toDay = $(this).find('to_day').text();
		  if (toDay < 10) {
			 toLocalDay = '0' + toDay;
		  } else {
			 toLocalDay = toDay;
		  }  
		HTMLOutput += '<h3>Weekly rankings: ';
		HTMLOutput += fromLocalMonth + '/';
		HTMLOutput += fromLocalDay + '/';
		HTMLOutput += $(this).find("from_year").text() + ' to ';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += $(this).find("to_year").text() + '.</h3>';		

	 	HTMLOutput += '<table class="table" width="60%"><tr><th>Place</th><th>User ID</th><th>Country</th><th>Points</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("userID").text();
			country = $(this).find("country").text();
			points = $(this).find("points").text();
			
			// Build row HTML data and store in string
			mydata = buildTop20Table(place,name,country,points);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p><span class="strong">Standings include all tournaments started as of ';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += $(this).find("to_year").text() + ' at 11:59 pm.</span></p>';
		
		// Update the DIV called Content Area with the HTML string
		$("#writeWeekly").append(HTMLOutput);
	});
});
	
});
 
function buildTop20Table(place,name,country,points){
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td>'+ place + '</td>';
	output += '<td>'+ name +'</td>';
	output += '<td style="text-align:center;">'+ country +'</td>';
	output += '<td>'+ points +'</td>';
	output += '</tr>';
	return output;
}
//$(document).ready(function(){ 
//	$.get("/data/leader-board/records.xml",{},function(xml){
//	myHTMLOutput = '';
//	myHTMLOutput += '<table class="table" width="80%"><th>Pro</th><th>Won</th><th>Lost</th><th>% Win Rate</th><th>Streak</th>';
//	$('pro',xml).each(function(i) {
//		var theName = $(this).find('name').text();
//		var theWin = Number($(this).find('win').text());
//		var theLoss = Number($(this).find('loss').text());
//		var theTotalMatches = theWin + theLoss;
//		var tempValue = Number((theWin*100)/theTotalMatches);
//		var percentRate = Math.round(tempValue*100)/100;
//		var toStringRate = String(percentRate);
//			if (toStringRate.match(/(\d+)\.(\d+)/)) {
//				var theRate = toStringRate;
//			}
//			else {
//				var theRate = toStringRate + '.00';
//			}
//						
//		//Bits needed in the Steaks row
//		var theStreakValue = $(this).find('streak').text();
//		var theStreakKind = $(this).find('streak').attr('win');
//			if ((theStreakKind == "yes") && (theStreakValue == 1)) {
//				var theKind="Win";
//			} else if ((theStreakKind == "yes") && (theStreakValue > 1)) {
//				var theKind="Wins";
//			} else if ((theStreakKind == "no") && (theStreakValue == 1)) {
//				var theKind="Loss";
//			} else {
//				var theKind="Losses";
//			}					
//		var theStreak = theStreakValue + " " + theKind;
//		//to add the dark color to the updated row
//		var theClass = $(this).attr('updated');
//		
//		mydata = buildHTML(theName,theWin,theLoss,theRate,theStreak,theClass);
//			myHTMLOutput = myHTMLOutput + mydata;
//		});
//		myHTMLOutput += '</table>';		
//		$("#writeRecord").append(myHTMLOutput);
//	});
//});
//function buildHTML(theName,theWin,theLoss,theRate,theStreak,theClass){
//	if (theClass == "yes") {
//		theClassHTML = " class='last'";
//	} 
//	else
//	{
//		theClassHTML = "";
//	}
//	
//	output = '';
//	output += '<tr' + theClassHTML + '>';
//	output += '<td>'+ theName + '</td>';
//	output += '<td>'+ theWin +'</td>';
//	output += '<td>'+ theLoss +'</td>';
//	output += '<td>'+ theRate +'</td>';
//	output += '<td>'+ theStreak +'</td>';
//	output += '</tr>';
//	return output;
//}
//$(document).ready(function(){ 
//	$.get("/data/leader-board/matches.xml",{},function(xml){
//	myHTMLOutput = '';
//	myHTMLOutput += '<table class="table" width="80%"><th>Date</th><th>Team PS Pro</th><th>Result</th><th>Player</th><th>Amount</th>';
//	$('match',xml).each(function(i) {
//		var theMonth = $(this).find('month').text();
//		  if (theMonth < 10) {
//			 theLocalMonth = '0' + theMonth;
//		  } else {
//			  theLocalMonth = theMonth;
//		  }
//		var theDay = $(this).find('day').text();
//		  if (theDay < 10) {
//			 theLocalDay = '0' + theDay;
//		  } else {
//			  theLocalDay = theDay;
//		  }
//		var theYear = $(this).find('year').text();
//		var theDate = theLocalMonth + '/' + theLocalDay + '/' + theYear;
//		
//		
//		
//		var thePro = $(this).find('pro').text();
//		//Bits needed in the Results row
//		var theResultValue = $(this).find('result').text();
//			if (theResultValue == 1) {
//				var theResult = "beat";
//			} else {
//				var theResult="lost to";
//			}		
//		var theUser = $(this).find('user').text();
//		var thePrizeValue = $(this).find('prize').text();
//		var thePrize = '$' + thePrizeValue + ',000';
//		//to add the dark color to the updated row
//		var theClass = $(this).attr('updated');
//		
//		mydata = buildHTML(theDate,thePro,theResult,theUser,thePrize,theClass);
//			myHTMLOutput = myHTMLOutput + mydata;
//		});
//		myHTMLOutput += '</table>';		
//		$("#writeMatches").append(myHTMLOutput);
//	});
//});
//function buildMatchesHTML(theDate,thePro,theResult,theUser,thePrize,theClass){
//	if (theClass == "yes") {
//		theClassHTML = " class='last'";
//	} 
//	else
//	{
//		theClassHTML = "";
//	}
//	
//	output = '';
//	output += '<tr' + theClassHTML + '>';
//	output += '<td>'+ theDate + '</td>';
//	output += '<td>'+ thePro +'</td>';
//	output += '<td>'+ theResult +'</td>';
//	output += '<td>'+ theUser +'</td>';
//	output += '<td>'+ thePrize +'</td>';
//	output += '</tr>';
//	return output;
//}
