$(document).ready(function(){
	
	$("#nav").localScroll();
	
	$(".commentlist li").hide();
	var comments = $(".commentlist li").length;
	var visible = 5;
	var start = 1;
	var pages = Math.ceil(comments/visible);
	function showcommentpage(start) {
		end = Math.ceil(start*visible)+1;
		active = start;
		start = end-visible;
		$(".commentlist li").each(function() {
			var index = $(".commentlist li").index(this)+1;
			if(index>=start && index<end) {
				$(this).fadeIn(500);
			}
			else {
				$(this).hide();
			}
		});
		if(comments<start+visible) {
			$(".next-comment-page").hide();
		}
		else $(".next-comment-page").show();
		if(start>1) {
			$(".prev-comment-page").show();
		}
		else $(".prev-comment-page").hide();
		
		$(".comment-page-link").removeClass("active");
		$(".comment-page-" + active).addClass("active");
		
	}
						
	if(comments>visible) {
		
	$("<ol />").addClass("comment-paging").insertAfter(".commentlist");
	
	$(".comment-paging").prepend('<li><a href="#prev-comment-page" class="prev-comment-page">Previous</a></li>').find(".prev-comment-page").hide().click(function() {
		showcommentpage(active-1);
	});
	
	for(i=1;i<=pages;i++) {
		$(".comment-paging").append('<li><a href="#comments" class="comment-page-link comment-page-'+i+'">' + i + '</a></li>');
	}
	
	$(".comment-paging").append('<li><a href="#next-comment-page" class="next-comment-page">Next</a></li>').find(".next-comment-page").click(function() {
		
		showcommentpage(active+1);
		
	});
	
	$(".comment-page-link").each(function() {
		$(".comment-page-1").addClass("active");
		$(this).click(function() {
			showcommentpage(eval($(this).text()));					
		});
	});
	
	
	}								
	showcommentpage(start);
	function gettweet(user) {
		$.ajax({
			'url': 'http://twitter.com/statuses/user_timeline/' + user + '.json',
			'data': { count: 10 },
			'success': showtweet,
			'dataType': 'jsonp'
		});
	}

	function showtweet(tweet) {
		$.each(tweet, function() {
			if(!this["in_reply_to_user_id"]) {
				
				$("#twitter").html('<ul class="twitter"><li class="twitter-item">&bdquo;<a href="http://twitter.com/' + this["user"]["screen_name"] + '/status/' + this["id"] + '" class="twitter-link">' + this["text"] + '</a>&ldquo;<span class="twitter-timestamp">' +  DateHelper.time_ago_in_words_with_parsing(this["created_at"]) + '</span></li></ul>').fadeIn();
				return false;
			}
			
		});
	}
	$("#twitter").hide();
	gettweet("NorthKingdom");
		var DateHelper = {
		  // Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
		  // Ruby strftime: %b %d, %Y %H:%M:%S GMT
		  time_ago_in_words_with_parsing: function(from) {
		    var date = new Date; 
		    date.setTime(Date.parse(from));
		    return this.time_ago_in_words(date);
		  },

		  time_ago_in_words: function(from) {
		    return this.distance_of_time_in_words(new Date, from);
		  },

		  distance_of_time_in_words: function(to, from) {
		    var distance_in_seconds = ((to - from) / 1000);
		    var distance_in_minutes = Math.floor(distance_in_seconds / 60);
		    if (distance_in_minutes == 0) { return 'less than a minute ago'; }
		    if (distance_in_minutes == 1) { return 'a minute ago'; }
		    if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes ago'; }
		    if (distance_in_minutes < 90) { return 'about 1 hour ago'; }
		    if (distance_in_minutes < 1440) { return 'about ' + Math.floor(distance_in_minutes / 60) + ' hours ago'; }
		    if (distance_in_minutes < 2880) { return '1 day ago'; }
		    if (distance_in_minutes < 43200) { return Math.floor(distance_in_minutes / 1440) + ' days ago'; }
		    if (distance_in_minutes < 86400) { return 'about 1 month ago'; }
		    if (distance_in_minutes < 525960) { return Math.floor(distance_in_minutes / 43200)+ ' months ago'; }
		    if (distance_in_minutes < 1051199) { return 'about 1 year ago'; }

		    return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years ago';
		  }
		};
	
 });