	var tumblr_api_read = null; // this is important do not remove

	$(document).ready(function() {	
			// setup our namespace
			$.tumblr = {};
			// modify these
			$.tumblr.url = "http://experiencebrands.tumblr.com/api/read/json?callback=?";
			$.tumblr.numPostsToDisplay = 12;
			$.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 = '';	
			$.tumblr.postCount = 0;
		
 			reloadTumblr();
  });		
  
  function reloadTumblr(){  	
  	$("#tumblrFeed").empty();
  	$("#tumblrFeed").append("<p><a href='http://blog.jackmorton.com'>Visit http://blog.jackmorton.com</a><br /><br /></p>");
  
  	$.ajax({
		  url: $.tumblr.url,
		  dataType: 'script',
		  timeout: 10000,
      success:function(){ 
      	$("#tumblrFeed").empty();
      	if ((tumblr_api_read == undefined) || (tumblr_api_read == null)) {
      		$("#tumblrFeed").append("<p>See our latest blog posts at:</p>");
      		$("#tumblrFeed").append("<p><a href='http://blog.jackmorton.com'>http://blog.jackmorton.com</a><br /><br /></p>");    			
    		} else {    			
    			$.tumblr.postCount = 0;
	        $.each(tumblr_api_read.posts.slice(0, 2), function(i,post){
						if ($.tumblr.postCount >= $.tumblr.numPostsToDisplay) {
							return;
						}    			
	    			parseTumblrJSON(post);
	    			$.tumblr.postCount++;
	  			});
	
	    		// Apply Expander
	    		/* if (postMaxDescriptionLength > -1) {
					  $('div.expandable').expander({
					    slicePoint:       postMaxDescriptionLength,  // default is 100
					    expandText:         '[more]',
					    userCollapseText: '[^]'
					  });					    			
	    		} */
    		}
      },
      error:function (xhr, statusTxt, errorTxt){
      		//do nothing
      } 					      
		});  	
  }
	
	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('dd-MMM-yyyy @ hh:mm tt')
	}
	
	function processResponse() {
	}
	
	function parseTumblrJSON(post) {
		//alert(post.type);
		var d = Date.parse(post["date-gmt"]);
		var dateFmt = formatDate(d);

	function truncatePost(str) {
		//replace HTML entities
		var justtext = str.replace('&#8216;', '\'');
		var justtext = justtext.replace('&#8217;', '\'');
		var justtext = justtext.replace('&#8220;', '\"');
		var justtext = justtext.replace('&#8221;', '\"');
		var justtext = justtext.replace('<p>', '');
		var justtext = justtext.replace('</p>', '');
		var justtext = justtext.replace('<span>', '');
		var justtext = justtext.replace('</span>', '');
		var justtext = justtext.replace('<strong>', '');
		var justtext = justtext.replace('</strong>', '');

		//truncate string
		var trunc = justtext.substring(0,100);
		trunc = trunc.replace(/\w+$/, '');
		trunc += '...';
		return trunc;
	}

	function cleanPost(str) {
		//replace HTML entities
		var slug = str.replace(/-/g, ' ');
		slug = slug.charAt(0).toUpperCase() + slug.slice(1);
		slug = slug.substring(0,40);
		slug = slug.replace(/\w+$/, '');
		slug += ' ...';
		return slug;
	}
		
    switch(post.type)
    {		    	
    	case "regular":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}
    	case "link":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}		    	
    	case "quote":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}		    	
    	case "photo":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}
    	case "conversation":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}
    	case "audio":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}
    	case "video":
    	{
				$("#tumblrFeed").append("<p><a href='" + post.url + "'>" + cleanPost(post.slug) + "</a></p>");
    		break;
    	}		    	
    	default:
    		break;
    }
     $("#tumblrFeed").append("<img src='/img/hr-dots-home.png' class='hr-blog' width='264' height='3' border='0' alt='-' />");
	}	
	


