// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the xml file
	$.get("/league-data/eepl.xml",{},function(xml){
      	
	// Build an HTML string
	HTMLOutput = '';
	
	$('date',xml).each(function(i) {
			
		HTMLOutput += '<p>V následujícím pořadí jsou zahrnuty turnaje odehrané mezi ';
		HTMLOutput += $(this).find("from_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011 a ';
		HTMLOutput += $(this).find("to_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011.</p>';
		
		
		//build 1st division
		HTMLOutput += '<h2>Divize 1 – turnaj $2,20</h2>';
		HTMLOutput += '<table class="online_events" width="90%"><tr><th>Místo</th><th>Hráč</th><th>Země</th><th>Body</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("div1name").text();
			points = $(this).find("div1points").text();
			country = $(this).find("div1country").text();
			
			// Build row HTML data and store in string
			mydata = buildHTML(place,name,points,country);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p class="to_Top" align="right"><a href="#top">Zpět nahoru</a></p> ';
		
		//build 2nd division
		HTMLOutput += '<h2>Divize 2 – turnaj $0,10</h2>';
		HTMLOutput += '<table class="online_events" width="90%"><tr><th>Místo</th><th>Hráč</th><th>Země</th><th>Body</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("div2name").text();
			points = $(this).find("div2points").text();
			country = $(this).find("div2country").text();
			
			// Build row HTML data and store in string
			mydata = buildHTML(place,name,points,country);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p class="to_Top" align="right"><a href="#top">Zpět nahoru</a></p> ';
		
		//build 3rd division
		HTMLOutput += '<h2>Divize 3 – 2.000 hracích peněz v turnajových žetonech</h2>';
		HTMLOutput += '<table class="online_events" width="90%"><tr><th>Místo</th><th>Hráč</th><th>Země</th><th>Body</th></tr>';
	  	
		// Run the function for each tag in the XML file
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("div3name").text();
			points = $(this).find("div3points").text();
			country = $(this).find("div3country").text();
			
			// Build row HTML data and store in string
			mydata = buildHTML(place,name,points,country);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p class="to_Top" align="right"><a href="#top">Zpět nahoru</a></p> ';
		
		// Update the DIV called Content Area with the HTML string
		$("#writeRoot").append(HTMLOutput);
		$("tr:odd", "#writeRoot").addClass('rowTint');
	});
});
	
});
 
 function buildHTML(place,name,points,country){
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td>'+ place + '</td>';
	output += '<td>'+ name +'</td>';
	output += '<td>'+ country +'</td>';
	output += '<td>'+ points +'</td>';
	output += '</tr>';
	return output;
}
	 
