/* --------------------------------------------------------------
File:			tumblr.js
Version:        1.0
Last Modified:  June-21-2011
-----------------------------------------------------------------
Description:    Code has been adapted from information found on
				the web with special recognition to
				https://chris-tran.com/blog/?p=236
================================================================= */



function getTumblr(tumblrJsonUrl, postQuantity, startAtPost, maxCharacters) {

	if (typeof(postQuantity) == 'undefined') postQuantity = 1;
	if (typeof(startAtPost) == 'undefined') startAtPost = 1;
	if (typeof(maxCharacters) == 'undefined') maxCharacters = 0;

	var start = startAtPost - 1;
	var end = start + postQuantity;

	$.getJSON(tumblrJsonUrl, function(data) {
		$.each(data.posts.slice(start, end), function(i,posts){
			var title = this["regular-title"];
			var type = this.type;
			var date = this.date;
			//var date2 = Date.parse(this.date);
			var dateFmt = formatDate(date);
			var dateFmt2 = formatDate2(date);
			var url = this["url-with-slug"];
			if (maxCharacters != 0) {

				var bodytext = this["regular-body"];

				var bodytrim = cutHtmlString(bodytext, maxCharacters);

				$("#tumblr-post").append('<div class="leadstory"><a href=' + url +'>' + title + '</a></div><p class="dateposted">Posted ' + dateFmt + '</p>');

				if (bodytext.length > bodytrim.length) {
					var bodytrim = bodytrim.slice(0, bodytrim.lastIndexOf(' ')) + '</a> &hellip;</p>';
					$("#tumblr-post").append('<div class="storytext">' + bodytrim + '</div>');
					$("#tumblr-post").append('<div class="storytext"><p><a href="' + url + '">Continue Reading</a></p></div>');
				}

			} else {

				$("#tumblr-list").append('<dt>' + dateFmt2 + '</dt><dd><ul></li><a href=' + url +'>' + title + '</a></li></ul></dd><div class="clear"></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('MMMM d, yyyy')
	};
function formatDate2(d) {
	return d.toString('MMM<br />d')
	}
