/*
Tumblr - get recent posts

*/

if(tumblr_api_read){
	var posts = tumblr_api_read['posts'];

	
	var output = "";
	var i = 0;
	var type = '';
	var finalposts = new Array();
	
	var test = '';
	
	for(i = 0; i < 5; i++){
		if(posts[i]){
			
			finalposts[i] = new Array();
			
			finalposts[i]['url'] = posts[i]['url'];
			finalposts[i]['type'] = posts[i]['type'];
			
			if(posts[i]['title']){
				finalposts[i]['title'] = posts[i]['title'];
			} else {
				if(finalposts[i]['type'] == "regular"){ //regular posts
					
					finalposts[i]['title'] = getsummary(posts[i]['body'], 45);
				
				} else if(finalposts[i]['type'] == "photo"){ //Photo
					
					if(posts[i]['photo-caption']){
						finalposts[i]['title'] = getsummary(posts[i]['photo-caption'], 45);
					} else {
						finalposts[i]['title'] = "Untitled Photo";
					}
									
				} else if(finalposts[i]['type'] == "quote"){ //quotes
					
					finalposts[i]['title'] = getsummary(posts[i]['quote-text'], 45);
				
				} else if(finalposts[i]['type'] == "link"){ //links
					
					if(posts[i]['link-text']){
						
						finalposts[i]['title'] = posts[i]['link-text'];
						
					} else if(posts[i]['link-description']){
						
						finalposts[i]['title'] = getsummary(posts[i]['link-description'], 45);
					
					} else{
						
						finalposts[i]['title'] = getsummary(posts[i]['link-url'], 45);
					
					}
				
				} else if(finalposts[i]['type'] == "conversation"){ //Conversation
					
					if(posts[i]['conversation-title']){
						finalposts[i]['title'] = posts[i]["conversation-title"];
					} else {
						finalposts[i]['title'] = getsummary(posts[i]["conversation-text"], 45);

					}				
				} else if(finalposts[i]['type'] == "video"){
					
					if(posts[i]['video-caption']){
						finalposts[i]['title'] = getsummary(posts[i]['video-caption'], 45);
					} else {
						finalposts[i]['title'] = "Untitled Video";
					}
				
				} else if(finalposts[i]['type'] == "audio"){
					
					if(posts[i]['audio-caption']){
						finalposts[i]['title'] = getsummary(posts[i]['audio-caption'], 45);
					} else {
						finalposts[i]['title'] = "Untitled Audio";
					}
				} else {
					finalposts[i]['title'] = "Untitled Post";
				}
			}
		} else {
			break;
		}
	}
	
	
	if(finalposts.length > 0){
		document.write('<div class="module module_latest_posts"><h3 class="sectiontitle">Latest.</h3><ul class="titlelist">');
		
		var j = 0;
		for(j = 0; j < finalposts.length; j++){
			document.write('<li class="titleitem"><h2><a href="' + finalposts[j]['url'] + '">' + finalposts[j]['title'] + '</a></h2></li>');
		}
		
		document.write('</ul></div>');
	}
	
}


function getsummary(string, l){
	if (string) {
		
		string = string.replace(/<a[^href]+href=\"([^\"]*)\"[^>]*>/ig,"$1"); //replace links with urls
		string = string.replace(/(<([^>]+)>)/ig,""); //remove tags
		
		//string = string.replace(/^(\s)*/, ''); removing spaces before and after
		//string = string.replace(/(\s)*$/, '');

		
	  if (string.length > l) {
	
		string = string.substring(0, l);
		string = string.replace(/\w+$/, '');
		
		string += "&hellip;";
					
	  }
	  
	  return string;
	  
	}
}