var posts = new Array();
var BLOG_URL = '/blog/?json=get_recent_posts';
var currPost = 0;
var postCount = 0;

function loadPosts() {
	$.ajax({
		url : BLOG_URL,
		dataType : 'json',
		success : function(data) {
			$.each(data.posts, function(i,item){
				posts.push({'id':item.id,'title':item.title,'content':item.content,'posturl':item.url,'excerpt':item.excerpt});
			});		

			if(data.count>0) {
				postCount=parseInt(data.count - 1);
				changeEntry(currPost);	
				$('#navNext').hide();
				$('#navSlash').hide();
			}	
		}
	});
}

function prevPost() {
	if(currPost<postCount) {
		$('#blogOverflow').fadeOut(50);
		currPost+=1;
		setTimeout("changeEntry("+currPost+");", 100);
		
		$('#navNext').show();
		if(currPost==postCount) {
			$('#navPrev').hide();
			$('#navSlash').hide();			
		} else {
			$('#navPrev').show();
			$('#navSlash').show();
		}
	}
}

function nextPost() {
	
	if(currPost>0) {
		$('#blogOverflow').fadeOut(50);
		currPost-=1;
		setTimeout("changeEntry("+currPost+");", 100);
		//changeEntry(currPost);
		$('#navPrev').show();
		if(currPost==0) {
			$('#navNext').hide();
			$('#navSlash').hide();			
		} else {
			$('#navNext').show();
			$('#navSlash').show();
		}
	}
}

function changeEntry(nr) {

	$('#blogTitle').html(posts[nr].title);
	Cufon.replace('#blogEntry h4 ', { fontFamily: 'ITC Franklin Gothic Std' });
	$('#blogEntryContent').hide();
	$('#blogEntryContent').html(posts[nr].content);
	$.each($('#blogEntryContent img'),function() { $(this).remove()});
	$('#blogEntryContent').show();

	if(posts[nr].excerpt!=undefined && posts[nr].excerpt!='') {
		$('#blogSubTitle').hide();
		$('#blogSubTitle').html(posts[nr].excerpt);
		$.each($('#blogSubTitle a'),function() { $(this).remove()});
		$('#blogSubTitle').show();
		$('#stars_2').show();
	} else {
		$('#blogSubTitle').html('').hide();
		$('#stars_2').hide();
	}
	setTimeout("showEntry()", 100);

}

function showEntry(){
	$('#blogOverflow').fadeIn(100);
}


$(document).ready(function() {
    loadPosts();
});
