$(document).ready(function() {

	// Hämta tumblrfeed
	$.getJSON("http://tumblr.karnhuset.net/api/read/json?callback=?", function(data) {

		var numberOfItems = 3;

		$.each(data.posts, function(i, posts) {
			if (i >= 3) { return true; }
			var postDate = new Date(this["date"]);
			var postDateMonth = "" + (postDate.getMonth() + 1);
			if (postDateMonth.length < 2) { postDateMonth = "0" + postDateMonth }
			var postDateDay = "" + (postDate.getDate());
			if (postDateDay.length < 2) { postDateDay = "0" + postDateDay }
			var postDateString = postDate.getFullYear() + "-" + postDateMonth + "-" + postDateDay;

			switch (this["type"]) {
				case "regular":
					$("ul#tumblrFeed").append("<li id=\"" + this["id"] + "\"><h4>" + this["regular-title"] + "</h4>" + this["regular-body"] + "<p class=\"dateLine\">Postad " + postDateString + "</p></li>");
					break;

				case "link":
					$("ul#tumblrFeed").append("<li id=\"" + this["id"] + "\"><a href=\"" + this["link-url"] + "\">" + this["link-text"] + "</a><p>" + this["link-description"] + "</p><p class=\"dateLine\">Postad " + postDateString + "</p></li>");
					break;

				case "quote":
					$("ul#tumblrFeed").append("<li id=\"" + this["id"] + "\"><blockquote>" + this["quote-text"] + "</blockquote><p class=\"quoteSource\">" + this["quote-source"] + "</p><p class=\"dateLine\">Postad " + postDateString + "</p></li>");
					break;

				case "photo":
					$("ul#tumblrFeed").append("<li id=\"" + this["id"] + "\"><img src=\"" + this["photo-url-250"] + "\" width=\"243\" class=\"tumblrImage\">" + this["photo-caption"] + "<p class=\"dateLine\">Postad " + postDateString + "</p></li>");
					break;

				case "conversation":
					break;

				case "video":
					var videoPlayer = this["video-player"];
					videoPlayer = videoPlayer.replace(/width="\d*\d\"/gi, "width=\"243\"");
					videoPlayer = videoPlayer.replace(/height="\d*\d\"/gi, "height=\"180\"");
					$("ul#tumblrFeed").append("<li id=\"" + this["id"] + "\">" + videoPlayer + this["video-caption"] + "<p class=\"dateLine\">Postad " + postDateString + "</p></li>");
					break;

				case "audio":
					break;

				case "answer":
					break;
			};
		});
	});
});