// To-Do: replace all references to "tumblr_api_read"

// by David Lantner - http://lantner.net/david
$('#hcard-David-Lantner').animate({width:'25em', marginLeft:'0', left:'0'}, 250).addClass('thin');
$('body').append('<div id="tumblr"><h2>Recently <a href="http://davidl.tumblr.com/" title="my tumblelog" rel="me">tumbled</a>:</h2><ul id="posts" aria-live="polite" aria-relevant="additions"></ul><a id="more" href="#" role="button" title="Load more items">More</a></div><div id="loading" aria-live="assertive" aria-relevant="additions"></div>');
function getPosts(startnum,num) {
	$('#loading').text('Loading...').fadeIn('fast');
	$.getJSON('http://davidl.tumblr.com/api/read/json?start=' + startnum + '&num=' + num + '&callback=?', function(data) {
		if (startnum == 0) {
			$('h2:first', $('#tumblr')).css('margin-top', $('#hcard-David-Lantner h2').offset().top + 'px');
		}
		var next = ($('#posts li:last a.perm').text() / 1); // set this for the scroll afterwards
		for (var i=0, post; post = data.posts[i]; i++) {
			var li = document.createElement('li');
			li.setAttribute('class', post['type']);
			/* all posts have the following attributes:
				[id]
				[url] (tumblr permalink)
				[type]
				[date]
				[feed-item]
				[from-feed-id] -> Twitter: "205815", del.icio.us: "137941", Flickr (photos): "137941"
			*/
			// add a class to list item: post['type'] or post['from-feed-id']:
			(post['from-feed-id'] == 0) ? $(li).addClass(post['type']) : $(li).addClass('feed' + post['from-feed-id']);
			
			//add the permalink:
			$(li).append('<a href="' + post['url'] + '" class="perm" title="permanent link for this post at Tumblr">'+ (data['posts-start'] + 1 + i) + '</a><span class="hide">: </span>'); // append the permalink
			
			if (post['type'] == 'regular') {
				if (post['from-feed-id'] == '205815') { // check for Twitter posts
					$(li).append('<p>' + post['regular-body'] + ' <a href="' + post['feed-item'] + '" title="permanent link for this tweet at Twitter">#</a></p>');
				} else {
					$(li).append('<h3>' + post['regular-title'] + '</h3><p>' + post['regular-body'] + '</p>');
				}
			} else if (post['type'] == 'link') {
				//$(li).append('<a href="' + post['link-url'] + '" class="link-url">' + post['link-text'] + '</a><p>' + post['link-description'] + '</p>');
				$(li).append('<a href="' + post['link-url'] + '" class="link-url">' + post['link-text'] + '</a>');
				if (post['link-description'] != '') {
					$(li).append('<p>' + post['link-description'] + '</p>');
				}
			} else if (post['type'] == 'quote') {
				// &mdash; not allowed in XML so we replace with it with its numberic entity equivalent &#8212;
				$(li).append('<q>' + post['quote-text'] + '</q><br /><span>' + post['quote-source'].replace(/mdash/, '#8212') + '</span>');
			} else if (post['type'] == 'photo') {
				$(li).append('<img src="' + post['photo-url-400'] + '" alt="photo" /><span class="photo-caption">' + post['photo-caption'] + '</span>');
			} else if (post['type'] == 'conversation') {
				$(li).append('<em>conversation (coming soon...)</em>');
			} else if (post['type'] == 'video') {
				$(li).append( post['video-caption'] + '<br /><br />' + post['video-player']);
			} else if (post['type'] == 'audio') {
				$(li).append('<a href="' + post['audio-source'] + '">' + post['audio-caption'] + '</a><br />' + post['audio-player']);
			}
			$('#posts').append(li); // append the list item
		}
		if (window.navigator.platform == 'iPhone') {
			$('#hcard-David-Lantner').addClass('auto');
			$('#more').addClass('wide');
		}
		if((data['posts-start'] + 5) >= data['posts-total']) { // 'end' case
			$('#more').remove();
			$('#tumblr').append('<span id="noMore">That\'s all, folks.</span>');
		}
		if(startnum != 0) {
			// check for IE 6 and below. Thanks, Jesse: http://www.thefutureoftheweb.com/blog/detect-ie6-in-javascript
			var lteIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
			if (lteIE6 == true) { // 'next' is defined above
				$('body').animate({scrollTop: $('#posts > li:eq(' + next + ')').offset().top}, 250);
			} else {
				$('html').animate({scrollTop: $('#posts > li:eq(' + next + ')').offset().top}, 250);
			}
			$('#posts > li:eq(' + next + ') a.perm')[0].focus();
		}
		$('#loading').fadeOut('slow', function(){
			$(this).empty();
		});
	});
}
	
$('#more').click(function() {
	getPosts($('li', $('#posts')).length, 5);
	return false;
});

$(window).load(function () {
	getPosts(0,5); // initial call
});