// JavaScript Document

// Start function when DOM has completely loaded 
$(document).ready(function(){ 
	
	
	
	// Open the calendar.xml file
	$.get(calendar_file,{},function(xml){
      	
		// Initialize counter
		var event_counter = 0;
		
		// Build an HTML string
		myHTMLOutput = '';
		myHTMLOutput += '<table id="events_listing">';
		myHTMLOutput += '<th>Event</th><th>Date</th><th>Time</th>';
		
		// Run the function for each event in the XML file
		$('event',xml).each(function(i) {
			event_name = $(this).find("event_title").text();
			event_date = $(this).find("event_date").text();
			start_time = $(this).find("start_time").text();
			end_time = $(this).find("end_time").text();
			event_url = $(this).find("event_url").text(); 
			
			if(event_counter<=5) {
				// Build row HTML data and store in string
				mydata = BuildEventHTML(event_name, event_date, start_time, end_time, event_url);
				myHTMLOutput = myHTMLOutput + mydata;
			}
			event_counter++;
		});
		myHTMLOutput += '</table>';
			
		// Update the DIV called 'jquery_table' with the HTML string
		$("#jquery_table").append(myHTMLOutput);
	});
	
	// Open the news.xml file
	$.get(news_file,{},function(xml){
      	
		// Initialize counter
		var news_counter = 0;
		
		// Build an HTML string
		myNewsHTMLOutput = '';
		myNewsHTMLOutput += '<table id="news_listing">';
		
		// Run the function for each news item in the XML file
		$('news',xml).each(function(i) {
			item_name = $(this).find("item_title").text();
			item_pubDate = $(this).find("item_pubDate").text();
			item_link = $(this).find("item_link").text(); 
			
			if(news_counter<=5) {
				// Build row HTML data and store in string
				myNewsdata = BuildNewsHTML(item_title, item_pubDate, item_link);
				myNewsHTMLOutput = myNewsHTMLOutput + myNewsdata;
			}
			news_counter++;
		});
		myNewsHTMLOutput += '</table>';
			
		// Update the DIV called 'news_table' with the HTML string
		$("#news_table").append(myNewsHTMLOutput);
	});
	
});
 
 
 
 function BuildEventHTML(event_title,event_date,start_time,end_time,event_url) {
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td class="event"><a href="' + event_url + '" title="IUPUI Events Calendar: ' + event_title + '">' + event_title + '</a></td>';
	output += '<td class="date">'+ event_date +'</td>';
	output += '<td class="time">'+ start_time + ' - ' + end_time + '</td>';
	output += '</tr>';
	return output;
}

 function BuildNewsHTML(item_title,item_pubDate,item_link) {
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td class="title"><a href="' + item_link + '" title="' + item_title + '">' + item_title + '</a></td>';
	output += '<td class="date">'+ item_pubDate +'</td>';
	output += '</tr>';
	return output;
}
	 
