// Polish Weekly code: dd/mm/yyyy with 0 - #.000,00 - dot
$(document).ready(function(){ 
	$.get("http://www.pokerstars.pl/data/leader-board/weekly.xml",{},function(xml){
	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>Ranking tygodniowy: od ';
		HTMLOutput += fromLocalDay + '/';
		HTMLOutput += fromLocalMonth + '/';
		HTMLOutput += $(this).find("from_year").text() + ' do ';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += $(this).find("to_year").text() + '.</h3>';		

	 	HTMLOutput += '<table class="table" width="60%"><tr><th>Miejsce</th><th>ID U&#380;ytkownika</th><th>Kraj</th><th>Punkty</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 - PL
			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">Ranking obejmuje wszystkie turnieje od godziny 23.59 czasu ET, ';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += $(this).find("to_year").text() + '.</span></p>';
		
		$("#writeWeekly").append(HTMLOutput);
	});
});
	
});
 
function buildTop20Table(place,name,country,points){
	// Place with dot
	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.pl/data/leader-board/matches.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="90%"><th>Data</th><th>Team PS Pro</th><th>Wynik</th><th>Gracz</th><th>Kwota</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 = 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 = "pokona&#322;";
			} else {
				var theResult="przegra&#322; z";
			}		
		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.pl/data/leader-board/records.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="90%"><th>Pro</th><th>Wygrane</th><th>Przegrane</th><th>Wskaznik<br/>procentowy</th><th>Passa</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="Wygrana";
			} else if ((theStreakKind == "yes") && (theStreakValue > 1)) {
				var theKind="Wygrane";
			} else if ((theStreakKind == "no") && (theStreakValue == 1)) {
				var theKind="Przegrana";
			} else {
				var theKind="Przegrane";
			}					
		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;
}
