// Hungarian Weekly code: yyyy.mm.dd with 0
$(document).ready(function(){ 
	$.get("http://www.pokerstars.hu/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>Heti ranglista: ';
		HTMLOutput += $(this).find("from_year").text() + '.';
		HTMLOutput += fromLocalMonth + '.';
		HTMLOutput += fromLocalDay + ' &eacute;s ';
		
		HTMLOutput += $(this).find("to_year").text() + '.';
		HTMLOutput += toLocalMonth + '.';
		HTMLOutput += toLocalDay + ' k&ouml;z&ouml;tt.</h3>';		

	 	HTMLOutput += '<table class="table" width="60%"><tr><th>Helyez&eacute;s</th><th>J&aacute;t&eacute;kos</th><th>Orsz&aacute;g</th><th>Pontsz&aacute;m</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 - HU
			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">A fenti pontsz&aacute;mokba minden, ';
		HTMLOutput += $(this).find("to_year").text() + '.';
		HTMLOutput += toLocalMonth + '.';
		HTMLOutput += toLocalDay + ' ET 23:59-ig (CET 5:59) elindult verseny belesz&aacute;m&iacute;t.</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.hu/data/leader-board/matches.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="90%"><th>D&aacute;tum</th><th>Team PS Pro</th><th>Eredm&eacute;ny</th><th>J&aacute;t&eacute;kos</th><th>Nyerem&eacute;ny</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 = theYear + '.' + theLocalMonth + '.' + theLocalDay;		
		var thePro = $(this).find('pro').text();
		//Bits needed in the Results row
		var theResultValue = $(this).find('result').text();
			if (theResultValue == 1) {
				var theResult = "legy&#337;zte &#337;t:";
			} else {
				var theResult="vesz&iacute;tett ellene:";
			}		
		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.hu/data/leader-board/records.xml",{},function(xml){
	myHTMLOutput = '';
	myHTMLOutput += '<table class="table" width="90%"><th>Pro</th><th>Gy&#337;zelem</th><th>Veres&eacute;g</th><th>Gy&#337;zelmi ar&aacute;ny (%)</th><th>Sorozat</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="gy&#337;zelem";
			} else if ((theStreakKind == "yes") && (theStreakValue > 1)) {
				var theKind="gy&#337;zelem";
			} else if ((theStreakKind == "no") && (theStreakValue == 1)) {
				var theKind="veres&eacute;g";
			} else {
				var theKind="veres&eacute;g";
			}					
		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;
}
