var twtCount = 0;
var twtLimit = 5;
$(document).ready(function() {	
	//COMMENTS
	$('div.comment-wrap').find('ul.children').hide();	
	$('div.comment-wrap').each(function(){
		$this = $(this);
		if($this.children('ul.children').length > 0){
			$this.addClass('hasChildren');
		}
		if($this.children('ul.children').children('li').length > 1){
			$this.children('ul.children').addClass('multi');
		} else {
			$this.children('ul.children').addClass('single');
		}
	});	
	$('div.comment-wrap.hasChildren').each(function(){
		$this = $(this);
		$this.children('div.comment').children('div.footer').prepend('<span class="count">'+$this.children('ul').children('li').length+'</span> <a href="" class="see-replies">Show Replies</a>');
	});
	$('div.comment-wrap.hasChildren .comment a.see-replies').click(function(){
		$this = $(this);
		if($this.text() == 'Show Replies'){
			$this.text('Hide Replies');
		} else {
			$this.text('Show Replies');		
		}
		$this.parent().parent().siblings('ul.children').toggle();
		fauxcols();
		return false;
	});	

	//IN-PAGE NAV
	$('.lnkUp').click(function() {
		$('html,body').stop().animate({scrollTop: 0},1000, 'swing');
		return false;
	});
	$('.lnkDown').click(function() {
		$('html,body').stop().animate({scrollTop: $('html').height() - $(window).height()},1000, 'swing');
		return false;
	});
	
	//TWITTER
	var userName = '';
	$("p.twitter-link a").each(function(){
		showMinus();
		$t = $(this);
		userName = $t.text();
		$t.parent().before('<ul class="block-nav twitter"></ul>');
		$('ul.block-nav.twitter').hide();
		$t.hide(); 
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
				$t.fadeIn();
				fauxcols();
			});	
		});
	});
	
	$('.twitter p.see-more a').click(function(){
		var $t = $(this);
		$('.twitter ul li').remove();
		$t.hide();
		$t.parent().parent().addClass('loading');
		twtCount+=twtLimit;
		showMinus();
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
				$t.fadeIn();
				fauxcols();
			});	
		});
		return false;
	});
	
	$('.twitter p.see-less a').click(function(){
		showMinus();
		var $t = $(this);
		$('.twitter ul li').remove();
		$t.parent().parent().addClass('loading');
		twtCount-=twtLimit;
		showMinus();
		if(twtCount < 0) {
			twtCount = 0;
		}
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
				fauxcols();
			});	
		});
		return false;
	});
});
function fauxcols(){
	//FAUX, FAUX COLUMNS
	var colHeight = $('#col-2').height() > $('#col-3 .content').height() ? $('#col-2').height() : $('#col-3 .content').height();
	$('#col-2 .content').height(parseInt(colHeight)+44)
	$('#col-3 .content').height(parseInt(colHeight));
}
// Taken from http://twitter.com/javascripts/blogger.js  //
// Modified by Ollie Relph 15/07/09                      //
function twitterCallback(twitters) {
	var statusHTML = [];
	for (var i=0; i<twitters.length; i++){
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		  return '<a href="'+url+'" target="_blank">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
		});
		if(i>=twtCount && i<twtCount+twtLimit) {
			statusHTML.push('<li style="display:none;"><p>'+status+' <a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'" target="_blank">*</a></p></li>');
		}
	}
	$('ul.block-nav.twitter').append(statusHTML.join(''));
	$('ul.block-nav.twitter li').fadeIn();
}
function showMinus() {
	if(twtCount > 0) {
		$('.twitter p.see-less a').show();
	} else {		
		$('.twitter p.see-less a').hide();
	}
}
