// tumblrBadge by Robert Nyman, http://www.robertnyman.com/, http://code.google.com/p/tumblrbadge/
// customisation by Tim Dawson, http://www.reconfine.com.



var tumblrBadge = function () {
	
	// Read a page's GET URL variables and return them as an associative array.
	var map = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		map[key] = value;
	});
    if (map['q']) {
    	var searchterm = map['q'];
    } else {
    	var searchterm = ""
    }
	
	// User settings
	var settings = {
		userName : "imagemechanics", // Your Tumblr user name
		itemsToShow : 10, // Number of Tumblr posts to retrieve
		itemToAddBadgeTo : "tumblr", // Id of HTML element to put badge code into
		imageSize : 400, // Values can be 75, 100, 250, 400 or 500
		shortPublishDate : false, // Whether the publishing date should be cut shorter
		timeToWait : 5000, // Milliseconds, 1000 = 1 second
		searchTerm : searchterm
	};
	
	// 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+"&search=" +settings.searchTerm;
		head.appendChild(badgeJSON);
		
		var wait = setTimeout(function () {
			badgeJSON.onload = null;
			badgeJSON.parentNode.removeChild(badgeJSON);
			badgeJSON = null;
		}, settings.timeToWait);
		
		listItems = function (json) {
		
			var posts = json.posts,
				list = document.createElement("ul"), 
				post, 
				listItem, 
				text, 
				link, 
				img, 
				conversation, 
				postLink;
			list.className = "tumblr";
			
			if (posts.length==0){
			listItem = document.createElement("li");
			listItem.innerHTML +='<div class="bordercontainer"><div class="whitebox"><h2>Sorry!</h2><p>No posts matching your query were found</p><p><a href="#/journal">Click here</a> to see all posts</p></div><div class="whitealphaborder"></div></div>';
			list.appendChild(listItem);
			}
			
			for (var i=0, il=posts.length; i<il; i=i+1) {
				post = posts[i];

				// Only get content for text, photo, quote and link posts
				if (/regular|photo|quote|link|video|audio|conversation/.test(post.type)) {
					listItem = document.createElement("li");
					text = post["regular-body"] || post["photo-caption"] || post["quote-source"] || post["link-text"] || post["video-caption"] || post["audio-caption"] || post["link-url"] || "";
					if (post.type === "regular") {
						text = "<h2>" + post["regular-title"]+ "</h2><p>" + text+"</p>";
					}
					else 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 = "<p>" + text + "</p>";
					}
					else if (post.type === "quote") {
						text = "<div class='tumblr-quote'><blockquote>"+ post["quote-text"] + "</blockquote></div><p>" + text + "</p>";
					}
					else if (post.type === "link") {
						text = '<h2><a href="' + post["link-url"] + '">' + text + '</a></h2><p>'+post["link-description"]+'</p>';
					}
					else if (post.type === "video") {
						text = post["video-player"] + "<p>" + post["video-caption"] + "</p>";
					}	
					else if (post.type === "audio") {
						text = post["audio-player"] + "<p>" + text + "</p>";
					}	
					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>");
						}
					}
					var postDate = formatDate(new Date(post["unix-timestamp"]* 1000),"l F j, Y");
					
					if (post.type === "photo") {
					var $img = '<a class="imagepost" href="'+post.url+'"><img src="'+post["photo-url-" + settings.imageSize]+'"  width="'+settings.imageSize+'" /></a>'; 
					}else {$img =''}
					
					listItem.innerHTML += '<div class="bordercontainer"><div class="whitebox"><div class="datewrapper">'+$img+'<a href="'+post.url+'" class="tumblr-post-date">'+postDate+'</a></div>'+text+'<br class="clearfix" /></div><div class="whitealphaborder"></div></div>';
					
					
					list.appendChild(listItem);
				}
				
			}
			
			// Apply list to container element
			badgeContainer.innerHTML = "";
			badgeContainer.appendChild(list);
		};
		
		return {
			listItems : listItems
		};
	}
}();