// tumblrBadge by Robert Nyman, http://www.robertnyman.com/, http://code.google.com/p/tumblrbadge/
(function () {
	// User settings
	var settings = {
		url : "sympathizer.org", // Your Tumblr url
		itemsToShow : 4, // Number of Tumblr posts to retrieve
		itemToAddBadgeTo : "tumblr_container", // Id of HTML element to put badge code into
		imageSize : 250, // Values can be 75, 100, 250, 400 or 500
		shortPublishDate : true, // Whether the publishing date should be cut shorter
		timeToWait : 2000, // Milliseconds, 1000 = 1 second
		width : 540 // 7-column
	};
	
	// Badge functionality
	var head = document.getElementsByTagName("head")[0];
	var badgeContainer = document.getElementById(settings.itemToAddBadgeTo);
	if (head && badgeContainer) {
		var badgeJSON = document.createElement("script");
		badgeJSON.type = "text/javascript";
		badgeJSON.src = "http://" + settings.url + "/api/read/json?num=" + settings.itemsToShow;
		head.appendChild(badgeJSON);
		
		var wait = setTimeout(function () {
			badgeJSON.onload = null;
			badgeJSON.parentNode.removeChild(badgeJSON);
			badgeJSON = null;
		}, settings.timeToWait);
		
		badgeJSON.onload = function () {
			var posts = tumblr_api_read.posts;
			var list = document.createElement("ul"), 
				post, 
				listItem, 
				text, 
				link, 
				img, 
				conversation, 
				postLink;
			list.id = "tumblr_list";
			for (var i=0, il=posts.length; i<il; i=i+1) {
				post = posts[i];

				if (/regular|photo|quote|link|conversation|video/.test(post.type)) {
					listItem = document.createElement("li");
					text = post["regular-body"] || post["photo-caption"] || post["quote-source"] || post["link-text"] || post["link-url"] || "";
					if (post.type === "photo") {
						link = document.createElement("a");
						link.href = post.url;
						img = document.createElement("img");
						// To avoid a creeping page
						img.style['max-width'] = settings.width;
						img.src = post["photo-url-" + settings.imageSize];
						link.appendChild(img);
						listItem.appendChild(link);
						text = "<em>" + text + "</em>";
					}
					else if (post.type === "quote") {
						text = post["quote-text"] + "<em>" + text + "</em>";
					}
					else if (post.type === "link") {
						text = '<a href="' + post["link-url"] + '">' + text + '</a><br/><em>' + post['link-description'] + '</em>';
					}
					else if (post.type === "conversation") {
						conversation = post["conversation-lines"];
						for (var j=0, jl=conversation.length; j<jl; j=j+1) {
							text += conversation[j].label + " " + conversation[j].phrase + ((j === (jl -1))? "" : "<br>");
						}
					}
					else if (post.type === "video") { // added by lawrence
						if (post['video-caption']) {
							link = '<em>' + post['video-caption'] + '</em>';
						} else
							link = '';
						var video = post['video-player'];
						
						// resize the video
						var width = video.match(/width="(\d*)"/);
						if (width) {
							// change width
							video = video.replace(/width="\d*"/g, 'width="' + settings.width + '"');
							// change height
							var hm = video.match(/height="(\d*)"/);
							if (hm) {
								var height = parseInt(hm[1]) * settings.width / parseFloat(width[1]);
								video = video.replace(/height="\d*"/g, 'height="' + height + '"');
							}
						}
						text = video + '<br/>' + link;
					}
					listItem.innerHTML += text;

					// Create a link to Tumblr post
					postLink = document.createElement("a");
					postLink.className = "tumblr-post-date";
					postLink.href = post.url;
					postLink.innerHTML = (settings.shortPublishDate)? post["date"].replace(/(^\w{3},\s)|(:\d{2}$)/g, "") : post["date"];
					listItem.appendChild(postLink);

					// Apply list item to list
					list.appendChild(listItem);
				}
			}
			
			// Apply list to container element
			badgeContainer.innerHTML = "";
			badgeContainer.appendChild(list);
		}
	}
}());

