// Czech Weekly code: d.m.yyyy without 0 - #.000,00 - . 
$(document).ready(function(){ 
	$.get("http://www.pokerstars.cz/data/leader-board/weekly.xml",{},function(xml){
	HTMLOutput = '';	
	$('date',xml).each(function(i) {
		var fromLocalMonth = $(this).find('from_month').text();
		var toLocalMonth = $(this).find('to_month').text();		
		var fromLocalDay = $(this).find('from_day').text();		
		var toLocalDay = $(this).find('to_day').text();
		
		HTMLOutput += '<h3>T&yacute;denn&iacute; &#382;eb&#345;&iacute;&#269;ek: ';
		HTMLOutput += fromLocalDay + '.';
		HTMLOutput += fromLocalMonth + '.';
		HTMLOutput += $(this).find("from_year").text() + ' - ';
		HTMLOutput += toLocalDay + '.';
		HTMLOutput += toLocalMonth + '.';
		HTMLOutput += $(this).find("to_year").text() + '.</h3>';		

	 	HTMLOutput += '<table class="table" width="60%"><tr><th>Po&#345;ad&iacute;</th><th>U&#382;ivatelsk&eacute; ID</th><th>Zem&#283;</th><th>Bod&#367;</th></tr>';
		
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("userID").text();
			country = $(this).find("country").text();
			
			// Thousands with Dots and decimals with comma - CZ
			var iniPoints = $(this).find("points").text();
			var makingLocal_Points = iniPoints.replace(/\,/g, ".");
			var points = makingLocal_Points.replace(/((\d+)\.(\d{3}))\.(\d{2})/, "$1,$4"); // $1 thousands only,$2 1st decimal only, $3 the three hundreds, $4 two decimals
			
			mydata = buildTop20Table(place,name,country,points);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p><span class="strong">Po&#345;ad&iacute; zahrnuje v&#353;echny turnaje od ';
		HTMLOutput += toLocalDay + '.';
		HTMLOutput += toLocalMonth + '.';
		HTMLOutput += $(this).find("to_year").text() + ', 23:59.</span></p>';
		
		$("#writeWeekly").append(HTMLOutput);
	});
});
	
});
 
function buildTop20Table(place,name,country,points){
	
	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;
}
// The 5 matches results
$(document).ready(function(){ 
	$.get("http://www.pokerstars.cz/data/leader-board/matches.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="80%"><th>Datum</th><th>Team PS Pro</th><th>V&yacute;sledek</th><th>Hr&aacute;&#269;</th><th>&#268;&aacute;stka</th>';
	$('match',xml).each(function(i) {
		var theLocalMonth = $(this).find('month').text();		
		var theLocalDay = $(this).find('day').text();
		var theYear = $(this).find('year').text();
		var theDate = theLocalDay + '.' + theLocalMonth + '.' + theYear;		
		var thePro = $(this).find('pro').text();
		//Bits needed in the Results row
		var theResultValue = $(this).find('result').text();
			if (theResultValue == 1) {
				var theResult = "vyhr&aacute;l nad";
			} else {
				var theResult="prohr&aacute;l s";
			}		
		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;
}
//Records table
$(document).ready(function(){ 
	$.get("http://www.pokerstars.cz/data/leader-board/records.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="80%"><th>Pro</th><th>V&yacute;hry</th><th>Prohry</th><th>% v&yacute;hern&iacute; pod&iacute;l</th><th>V&yacute;sledek</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);
		var commaRate = toStringRate.replace(/[^0-9]/, ",");
			if (commaRate.match(/(\d+)\,(\d+)/)) {
				var theRate = commaRate;
			}
			else {
				var theRate = commaRate + ',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="v&yacute;hra";
			} else if ((theStreakKind == "yes") && (theStreakValue > 1)) {
				var theKind="v&yacute;hry";
			} else if ((theStreakKind == "no") && (theStreakValue == 1)) {
				var theKind="prohra";
			} else {
				var theKind="prohry";
			}					
		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;
}
