// File: myjavascript.js
// will load and process the RSS file (initially in the js directory, eventually on the Eureka XML site).

// Start function when DOM has completely loaded 
$(document).ready(function(){ 
						   

	var maxnum = 10;
	var num = 0;
	var RSSURL = '';
	
	
	// Open the rss.xml file depending upon which URL you are currently at:

	if (document.URL == 'http://www2.ku.edu/~kuscied/' || document.URL == 'http://www.kuscied.org/'){
		RSSURL = "/~kuscied/cgi-bin/getrss.php?type=index";
//		RSSURL = "/~kuscied/js/rss.xml";
	}
	else if (document.URL == 'http://www2.ku.edu/~kuscied/sciencenews/' || document.URL == 'http://www.kuscied.org/sciencenews/' || document.URL == 'http://www.kuscied.org/~kuscied/sciencenews/'){
		RSSURL = "/~kuscied/cgi-bin/getrss.php?type=policy";
//		RSSURL = "/~kuscied/js/policy_ethics.xml";
		maxnum = 20;
	}
	else if (document.URL == 'http://www2.ku.edu/~kuscied/resources/' || document.URL == 'http://www.kuscied.org/resources/' || document.URL == 'http://www.kuscied.org/~kuscied/resources/'){
		RSSURL = "/~kuscied/cgi-bin/getrss.php?type=education";
//		RSSURL = "/~kuscied/js/education.xml";
		maxnum = 20;
	}
	else {
		RSSURL = "/~kuscied/cgi-bin/getrss.php";
//		RSSURL = "/~kuscied/js/rss.xml";
	}

//		RSSURL = "/~kuscied/cgi-bin/getrss.php?type=education";
	$.get(RSSURL,{},function(xml){
      	
		// Build an HTML string
		myHTMLOutput = '';
	  	
		// Run the function for each student tag in the XML file
		$("item",xml).each(function(i) {
			num = num + 1;
			if (num <= maxnum) {
//				itemtitle = $(this).text();
				itemtitle = $(this).children(":first").text();
				itemdescript = $(this).find("description").text();
				itemlink = $(this).find("link").text();
				itemPubdate = $(this).find("pubDate").text();
				itemguid = $(this).find("guid").text(); 
			
				// Build row HTML data and store in string
				mydata = BuildItemHTML(itemtitle, itemdescript, itemlink, itemPubdate, itemguid);

//				mydata = '<p>' + itemtitle + '</p>';
				myHTMLOutput = myHTMLOutput + mydata;
			}
		});
		
		
	// Update the DIV called Content Area with the HTML string
		$("#RSSHeadlines").append(myHTMLOutput);

	});
	
});
 
 
 
 function BuildItemHTML(itemtitle, itemdescript, itemlink, itemPubdate, itemguid){
		
	// Build HTML string and return
	output = '';
	output += '<h3><a href="'+itemguid+'">'+itemtitle+'</a></h3>';
	output += '<p style="padding-bottom:.5em;">'+itemdescript+'</p>';
	return output;
}
	 