/*---------------
 * jQuery tumblr Plugin by mrnhim
 * Examples and documentation at: http://www.nhimblog.net
 * Copyright (c) 2009 Long Nguyen Hai
 * Version: 1.0 (23-AUG-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.3 or later
---------------*/

(function($){
	$.fn.tumblr = function(options) {
		
		var defaults = {
			start: 0,
			num: 5,
			type: null,
			id: null,
			username: 'mrnhim',
			filter: 'text',
			loading: ' loading ...',
			onComplete: function(){}
		},
		settings = $.extend({}, defaults, options);
		
		var startOpt = idOpt = typeOpt = '';
		
		if (settings.start > 0) { startOpt = '&start='+settings.start; }
		if (settings.id != null) { idOpt = '&id='+settings.id; }
		if (settings.type != null){ typeOpt = '&type='+settings.type; }

		var tumblrUrl = 'http://'+settings.username+'.tumblr.com/api/read/json?filter='+settings.filter+'&num='+settings.num+typeOpt+startOpt+idOpt+'&callback=?';
		var $this = $(this);
		var container = $this.html();
		
		$this.children().remove();
		for (var i=0; i < settings.num; i++) {
			$this.append(container);
			var $current = $this.children(':eq('+i+')');
			$current.find('[class=tumblr_post]').html(settings.loading);
		};

		this.each(function() {
			$.getJSON(tumblrUrl, function(data){ 
				$.each(data.posts,function(i, item){
					
					url = stripslashes(item['url-with-slug']);
					separate = ' &mdash; ';
					switch (item['type']) {
						case 'regular':
							prefix = '';
							title = item['regular-title'];
							body = item['regular-body'];
							break;
							
						case 'quote':
							prefix = '';
							title = item['quote-text'];
							body = item['quote-source'];
							break;
							
						case 'photo':
							prefix = 'Photo: ';
							title = item['photo-caption'];
							body = '';
							break;
							
						case 'link':
							prefix = '';
							title = item['link-text'];
							body = item['link-description'];
							break;
							
						case 'conversation':
							prefix = 'Chat: ';
							title = item['conversation-title'];
							body = '';
							break;
							
						case 'video':
							prefix = 'Video: ';
							title = item['video-caption'];
							body = '';
							break;
							
						case 'audio':
							prefix = 'Audio: ';
							title = item['audio-caption'];
							body = '';
							break;
						
						default:
							prefix = '';
							title = item['regular-title'];
							body = item['regular-body'];
					}
					
					// remove separate if title or body empty
					if ((title == '')||(body == '')) { separate = ''; }
					
					// ok, process output string
					output = prefix + title + separate + body;
					
					// wanna shorten string for better view?
					if (output.length > 45) { output = output.substring(0, 45) + " &hellip;"; }
					
					var $current = $this.children(':eq('+i+')');
					$current.find('[class=tumblr_post]').html(output);
					$current.find('a').attr('href', url).attr('title', title + " " + body).attr('target', '_blank');
					
					// call back
					if(i==(settings.num-1)){
						settings.onComplete.call(this);
					}
				});
			});
		});
	};
	
	//Clean up the URL's
	function stripslashes( str ) {	 
		return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
	}
})(jQuery);