$(document).ready(function() {

	function loadData() {

		$.post("proxy.php?url=http://splash.mainstreamdata.com/RSS/Celebrity-RSS.xml", { action: "get" } , function(d) {

			var i 			= 0;
			var post_limit 	= 10;
			var feedContent = "";
			var newsContent = "";

			//find each 'item' in the file and parse it
			$(d).find('item').each(function() {

				//name the current found item this for this particular loop run
				var $item = $(this);
				// grab the post title
				var title = $item.find('title').text();
				// grab the post's URL
				var link = $item.find('link').text();
				// next, the description
				var description = $item.find('description').text();
				// next, the author
				var author = $item.find('author').text();
				//don't forget the pubdate
				var pubDate =  new Date($item.find('pubDate').text());
				var hours = pubDate.getHours()
				var minutes = pubDate.getMinutes()

				if (minutes < 10){
					minutes = "0" + minutes
				}

				var day_time = "AM"
				if(hours > 11){
					day_time = "PM";
				}


				if(i < post_limit){
					// now create a var 'html' to store the markup we're using to output the feed to the browser window
					feedContent += '<div class="post">';
					
					feedContent += '<div class="headline span-24">';
					feedContent += '<div class="title span-14">' + title + '</div>';
					feedContent += '<div class="author span-6">' + author + '</div>';
					feedContent += '<div class="pubdate span-4 last">Today, ' + hours + ':' + minutes + ' ' + day_time + '</div>';
					feedContent += '</div>';
					
					feedContent += '<div class="post-body">' + description + '</div>';
					feedContent += '<div style="clear: both;"/></div>';

					i += 1;
				}

				// Create ticker content
				newsContent += '<li><a href="'+link+'">'+title+'</a></li>';
			});

			//clear the content in the div for the next feed.
			$("#feedContent").empty();
			$("ul#news").empty();

			// Append content to page
			$("#feedContent").append(feedContent);
			$("ul#news").append(newsContent);

			// Restart ticker
			$().newsTicker({newsList: "ul#news", startDelay: 10, firstRun:0});
		});
	}
	
	// Load the data
 	loadData();
 	// And load it every 15 minutes thereafter
 	setInterval( loadData, 900000 );

});


