/*
	Originally by Jacob DeHart:
	http://www.jacobd.com/post/22442355/tumblr-search-update
	
	Modified for maXimum 1337ness by james <at> bandit.co.nz
	http://blog.bandit.co.nz
*/
Tumblr = {
	'searchDB' : [],
	'searchStart' : 0,
	'searchNum' : 50,
	'searchLimit' : 5, // total number of results to show
	'searchBoxes' : 0,
	'searchBox' : function(){
		Tumblr.searchBoxes++;
		document.write(Tumblr.searchBoxHTML('SearchBox'+Tumblr.searchBoxes));
	},
	'searchBoxHTML' : function(id){
		html = '<input type="text" onkeyup="Tumblr.doSearch(this.value,\''+id+'\')" />';
		html += '<div id="'+id+'-results" class="searchresultbox"></div>';
		return html;
	},
	'doSearch' : function(value,id){
		ret = ''; count = 0; total = 0;
		titlelength = 32; // maximum character length of post titles
		previewlength = 96; // maximum character length of content preview
		
		leftpadding = Math.max(0,Math.floor((previewlength-value.length)/2));
		rightpadding = Math.max(0,Math.ceil((previewlength-value.length)/2));
		if(value!=''){
			for(i=0;i<Tumblr.searchDB.length;i++) {
				p = Tumblr.searchDB[i];
				
				if(typeof p['tags']!='undefined') tags = 'tagged: '+p['tags'];
				else tags = '';
				
				switch(p.type){
					case 'link':
						string = p['link-text'] + '. ' + p['link-description'] + '. ' + tags + ' ';
						title = 'link-text';
					break;
					case 'photo':
						string = p['photo-caption'] + '. ' + tags + ' ';
						title = 'photo-caption';
					break;
					case 'quote':
						string = p['quote-text'] + '. ' + p['quote-source'] + '. ' + tags + ' ';
						title = 'quote-text';
					break;
					case 'regular':
						string = p['regular-title'] + '. ' + p['regular-body'] + '. ' + tags + ' ';
						title = 'regular-title';
					break;
					case 'video':
						string = p['video-caption'] + '. ' + p['video-source'] + '. ' + tags + ' ';
						title = 'video-caption';
					break;
					default:
						string = '';
						title = '';
					break;
				}
				
				string = string.replace(/<\/?[^>]+>/gi, '');
				match = string.toLowerCase().indexOf(value.toLowerCase());
				
				if(match>-1){
					if(count<Tumblr.searchLimit) {
						if(p[title] != '') titlestr = p[title].replace(/<\/?[^>]+>/gi, '');
						else titlestr = p['url'];
						if(titlestr.length>titlelength) titlestr = titlestr.substr(0,titlelength)+"...";
						
						ret += '<a class="searchresultrow '+p.type+'" href="'+p['url-with-slug']+'">';
						ret += '<strong>'+titlestr+'</strong><br />';
						ret += '<div class="searchexcerpt">"...';
						ret += string.substr(match-leftpadding,leftpadding).replace('<','&lt;').replace('>','&gt;');
						ret += '<span>';
						ret += value.replace('<','&lt;').replace('>','&gt;');
						ret += '</span>';
						ret += string.substr(match+value.length,rightpadding).replace('<','&lt;').replace('>','&gt;');
						ret += '..."</div>';
						ret += '</a>';
						
						count++;
					}
					total++;
				}
			}
			if(ret == ''){
				ret = '<div class="searchresultrow">';
				ret += 'No results for that search!';
				ret += '</div>';
			}
			else {
				shown = Tumblr.searchLimit;
				if(total<Tumblr.searchLimit) shown = total;
				
				ret = '<div class="totalresults">Showing <strong>'+shown+'</strong> of <strong>'+total+'</strong> results. <a href="/search/'+value+'">See all &raquo;</a></div>'+ret;
			}
		}
		$('#'+id+'-results').html(ret);
	},
	'getData' : function(){
		$.get('/api/read/json?num='+Tumblr.searchNum+'&start='+Tumblr.searchStart,
			function(data){
				eval(data);
				for(i=0;i<tumblr_api_read.posts.length;i++){
					Tumblr.searchDB.push(tumblr_api_read.posts[i]);
				}
				if(tumblr_api_read['posts-total'] > (tumblr_api_read['posts-start']+Tumblr.searchNum)){
					Tumblr.searchStart = (tumblr_api_read['posts-start']+Tumblr.searchNum);
					Tumblr.searchNum = Math.min(Tumblr.searchNum,tumblr_api_read['posts-total'] - Tumblr.searchStart);
					Tumblr.getData();
				}else{
					//All data has loaded
				}
			}
		);
	}
};

$(document).ready(function(){
	Tumblr.getData();
});