
function getRecentPosts(args,tries)
{
	var url = "http://soyrex.tumblr.com/api/read/json/?callback=?";
	$('#recentposts').hide();
	$.getJSON(url,args, function(jsondata)
	{
		if(jsondata.posts.length == 0 && tries < 1)
		{
			var args = {type:'text',num:3};
			getRecentPosts(args,tries+1)
		}
		else
		{
			$(jsondata.posts).each(function(i,post)
			{
				var date_obj = new Date(post.date);
				var pdate = date_obj.getDate()+'/'+(date_obj.getMonth()+1)+'/'+date_obj.getFullYear();
				var post_html = '<li class="'+post.id+' '+post.type+'">'+
							'<a class="post-title" href="'+post['url-with-slug']+'"><span>'+
							post['regular-title']+
							'</span><small>'+pdate+'</small></a>'+
							'</li>';
				
				$('#recentposts').append(post_html)
			});
			$('#recentposts-loading').fadeOut('fast',function(){$('#recentposts').slideDown();});
		
		}
	});
}
$('#recentposts').ready(function()
{
	
	var args = {type:'text',num:10};
	
	var current_url = window.location.href;
	// we're on a tag page:
	if(current_url.match('/tagged/'))
	{
		var bits = current_url.split('/tagged/');
		tag = bits[1];
		args['tagged'] = tag;
	}
	else if(current_url.match('/post/'))
	{
		var thetags = $('#content .postinfo ul.tags > li > a');
		if(thetags.length > 0)
		{
			args['tagged'] = thetags[0].text;	
		}
	}
	
	getRecentPosts(args,0);
	
});

function getRecentLinks(args,tries)
{
	var url = "http://soyrex.tumblr.com/api/read/json/?callback=?";
	$('#recentlinks').hide();
	
	$.getJSON(url,args, function(jsondata)
	{
		if(jsondata.posts.length == 0 && tries < 1)
		{
			var args = {type:'link',num:3};
			getRecentLinks(args,tries+1)
		}
		else
		{
			$(jsondata.posts).each(function(i,post)
			{
				var date_obj = new Date(post.date);
				var pdate = date_obj.getDate()+'/'+(date_obj.getMonth()+1)+'/'+date_obj.getFullYear();
				var post_html = '<li class="'+post.id+' '+post.type+'">'+
							'<a title="'+post['link-description']+'" class="post-title" href="'+post['url-with-slug']+'"><span>'+
							post['link-text']+
							'</span><small>'+pdate+'</small></a>'+
							'</li>';
				
				$('#recentlinks').append(post_html)
			});
			$('#recentlinks-loading').fadeOut('fast',function(){$('#recentlinks').slideDown();});
			fixcols();
		}
		
	});
}
$('#recentlinks').ready(function()
{
	$(window).resize(fixcols);
	var args = {type:'link',num:10};
	
	var current_url = window.location.href;
	// we're on a tag page:
	if(current_url.match('/tagged/'))
	{
		var bits = current_url.split('/tagged/');
		tag = bits[1];
		args['tagged'] = tag;
	}
	else if(current_url.match('/post/'))
	{
		var thetags = $('#content .postinfo ul.tags > li > a');
		if(thetags.length > 0)
		{
			args['tagged'] = thetags[0].text;	
		}
	}
	getRecentLinks(args,0);
});


function fixcols()
{
	var boxes = ['#content','#right-wrap div.right-column','#right-wrap div.centre-column'];
	
	var newheight = 0;
	$(boxes).each(function(x,box)
	{
		var hbox = $(box).height();
		if(hbox > newheight) newheight = hbox;
	});
	
	
	$(boxes).each(function(x,box)
	{

		$(box).height(newheight);
	});
}

function getRelatedLinks(args,elem)
{
	var url = "http://soyrex.tumblr.com/api/read/json/?callback=?";
	
	$.getJSON(url,args, function(jsondata)
	{
		if(jsondata.posts.length == 0)
		{
			
			$(elem).append('<h3>No likely candidates found :(</h3>');
		}
		else if(jsondata.posts.length == 1)
		{
			var post = jsondata.posts[0];
			
			window.location.href = post['url-with-slug'];

		}
		else
		{
			$(jsondata.posts).each(function(i,post)
			{
				var date_obj = new Date(post.date);
				var pdate = date_obj.getDate()+'/'+(date_obj.getMonth()+1)+'/'+date_obj.getFullYear();
				var post_html = '<div class="'+post.id+' '+post.type+'">'+
							'<a class="post-title" href="'+post['url-with-slug']+'"><span>'+
							post['regular-title']+
							'</span><small>'+pdate+'</small></a>'+

							'</div>';
				
				
				$(elem).append(post_html)
			});
			$(elem+'-loading').fadeOut('fast',function(){$(elem).slideDown();});
		}
		
	});
}

$('.post h1 a').ready(function()
{
	
	if($('.post h1 a').text() == 'Not Found')
	{
		var args = {type:'regular',num:10};
	
		var search = window.location.href.split('/');
	
		var newsearch = [];
		$(search).each(function(i,item)
		{
			
			
			 if(item.length > 2)
			 	newsearch.push(item);
		});
		searchstr = newsearch[newsearch.length-1];
		searchstr = searchstr.replace(/\-/g,' ');		
		searchstr = searchstr.replace(/\b..\b/,'').replace(/\b[^ ]\b/,'').trim();
			
		args['search'] = searchstr;
		getRelatedLinks(args,'.post div.postcontent');
	
	}
});
