/**
Tumblr Reader
Developed by Liquid24
http://www.liquid24.com
Version 2.0
*/
(function($) {
	$.fn.tumblr = function(o){
		var s = {
			url:"cyclestreet.tumblr.com",
			offset:0,
			count:3,
			type:"",
			tagged:"",
			bg_img:false
		},
		el = $(this),
		loader = $('<div class="loading" id="tumblr_loader">Loading</div>').hide().appendTo(el),
		tagged = "";
		loader.slideDown("fast");
		if(o){$.extend(s, o);}
		if(s.tagged){
			tagged = '&tagged='+s.tagged;
		}
		var url = 'http://'+s.url+'/api/read/json?start='+s.offset+'&num='+s.count+tagged+'&type='+s.type+'&callback=?';
		$.getJSON(url, function(data, state){
			if(data.posts.length > 0){	
				$.each(data.posts, function(i,posts){
					var type = this.type, permalink = this['url-with-slug'], 
					datetime = $('<abbr class="timeago" title="'+this.date+'">'+this['date-gmt']+'</abbr>').hide().appendTo(el),
					title='',
					tagged_el = $('<div class="tag">Blog</div>').hide().appendTo(el);
					el.addClass(type);
					if(jQuery.timeago){
						// el.find('abbr.timeago').timeago();
					}
					if(jQuery.cuteTime){
						$('abbr.timeago',el).cuteTime();
					}
					if(s.tagged){
						tagged_el.html(s.tagged);
					}
				
					switch(type){
						case 'regular':
							if(s.bg_img){
								var bg = this['regular-body'].match(/src=[\"\']([^\"\']+)/);
								if(bg){
									var bg_el = $('<span class="bg_img"></span>').css({
										position:'absolute', 
										top:0, 
										left:0, 
										width:'100%', 
										height:'100%'
									}).hide().appendTo(el);
									$('<img src="'+bg[1]+'"/>').css('width','100%').appendTo(bg_el);
								}
							}
							title = this["regular-title"];
							body = this["regular-body"];
							break;
						case 'video':
							source = this["video-player"];
							source = source.replace(/width=\"([^\"]*)\"/ig, 'width="460"').replace(/height=\"([^\"]*)\"/ig, 'height="300"');
							el.append(source);
							el.find('div.tag').css('display','none');
							break;
						case 'link':
							title = this["link-text"];
							break;
						case 'audio':
							source = this["audio-player"];
							el.append(source);
							break;
					}
					if(title){
						var title_el = $('<h3 class="title"></h3>').hide().appendTo(el);
						$('<a href="'+permalink+'">'+title+'</a>').appendTo(title_el);
					}
					el.css('cursor','pointer').click(function(){
						window.location = permalink;
					});
					loader.fadeOut('slow', function(){
						loader.remove();
						if(bg_el){
							bg_el.fadeIn();
						}
						/*el.find('abbr.timeago').css({
							top:'-30px'
						}).show().animate({
							top:0
						},'1000','swing',function(){*/
							tagged_el.css({
								left:-tagged_el.width()
							}).show().animate({
								left:0
							},'1000','swing',function(){
								if(title){
									title_el.css({
										bottom:'-30px'
									}).show().animate({
										bottom:0
									},'1000','swing');
								}
							});		
						//});
					});
				});
				el.find('abbr.timeago').css('display','none');
			}else{
				error = $('<div class="error">Oops, Nothing to display yet.</div>').hide().appendTo(el);
				loader.fadeOut('slow', function(){
					loader.remove();
					error.fadeIn('slow');
				});
			}
		});
	};
})(jQuery);
