	var tumblr_api_read = null; // this is important do not remove

	$(document).ready(function() {	
			// setup our namespace
			$.tumblr = {};
			// modify these
			$.tumblr.url = "http://blog.pringlescotland.com/api/read/json?callback=?";
			$.tumblr.numPostsToDisplay = 2;
			$.tumblr.postMaxDescriptionLength = 100; // set to -1 to turn off jquery.expander
			$.tumblr.videoWidth='200'; // youtube default 400
			$.tumblr.videoHeight='163'; // youtube default 325
			// do not modify these unless you are hardcore
			$.tumblr.imagePath = 'http://www.pringlescotland.com/pws/client/images/tumblr/';	
			$.tumblr.postCount = 0;
			
 			reloadTumblr();
  });		
  
  function reloadTumblr(){  	
  	$("#container").empty();
  	$("#container").append("<div class='body'>loading Pringle Scotland <img class='icon' src='" + $.tumblr.imagePath + "small_red.gif'/></div>");
  
  	$.ajax({
		  url: $.tumblr.url,
		  dataType: 'script',
		  timeout: 10000,
      success:function(){ 
      	$("#container").empty();
      	if ((tumblr_api_read == undefined) || (tumblr_api_read == null)) {
      		$("#container").append("<div class='title' href='#'>unable to load Pringle Scotland</div>");
      		$("#container").append("<div class='body'><a href=\"#\" onclick=\"javascript:reloadTumblr();\">[retry]</a></div>");    			
    		} else {
    			$.tumblr.postCount = 0;
	        $.each(tumblr_api_read.posts.slice(0, 10), function(i,post){
						if ($.tumblr.postCount >= $.tumblr.numPostsToDisplay) {
							return;
						}    			
	    			parseTumblrJSON(post);
	    			$.tumblr.postCount++;
	  			});
	
	    		// Apply Expander
				var postMaxDescriptionLength = $.tumblr.postMaxDescriptionLength;
	    		if (postMaxDescriptionLength > -1) {
					  $('div.expandable').expander({
					    slicePoint:       postMaxDescriptionLength,  // default is 100
					    expandText:         '[read more]',
					    userCollapseText: '[^]'
					  });					    			
	    		}
    		}
      },
      error:function (xhr, statusTxt, errorTxt){
      		$("#container").append("<a class='title' href='#'>Tumblr Parse Error</a>");
      		$("#container").append("<div class='body'>" + errorTxt + "<br/>" + xhr.responseText + "</div>");
      } 					      
		});  	
  }
	
	function formatDate(d) {
 		return d.toString('MMMM d, yyyy')
	}
	
	function processResponse() {
	}
	
	function parseTumblrJSON(post) {
		//alert(post.type);
		var d = Date.parse(post["date-gmt"]);
		var dateFmt = formatDate(d);
		


    switch(post.type)
    {		    	
    	case "regular":
    	{
    		$("#container").append("<li class='posts'><h1><a class='title' href='" + post.url + "' target='_blank'>" + post["regular-title"] + "</a></h1><div class='body expandable'>" + post["regular-body"] + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><a href='http://twitter.com/share' class='twitter-share-button' data-text='" + post["regular-title"] + "' data-count='horizontal' data-counturl='" + post.url + "' data-url='" + post.url + "' data-via='PringleScotland'>Tweet</a><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
			break;
    	}
    	case "link":
    	{
			$("#container").append("<li class='posts'><h1><a class='title' href='" + post["link-url"] + "' target='_blank'>" + post["link-text"] + "</a></h1><div class='body expandable'>" + post["link-description"] + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><a href='http://twitter.com/share' class='twitter-share-button' data-text='" + post["link-text"] + "' data-count='horizontal' data-counturl='" + post.url + "' data-url='" + post.url + "' data-via='PringleScotland'>Tweet</a><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}		    	
    	case "quote":
    	{		
			$("#container").append("<li class='posts'><div class='body'><div class='quote expandable'>" + post["quote-text"] + "</div><div class='quotesrc'>" + post["quote-source"] + "</div></div><div class='meta'><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}		    	
    	case "photo":
    	{		

			var caption = post["photo-caption"];
			
			if (caption.length > 500) {
				var newcaption = caption.substr(0,500);
				var end = newcaption.lastIndexOf(" ");
				
				caption = newcaption.substr(0,end) + " <a href='http://blog.pringlescotland.com/' target='_blank' class='readmore'> ...Read more</a>";
			}

			if (post["photo-link-url"] == undefined) {
				var linkhref = "<img src='" + post["photo-url-250"] + "' height='250'/>";
			} else {
				var linkhref = "<a class='title' href='" + post["photo-link-url"] + "' target='_blank'><img src='" + post["photo-url-250"] + "' height='250'/></a>";
			}
			
			
			//valid values are: photo-url-[75, 100, 250, 400, 500, 1280]
    		$("#container").append("<li class='posts'><div class='object'>" + linkhref + "</div><div class='caption'>" + caption + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script><a href='http://twitter.com/share?url="+ post.url +"&via=Pringle of Scotland&count=horizontal' class='twitter-share-button'>Tweet</a></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}
    	case "conversation":
    	{		    		
 			var html = '';
    		$("#container").append("<a class='title' href='" + post.url + "' target='_blank'>" + post["conversation-title"] + "</a>");

				for(var i = 0; i < post["conversation"].length; i++) {
					var conv = post["conversation"][i];						
					html += "<div class='convlabel'>" + conv.label + "</div>";
					html += "<div class='convtext expandable'>" + conv.phrase + "</div>";
				}

				/*    		
				$(this).find("line").each(function(){
					html += "<div class='convlabel'>" + $(this).attr("label") + "</div>";
					html += "<div class='convtext'>" + $(this).text() + "</div>";
				});*/
    		$("#container").append("<li class='posts'><div class='body'>" + html + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><a href='http://twitter.com/share' class='twitter-share-button' data-text='" + title + "' data-count='horizontal' data-counturl='" + post.url + "' data-url='" + post.url + "' data-via='PringleScotland'>Tweet</a><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}
    	case "audio":
    	{				
			$("#container").append("<li class='posts'><h1><a class='title' href='" + post.url + "' target='_blank'>" + post["audio-caption"] + "</a></h1><div class='body'>" + post["audio-player"] + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><a href='http://twitter.com/share' class='twitter-share-button' data-text='" + title + "' data-count='horizontal' data-counturl='" + post.url + "' data-url='" + post.url + "' data-via='PringleScotland'>Tweet</a><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}
    	case "video":
    	{
var caption = post["video-caption"];
			
			if (caption.length > 500) {
				var newcaption = caption.substr(0,500);
				var end = newcaption.lastIndexOf(" ");
				
				caption = newcaption.substr(0,end) + " <a href='http://blog.pringlescotland.com/' target='_blank' class='readmore'> ...Read more</a>";
			}
			
			// resize our video code if possible
    		$("#container").append("<li class='posts'><div class='object'>" + post['video-player'] + "</div><div class='caption'>" + caption + "</div><div class='meta'><div class='icons'><iframe src='//www.facebook.com/plugins/like.php?href=" + post["url-with-slug"] +"&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:90px; height:21px;' allowTransparency='true'></iframe><script type='text/javascript' src='http://platform.twitter.com/widgets.js'></script><a href='http://twitter.com/share?url="+ post.url +"&via=Pringle of Scotland&count=horizontal' class='twitter-share-button'>Tweet</a></div><div class='post-info'><p class='info'>Posted on " + dateFmt + "</p></div></div></li>");
    		break;
    	}		    	
    	default:
    		break;
    }
}	



