// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the xml file
	$.get("http://www.pokerstars.pt/league-data/ppl.xml",{},function(xml){
      	
	// Build an HTML string
	HTMLOutput = '';
	
	$('date',xml).each(function(i) {
			
		HTMLOutput += '<p>Estes rankings reflectem os torneios jogados entre ';
		HTMLOutput += $(this).find("from_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2010 e ';
		HTMLOutput += $(this).find("to_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2010.</p>';
		HTMLOutput += '<h2>Rankings para Todas as Divisões da Liga Portuguesa</h2>';

	 	HTMLOutput += '<table class="online_events" width="90%"><tr><th>Lugar</th><th>Divisão 1</th><th>Pontos</th><th>Divisão 2</th><th>Pontos</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			div1name = $(this).find("div1name").text();
			div1points = $(this).find("div1points").text();
			div2name = $(this).find("div2name").text();
			div2points = $(this).find("div2points").text();
			//div3name = $(this).find("div3name").text(); 
			//div3points = $(this).find("div3points").text();
			
			// Build row HTML data and store in string
			mydata = buildHTML(place,div1name,div1points,div2name,div2points);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		
		// Update the DIV called Content Area with the HTML string
		$("#writeRoot").append(HTMLOutput);
		$("tr:odd", "#writeRoot").addClass('rowTint');
	});
});
	
});
 
 function buildHTML(place,div1name,div1points,div2name,div2points){
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td>'+ place + '</td>';
	output += '<td>'+ div1name +'</td>';
	output += '<td>'+ div1points +'</td>';
	output += '<td>'+ div2name +'</td>';
	output += '<td>'+ div2points +'</td>';
	//output += '<td>'+ div3name +'</td>';
	//output += '<td>'+ div3points +'</td>';
	output += '</tr>';
	return output;
}
	 