// Russian Weekly code: dd/mm/yyyy with 0
$(document).ready(function(){ 
	$.get("http://www.pokerstars.com/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>&#1056;&#1077;&#1081;&#1090;&#1080;&#1085;&#1075;&#1080; &#1079;&#1072; &#1085;&#1077;&#1076;&#1077;&#1083;&#1102;: ';
		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>&#1052;&#1077;&#1089;&#1090;&#1086;</th><th>&#1048;&#1075;&#1088;&#1086;&#1082;</th><th>&#1057;&#1090;&#1088;&#1072;&#1085;&#1072;</th><th>&#1054;&#1095;&#1082;&#1080;</th></tr>';
		
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("userID").text();
			country = $(this).find("country").text();
			
			// Thousands with Space and decimals with comma - RU
			var iniPoints = $(this).find("points").text();
			var makingLocal_Points = iniPoints.replace(/\,/g, " ");
			var points = makingLocal_Points.replace(/\.\b/, ",");
			
			mydata = buildTop20Table(place,name,country,points);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p><span class="strong">&#1055;&#1086;&#1079;&#1080;&#1094;&#1080;&#1080; &#1080;&#1075;&#1088;&#1086;&#1082;&#1086;&#1074; &#1091;&#1095;&#1080;&#1090;&#1099;&#1074;&#1072;&#1102;&#1090; &#1074;&#1089;&#1077; &#1090;&#1091;&#1088;&#1085;&#1080;&#1088;&#1099;, &#1085;&#1072;&#1095;&#1072;&#1074;&#1096;&#1080;&#1077;&#1089;&#1103; &#1076;&#1086; 23:59 ';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += $(this).find("to_year").text() + '  &#1075;.</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.com/data/leader-board/matches.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="100%"><th>&#1044;&#1072;&#1090;&#1072;</th><th>Team PS Pro</th><th>&#1056;&#1077;&#1079;&#1091;&#1083;&#1100;&#1090;&#1072;&#1090;</th><th>&#1048;&#1075;&#1088;&#1086;&#1082;</th><th>&#1057;&#1091;&#1084;&#1084;&#1072;</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 = "&#1087;&#1086;&#1073;&#1077;&#1076;&#1080;&#1083;";
			} else {
				var theResult="&#1087;&#1088;&#1086;&#1080;&#1075;&#1088;&#1072;&#1083;";
			}		
		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.com/data/leader-board/records.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="100%"><th>&#1055;&#1088;&#1086;&#1092;&#1080;</th><th>&#1055;&#1086;&#1073;&#1077;&#1076;</th><th>&#1055;&#1086;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1081;</th><th>&#1055;&#1088;&#1086;&#1094;&#1077;&#1085;&#1090; &#1087;&#1086;&#1073;&#1077;&#1076;</th><th>&#1048;&#1090;&#1086;&#1075;&#1086;</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="&#1087;&#1086;&#1073;&#1077;&#1076;&#1072;";
			} else if ((theStreakKind == "yes") && (theStreakValue > 1)) {
				var theKind="&#1087;&#1086;&#1073;&#1077;&#1076;&#1099;";
			} else if ((theStreakKind == "no") && (theStreakValue == 1)) {
				var theKind="&#1087;&#1086;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1077;";
			} else {
				var theKind="&#1087;&#1086;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1103;";
			}					
		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;
}
