// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	// Open the xml file
	$.get("/league-data/cispl.xml",{},function(xml){
      	
	// Build an HTML string
	HTMLOutput = '';
	
	$('date',xml).each(function(i) {
			
		HTMLOutput += '<p>Ниже Вы найдете турнирную таблицу по результатам турниров, сыгранных с ';
		HTMLOutput += $(this).find("from_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011 по ';
		HTMLOutput += $(this).find("to_day").text() + '.';
		HTMLOutput += $(this).find("month").text() + '.2011.</p>';
		HTMLOutput += '<h2>Турнирная таблица всех дивизионов</h2>';

	 	HTMLOutput += '<table class="online_events" width="90%"><tr><th>Место</th><th>Дивизион 1</th><th>Очки</th><th>Дивизион 2</th><th>Очки</th><th>Дивизион 3</th><th>Очки</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,div3name,div3points);
			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,div3name,div3points){
	
	// 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;
}
	 
