// blog handling javascript

NUMBER_BLOG_LINKS=2;
NUMBER_BLOG_ENTRIES=2;
BLOG_LINK = "http://bootstrapping101.wordpress.com";

$(document).ready(function() {
	if ( $.browser.msie ) {
  		var msiev = parseInt($.browser.version, 10);
  		if (msiev <= 6) {
  			$('#blogcontent').html('Visit the <a href="' + BLOG_LINK + '">Bootstrapping 101 Blog</a>');
  			return;
  		}
	}
	
	$('#blogcontent').show();
	var feedget = "http://bootstrapping101.wordpress.com/feed/";
	$.ajax({
		type : 'GET',
		dataType : 'xml',
		url : '../Scripts/curl.php?apiCall=' + escape(feedget),
		data : '',
		success : function(data, status) {
			populateBlog(data);
		},
		error : function(xhr, status, exc) {
			$('#blogcontent').html('Visit the <a href="' + BLOG_LINK + '">Bootstrapping 101 Blog</a>');
		},
		complete : function(xhr, status) {
			//alert(data);
			//$('#content').html("complete " + status);
		}
	});

});


function populateBlog(data) {
	$xml = $(data);
	//$title = $xml.find("title").first();
	//$('#content').html("got " + $title.text());
	var count = 0;
	var links = "<ul class='menu' id='blogmenu'>";
	var blogContent = "";
	$xml.find("item").each(function() {
		count ++;
		if (count <= NUMBER_BLOG_LINKS) {
			var $item = $(this);
			var title = $item.find('title').text().replace('bobreiss','');
			var link = $item.find('link').text();
			links += '<li><a href="' + link + '">' + title + '</a></li>';
			
			if (count <= NUMBER_BLOG_ENTRIES) {
				blogContent += populateContent($item, title, link);
			}
		}	
	});
	links += "</ul>"
	$('#blogcontent').html(blogContent);
	// $('#bloglinks').html(links);
}

function populateContent(blogItem, title, link) {
	var content = "";
	var pubDate = blogItem.find('pubDate').text();
	var date = new Date(pubDate);
	var description = blogItem.find('description').text();
	content += '<h2>' + title + '</h2>';
	content += '<span class="smaller">From <a href="' + BLOG_LINK + '">Bootstrapping 101 Blog</a> on ' + date.toLocaleDateString() + '.</span>';
	content += '<p>'+description+'</p>';
	content += '<span class=smaller><a href="' + link +'">read more</a></span>';
	return content;
}

