// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the xml file
	$.get("http://www.pokerstars.net/league-data/sppl.xml",{},function(xml){
      	
	// Build an HTML string
	HTMLOutput = '';
	
	$('date',xml).each(function(i) {
			
		HTMLOutput += '<p>Esta clasificación es el resultado de los torneos celebrados entre el ';
		HTMLOutput += $(this).find("from_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011 y el ';
		HTMLOutput += $(this).find("to_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011.</p>';
		HTMLOutput += '<h2>Clasificación de la 2ª División de la liga española</h2>';

	 	HTMLOutput += '<table class="redtable" width="90%"><tr><th>Posición</th><th>2ª División</th><th>Puntos</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			div2name = $(this).find("div2name").text();
			div2points = $(this).find("div2points").text();
			
			// Build row HTML data and store in string
			mydata = buildHTML(place,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,div2name,div2points){
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td>'+ place + '</td>';
	output += '<td>'+ div2name +'</td>';
	output += '<td>'+ div2points +'</td>';
	output += '</tr>';
	return output;
}
	 
