var n_post = -1
var n_anim_speed = 200
var a_posts = []
//
$(document).ready(function() {
	shortcut.add("Down", next_post);
	shortcut.add("Up", previous_post)
	shortcut.add("Left", previous_page)
	shortcut.add("Right", next_page)
	currentPostID = $(".post:first").attr("id")
	
	$("#searchfield").focus(function(){
		$(this).addClass("tf_focused")
		if($(this).val() == "Type and press enter."){
			$(this).val("")
		}
	})
	$("#searchfield").blur(function(){
		$(this).removeClass("tf_focused")
		if($(this).val() == ""){
			$(this).val("Type and press enter.")
		}
	})
	$("#loading").fadeTo(200, 0)
	$(".post").each(function(){
		a_posts.push({_id:$(this).attr("id"), _y:$(this).position().top})
	})
	//
	a_posts.sort(sort_posts)
	//
	if(twitter_username != "" && current_page == 1){
		get_twitter_status()
	}
	//
	$("#posts").append("<div class=\"entry credit\"><div class=\"icon\"><a href=\"http://jbd2.com\" class=\"permalink\"></a></div><div class=\"content\"><div class=\"caption\"><p>Theme by <a href=\"http://log.jbd2.com\">Jarred Bishop</a>. <a href=\"http://seointelligence.com\">SEO Tools</a> by <a href=\"http://www.seointelligence.com/pricing-plans.php\">SEOintelligence</a>.</p></div></div></div>")
});
//
function sort_posts(a, b){
	return (a._y - b._y)
}
//
function next_post(){
	scroll_to_post(n_post+1)
	return false
}
//
function previous_post(){
	scroll_to_post(n_post-1)
	return false
}
//
function scroll_to_post(n_){
	if(n_ < 0){
		previous_page()
	}else if(n_ > a_posts.length-1){
		next_page()
	}else{
		$("body").animate({scrollTop:$("#"+a_posts[n_]._id).position().top-10}, n_anim_speed)
		n_post = n_
	}
}
//
function get_twitter_status(){
	$("#twitter").removeClass("hide")
	$("#twitter_status").getTwitter({
			userName: twitter_username,
			numTweets: 1,
			loaderText: "loading...",
			slideIn: false,
			showHeading: false,
			headingText: "",
			showProfileLink: false
	});
	//
	//var avatar_url = "http://twitteravatar.appspot.com/users/avatar/"+twitter_username+"/mini"
	//$("#twitter .icon a.permalink").css("background-image","url('"+avatar_url+"')");
	$("#twitter .icon a.permalink").click(function(){
		window.location = "http://twitter.com/"+twitter_username
		return false
	})
	$("#twitter p.latest").html("<a href=\"http://twitter.com/"+twitter_username+"\">@"+twitter_username+"</a> from twitter:")
	//setInterval(check_f, 5000)
}
//
function check_f(){
	alert($("#twitter_status").html())
}
// thanks matthewb: http://matthewbuchanan.name/post/95048088/disabling-diggbar
if ((top !== self) && (document.referrer.match(/http:\/\/digg.com\/\w{1,8}\/*(\?.*)?/))) {
	top.location.replace(self.location.href);
}