// JavaScript Document

/* Copyright (c) 20089 Gustavo Beathyate (root@obviamente.pe)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 *
 * Requires: jQuery 1.3+
 *
 ****** CURRENTLY ONLY TESTED IN FF and Safari 3 *******
 */
 
(function($){jQuery.fn.writeTumblelog=function(options){
	var $opts=$.extend({},jQuery.fn.writeTumblelog.defaults,options);
	$jsonUrl="http://"+$opts.tumblrUrl+"/api/read/json?callback=?";
	$jsonUrl+="&start="+$opts.start;$jsonUrl+="&num="+$opts.num;
	if($opts.tagged!=null){
		$jsonUrl+="&tagged="+$opts.tagged}
	if($opts.type!=null){
		$jsonUrl+="&type="+$opts.type}
	var $postsContainer=$(this);
	$.getJSON($jsonUrl,function(data){embedPosts(data)});
	function embedPosts(data){
		$.each(data.posts,function(i,item){
			var $post=$($opts.post);
			$post.addClass(item.type);
			var $meta=$($opts.meta);
			formattedDate=formatDate(item.date,$opts.dateFormat);
			$($opts.date).html(formattedDate).appendTo($meta);
			permalink='<a href="'+item["url-with-slug"]+'">'+$opts.permalinkText+"</a>";
			$($opts.permalink).html(permalink).appendTo($meta);
			$post.append($meta);
			var $data=$($opts.data);
			switch(item.type){case"regular":$($opts.regularTitle).html(item["regular-title"]).appendTo($data);
			$($opts.regularBody).html(item["regular-body"]).appendTo($data);
			break;
		case"photo":if(item.photos.length>0){
		$post.removeClass("photo").addClass("photoset");
		for(var j=0;j<item.photos.length;j++){image=$($opts.photoImage).html('<img src="'+item.photos[j]["photo-url-"+$opts.photosetImageWidth]+'" />\n');caption=$($opts.photoCaption).html(item.photos[j]["caption"]+"\n");
		$($opts.photoContainer).append(image).append(caption).appendTo($data)}}else{image=$($opts.photoImage).html('<img src="'+item["photo-url-"+$opts.photoImageWidth]+'" />\n');caption=$($opts.photoCaption).html(item["photo-caption"]+"\n");
		$($opts.photoContainer).append(image).append(caption).appendTo($data)}
		break;
		case"quote":$($opts.quoteText).html(item["quote-text"]).appendTo($data);$($opts.quoteSource).html(item["quote-source"]).appendTo($data);
		break;
		case"link":link='<a href="'+item["link-url"]+'">'+item["link-text"]+"</a>";
		$($opts.linkContainer).html(link).appendTo($data);
		$($opts.linkDescription).html(item["link-description"]).appendTo($data);
		break;
		case"conversation":$($opts.conversationTitle).html(item["conversation-title"]).appendTo($data);$conversationContainer=$($opts.conversationContainer);
		for(var j=0;j<item.conversation.length;j++){$($opts.conversationLabel).addClass(item.conversation[j]["label"]).html(item.conversation[j]["label"]).appendTo($conversationContainer);$($opts.conversationPhrase).addClass(item.conversation[j]["label"]).html(item.conversation[j]["phrase"]).appendTo($conversationContainer)}$conversationContainer.appendTo($data);break;case"video":$($opts.videoPlayer).html(item["video-player"]).appendTo($data);
		$($opts.videoCaption).html(item["video-caption"]).appendTo($data);break;case"audio":$($opts.audioPlayer).html(item["audio-player"]).appendTo($data);
		$($opts.audioCaption).html(item["audio-caption"]).appendTo($data);break;default:break}$post.append($data);
		$postsContainer.each(function(){$(this).append($post)})})}function formatDate(date,dateFormatString){_d=new Date(date);a=$opts.shortWeekDays[_d.getDay()];A=$opts.longWeekDays[_d.getDay()];b=$opts.shortMonths[_d.getMonth()];B=$opts.longMonths[_d.getMonth()];d=(_d.getDate()<10?"0"+_d.getDate().toString():_d.getDate().toString());H=_d.getHours();y=_d.getYear();Y=_d.getFullYear();m=(_d.getMonth()<10?"0"+_d.getMonth().toString():_d.getMonth().toString());M=_d.getMinutes();
		if(!dateFormatString){dateFormatString="%d/%m/%Y"}$dateOutput="";for(var i=0;i<dateFormatString.length;i++){if(dateFormatString[i]=="%"){$dateOutput+=eval(dateFormatString[++i])}
		else{$dateOutput+=dateFormatString[i]}}return $dateOutput}};
		
		jQuery.fn.writeTumblelog.defaults={
			tumblrUrl:null,start:0,num:5,tagged:null,type:null,post:'<div id="blog-post"><div class="post"></div></div>',meta:'<div class="meta"></div>',data:'<div class="data"></div>',permalink:'<span style="border: 1px solid white; background: black; padding: 1px; margin: 3px;"><p class="permalink"></p></span>',permalinkText:"Tumblr Blog Link",date:'<div id="blog-post-date"><p class="date"></p></div>',dateFormat:"%A, %d %B %Y",longWeekDays:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],shortWeekDays:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],longMonths:["January","February","March","April","May","June","July","August","September","October","November","December"],shortMonths:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],regularTitle:"<h2></h2>",regularBody:"<div></div>",photoContainer:'<div class="photo-item"></div>',photoImage:"<p></p>",photoCaption:'<p class="caption"></p>',photoImageWidth:500,photosetImageWidth:100,quoteText:"<blockquote></blockquote>",quoteSource:'<p class="quote-source"></p>',linkContainer:"<h2></h2>",linkDescription:'<p class="caption"></p>',conversationTitle:"<h2></h2>",conversationContainer:"<dl></dl>",conversationLabel:"<dt></dt>",conversationPhrase:"<dd></dd>",videoPlayer:'<div class="video-player"></div>',videoCaption:'<p class="caption"></p>',audioPlayer:'<div class="video-player"></div>',audioCaption:'<p class="caption"></p>'}})
(jQuery);
