	var tumblrURL = "http://aigajacksonville.tumblr.com/api/read/json?callback=?";
	var numPostsToDisplay = 4;
	var postCount = 0;
	// Make -1 to turn off jquery.expander
	var postMaxDescriptionLength = 200;
	var imagePath = 'http://chris-tran.com/images/tumblr/';	
	var tumblr_api_read = null;
		
	$(document).ready(function() {	
 		reloadTumblr();
  });		
  
  function reloadTumblr(){  	
  	$("#tumblrFeed").empty();
  	$("#tumblrFeed").append("<div class='blog-body'>loading Tumblr <img class='icon' src='" + imagePath + "small_red.gif'/></div>");
  
  	$.ajax({
		  url: tumblrURL,
		  dataType: 'script',
		  timeout: 10000,
      success:function(){ 
      	$("#tumblrFeed").empty();
      	if ((tumblr_api_read == undefined) || (tumblr_api_read == null)) {
      		$("#tumblrFeed").append("<div class='blog-title' href='#'>unable to load Tumblr</div>");
      		$("#tumblrFeed").append("<div class='blog-body'><a href=\"#\" onclick=\"javascript:reloadTumblr();\">retry</a></div>");    			
    		} else {
    			$("#tumblrFeed").append("<div class='blog-body'><a href=\"#\"  onclick=\"javascript:reloadTumblr();\">refresh</a></div>");
    			postCount = 0;
	        $.each(tumblr_api_read.posts.slice(0, 10), function(i,post){
						if (postCount >= numPostsToDisplay) {
							return;
						}    			
	    			parseTumblrJSON(post);
	    			postCount++;
	  			});
	
	    		// Do Not Apply Expander But Truncate Posts
	    		if (postMaxDescriptionLength > -1) {
					$('div.expandable').each(function(){
						var link   = $(this).prev("a.blog-title").attr("href");
						var curStr = $(this).text();
						var newStr = "<p>"+curStr.substr(0,200)+"<a href='"+link+"'> [...]</a></p>";

						$(this).html(newStr);
					});			    			
	    		}
    		}
      },
      error:function (xhr, statusTxt, errorTxt){
      		$("#tumblrFeed").append("<a class='blog-title' href='#'>Tumblr Parse Error</a>");
      		$("#tumblrFeed").append("<div class='blog-body'>" + errorTxt + "<br/>" + xhr.responseText + "</div>");
      } 					      
		});  	
  }
	
	function formatDate(d) {
		/*
Format  Description                                                                  Example
------  ---------------------------------------------------------------------------  -----------------------
 s      The seconds of the minute between 0-59.                                      "0" to "59"
 ss     The seconds of the minute with leading zero if required.                     "00" to "59"
 
 m      The minute of the hour between 0-59.                                         "0"  or "59"
 mm     The minute of the hour with leading zero if required.                        "00" or "59"
 
 h      The hour of the day between 1-12.                                            "1"  to "12"
 hh     The hour of the day with leading zero if required.                           "01" to "12"
 
 H      The hour of the day between 0-23.                                            "0"  to "23"
 HH     The hour of the day with leading zero if required.                           "00" to "23"
 
 d      The day of the month between 1 and 31.                                       "1"  to "31"
 dd     The day of the month with leading zero if required.                          "01" to "31"
 ddd    Abbreviated day name. Date.CultureInfo.abbreviatedDayNames.                  "Mon" to "Sun" 
 dddd   The full day name. Date.CultureInfo.dayNames.                                "Monday" to "Sunday"
 
 M      The month of the year between 1-12.                                          "1" to "12"
 MM     The month of the year with leading zero if required.                         "01" to "12"
 MMM    Abbreviated month name. Date.CultureInfo.abbreviatedMonthNames.              "Jan" to "Dec"
 MMMM   The full month name. Date.CultureInfo.monthNames.                            "January" to "December"

 yy     The year as a two-digit number.                                              "99" or "08"
 yyyy   The full four digit year.                                                    "1999" or "2008"
 
 t      Displays the first character of the A.M./P.M. designator.                    "A" or "P"
        $C.amDesignator or Date.CultureInfo.pmDesignator
 tt     Displays the A.M./P.M. designator.                                           "AM" or "PM"
        $C.amDesignator or Date.CultureInfo.pmDesignator
 
 S      The ordinal suffix ("st, "nd", "rd" or "th") of the current day.            "st, "nd", "rd" or "th"
 	*/
 		return d.toString('MMM dd/yy, hh:mmt')
	}
	
	function parseTumblrJSON(post) {
		//alert(post.type);
		var d = Date.parse(post["date-gmt"]);
		var dateFmt = formatDate(d);
		
    switch(post.type)
    {		    	
    	case "regular":
    	{
			$("#tumblrFeed").append("<div class='blog-post'><div class='blog-date'>" + dateFmt + "</div><a class='blog-title' href='" + post.url + "'>" + post["regular-title"] + "</a><div class='body expandable'>" + post["regular-body"] + "</div></div>");
    		
    		break;
    	}    	
 	
    	default:
    		break;
    }
	}	
	
	function parseTumblrXml(xml) {
		var postCount = 0;
		$(xml).find("post").each(function(){
			if (postCount >= numPostsToDisplay){
				return;
			}
						
	    switch($(this).attr("type"))
	    {		    	
	    	case "regular":
	    	{
					$("#tumblrFeed").append(
						"<div class='blog-post'><div class='blog-date'>" + $(this).attr("date") + "</div><a class='blog-title' href='" + $(this).attr("url") + "'>" + $(this).find("regular-title").text() + "</a><div class='body'>" + $(this).find("regular-body").text() + "</div></div>");
	    		break;
	    	}
	    	
	    	default:
	    		break;
	    }
	    postCount++;
	  });

	}

