$(document).ready(function(){ 
						   						   	
	// Open the students.xml file
	$.get("public/data/dealers.xml",{},function(xml){
      	
		// Build an HTML string
		myHTMLOutput = '';
	 	myHTMLOutput += '<table id="dealer_table">';
	  	myHTMLOutput += '<thead><tr><th>Name</th><th>Address 1</th><th>Address 2</th><th>Address 3</th><th>Area</th><th>Postcode</th></tr></thead>';
		myHTMLOutput += '<tbody>';
	  	
		// Run the function for each student tag in the XML file
		$('dealer',xml).each(function(i) {
			dealerName = $(this).find("name").text();
			dealerAddress1 = $(this).find("address1").text();
			dealerAddress2 = $(this).find("address2").text();
			dealerAddress3 = $(this).find("address3").text();
			dealerAddress4 = $(this).find("address4").text();
			dealerPostcode = $(this).find("postcode").text();
			
			// Build row HTML data and store in string
			mydata = BuildDealerHTML(dealerName, dealerAddress1, dealerAddress2, dealerAddress3, dealerAddress4, dealerPostcode);
			myHTMLOutput = myHTMLOutput + mydata;
		});
		myHTMLOutput += '</tbody>';
		myHTMLOutput += '</table>';
				
		// Update the DIV called Content Area with the HTML string
		$("#dealer_list div.loading").remove();
		$("#dealer_list").append(myHTMLOutput);
		
		$("#dealer_table").tablesorter({
			widgets: ['zebra'],
			sortList: [[0,0]],
			headers: { 
				1: { 
					sorter: false 
				}, 
				2: { 
					sorter: false 
				},
				3: { 
					sorter: false 
				},
				5: { 
					sorter: false 
				} 
			}
		}); 

		$("#dealer_table").trigger("update"); 
		
		$("#dealer_table tbody tr").hover(function(){
		  $(this).addClass('highlight');
		}, function(){
		  $(this).removeClass('highlight');
		});
		
		
	});
});
 
 
function BuildDealerHTML(dealerName, dealerAddress1, dealerAddress2, dealerAddress3, dealerAddress4, dealerPostcode){
			output = '';
			output += '<tr>';
			output += '<td>'+ dealerName +'</td>';
			output += '<td>'+ dealerAddress1 +'</td>';
			output += '<td>'+ dealerAddress2 +'</td>';
			output += '<td>'+ dealerAddress3 +'</td>';
			output += '<td>'+ dealerAddress4 +'</td>';
			output += '<td>'+ dealerPostcode +'</td>';
			output += '</tr>';
			return output;
}

