﻿var tumblrBadge = function () {
	// User settings
	var settings = {
		userName : "piryx",                // Your Tumblr user name
		itemsToShow : 99999,               // Number of Tumblr posts to retrieve
		itemToAddBadgeTo : "tumblr-badge", // Id of HTML element to put badge code into
		imageSize : 400,                   // Values can be 75, 100, 250, 400 or 500
		shortPublishDate : true            // Whether the publishing date should be cut shorter
	};
	
	// 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.userName + ".tumblr.com/api/read/json?callback=tumblrBadge.listItems&num=" + settings.itemsToShow;
		head.appendChild(badgeJSON);

		listItems = function (json) {
			var posts = json.posts,
				list = document.createElement("ul"), 
				post, 
				listItem, 
				text, 
				link, 
				img, 
				conversation, 
				postLink;
			list.className = "tumblr";
			for (var i=0, il=posts.length; i<il; i=i+1) {
				post = posts[i];

				// Only get content for text, video, photo, quote and link posts
				if (/regular|video|photo|quote|link|conversation/.test(post.type)) {
					listItem = document.createElement("li");
					// Create a link to Tumblr post
					postLink = document.createElement("a");
					postLink.className = "tumblr-post-date";
					postLink.href = post.url;
					var datetime = new Date(post["date"]);
					var datetimeString =  '<strong class="date">Posted ' + (datetime.getMonth() + 1) + '/' + datetime.getDate() + '/' + datetime.getFullYear()+  '</strong><br>';				
					var newdiv = document.createElement("div");
					var isTwitter = false;
					if (post.type == "video") 
					{
					    var temp = post["video-caption"];
		                var location = temp.indexOf('</h2>'); 
		                if (location !=-1)
                        {
		                    temp = temp.slice(4, location);
                        } 
                        else
                        {
                            location = temp.indexOf('</a></p>');
                            var locationFrom = temp.indexOf('">'); 
                            if (location !=-1)
                            { 
                                temp = temp.slice(locationFrom + 2, location);
                            }
                        } 
                        
                        newdiv.innerHTML = '<strong id="' + post["slug"] + '"></strong><a id="tumblr_title" target="_blank" href="'+  post["url"] +'">' + temp + '</a><br />'+ datetimeString + post["video-player"] + '';
					    listItem.appendChild(newdiv);						
					}
					else if (post.type == "photo") 
					{
						var temp = post["photo-caption"];
		                var location = temp.indexOf('</p>');
		                var locationfirst = temp.indexOf('">'); 
		                if (location !=-1)
                        {
		                    temp = temp.slice(locationfirst +2, location);
                        }                         
						newdiv.innerHTML = '<strong id="' + post["slug"] + '"></strong><a id="tumblr_title" target="_blank" href="'+  post["url"] +'">' + temp + '</a><br />'+ datetimeString + post["video-player"];
					    listItem.appendChild(newdiv);
					}
					else
					{
					    var feeditem = post["feed-item"];
					    if (feeditem.length > 0)
					    {
					        var checkTwitter = feeditem.indexOf('http://twitter.com/Piryx'); 
                            if (checkTwitter !=-1)
                            { 
                               newdiv.innerHTML = '<strong id="' + post["slug"] + '"></strong><a id="twitter" id="tumblr_title" target="_blank" href="'+  post["url"] +'">' + post["regular-body"] + '</a>' + datetimeString;
                                isTwitter = true;
                            }				        
					    }
					    else
					    {
					        newdiv.innerHTML = '<strong id="' + post["slug"] + '"></strong><a id="tumblr_title" target="_blank" href="'+  post["url"] +'">' + post["regular-title"] + '</a><br />' + datetimeString;
					    }
					   
					    listItem.appendChild(newdiv);	 
					}
					
					if (isTwitter)
					{
					 text = "";
					}
					else
					{		
					    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.width = settings.imageSize;
						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") {
						newdiv.innerHTML = '<strong id="' + post["slug"] + '"></strong><a id="tumblr_title" target="_blank" href="'+  post["url"] +'">' + post["link-text"] + '</a><br />'+ datetimeString;
					    listItem.appendChild(newdiv);					    
					    text = post["link-description"];
					}
					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>");
						}
					}
					listItem.innerHTML += text;
					
					var tempHtml = listItem.innerHTML;
					var undefloc = tempHtml.indexOf('<br>undefined'); 
	                if (undefloc !=-1)
                    {
	                    listItem.innerHTML = tempHtml.replace('<br>undefined', '<br>');
                    } 				
					

					postLink.innerHTML = '<br><hr width="630" size="1"><br>';
					listItem.appendChild(postLink);

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