$(document).ready(function() {
  $.ajax({
    url: "../1rss/php/rss.php",
    processData: false,
    dataType: 'xml',
    error: function(req, b,err){
	    //alert(err);
	    },
    success: parseRSS
	});
	function parseRSS(data) {
		// Clear contents of the box
		$('#blogFeedBox ul').empty();
		var numEntries = 0;
		// Iterate over feed items
		$(data).find("item").each(function() {
			var $item = $(this);

			// Read values for current item from feed
			var title = $item.find('title').text();
			var description = $item.find('description').text();
			var pubDate = $item.find('pubDate').text();
			var itemLink = $item.find('link').text();

			// Convert the string to date, so that we could be 
			// able to extract month and day from it
			var pDate = new Date(pubDate);
			var monthNumber = pDate.getMonth();

			// In javascript we cannot get month Name, i.e. January.
			// Thats why we need to declare an array and then 
			// we'll translate month number to month name
			var months = new Array(13);
			months[0]  = "January";
			months[1]  = "February";
			months[2]  = "March";
			months[3]  = "April";
			months[4]  = "May";
			months[5]  = "June";
			months[6]  = "July";
			months[7]  = "August";
			months[8]  = "September";
			months[9]  = "October";
			months[10] = "November";
			months[11] = "December";

			// Get month name from month number
			var monthName = months[monthNumber].substring(0,3); // remove .substring(0,3) part to show full month name
			var dateNum = pDate.getDate();

			// Strip out any html tags from the feed using Regular Expressions
			var desc = description.replace(/(<.*?>)/ig,"");

			// Strip out html comments that are present in the feed i.e. <!-- .... -->
			desc = desc.replace(/<!(?:--[\s\S]*?--\s*)?>\s*/g,'');

			// Strip out any leading and trainling blanks from the contents of feed item
			desc = $.trim(desc);
			
			
			var html = "<li id=\"noBorder"+numEntries+"\"><div class=\"eachPostDayMonth eachM"+numEntries+"\">" + monthName + "<br\/><a href=\""+itemLink+"\" onclick=\"sendLinkEvent('sml_blog_filter_" + monthName + dateNum + "');\">" + dateNum + "<\/a><\/div>";
			html += "<div class=\"eachPost eachP"+numEntries+"\">";
			html += "<a href=\""+itemLink+"\" onclick=\"sendLinkEvent('sml_blog_filter_" + monthName + dateNum + "');\">" + title + "<\/a>&nbsp;";
			html += desc.substring(0,100) + " ..."; // Show first 100 characters
			html += "<\/div><\/li>";

			$('#blogFeedBox ul').append(html); 

			numEntries++;
			
			// Exit if we have displayed 3 items
			if(numEntries >= 3){
				// Add ReadMore link
				$readMore = "<li id=\"noBorder4\"><div id=\"readMoreLink\"><br /><br /><a href=\"http:\/\/blog.addictinggames.com\/\" onclick=\"sendLinkEvent('sml_blog_filter_more');\">read more &raquo<\/a><\/div><\/li>";
				$('#blogFeedBox ul').append($readMore);
				return false;
			}
		 });
	}
});
