$(document).ready(function() {
	
	Cufon.replace($('h1, h2, h3, .comments-link,.entry-metadata').not('.transcript h4'));
	
	$('div#content div[id^=post]').each(function() { 
		var postId = $(this).attr('id');
		$('div#'+postId+' a[href$=".png"]').addClass('fancybox').attr('rel', postId);
		$('div#'+postId+' a[href$=".gif"]').addClass('fancybox').attr('rel', postId);
		$('div#'+postId+' a[href$=".jpg"]').addClass('fancybox').attr('rel', postId);
		$('div#'+postId+' a[href$=".PNG"]').addClass('fancybox').attr('rel', postId);
		$('div#'+postId+' a[href$=".GIF"]').addClass('fancybox').attr('rel', postId);
		$('div#'+postId+' a[href$=".JPG"]').addClass('fancybox').attr('rel', postId)
	});
	
	$('a.fancybox').fancybox({
		'overlayShow' : true,
		'hideOnContentClick': true
		});

	$('.transcript').disclosurize();
		
	$('table tr').removeClass('odd even');
	$('table tr:even').addClass('odd');
		
	$('#comments-list .comment-meta').append(' <span class="meta-sep">|</span> <a class="reply" href="#reply">Reply</a>');
	
	$('.reply').click(function() {
		var who = $(this).parents('.comment').children('.comment-author').children('.fn').text();
		var where = $(this).parents('.comment').attr('id');
		var replyTo = '<a href="#'+where+'">@'+who+':</a> ';
		var previousText = $('textarea#comment').val();
		var previousTextLength = previousText.length;
		var newText = previousText + '\r\r' + replyTo;
		
		var commentForm = $('form#commentform').clone(true);
		$('div#respond, form#commentform').remove();
		commentForm.insertAfter(this);
		
		if ( previousTextLength != '0' ) { 
			$('textarea#comment').focus().val(newText)
		} else {
			$('textarea#comment').focus().val(replyTo)
		}
		
		return false;
	});
	
	//scroll scales the RSS icon
	$(window).scroll(function () {
		var y = $(window).scrollTop(),
		$rss = $('#rssbar>img'),
		rssx, rssy;
		if (y<100) {
			$rss.css({
				width: 225, height: 94
			});
		} else if (y>100 && y<200) {
			rssx = 225 - (y-100);
			rssy = parseInt(94/(225/rssx));
			$rss.css({
				width: rssx, height: rssy
			})
		} else if (y>200) {
			$rss.css({
				width: 125,
				height: 52
			});
		}
//		console.log(y);
	});
});

(function($){
  $.fn.disclosurize = function() {
  	var that = this;
  	//hide
  	$("h4", this).addClass('collapsed');
  	$(".content", this).hide();
  	//bind toggle event
  	$("h4", this).toggle( function () {
  		//expand
			$(".content",that).show(200);
			$("h4", that).removeClass('collapsed');
  	}, function () {
  		//hide
			$(".content",that).hide(200);
			$("h4", that).addClass('collapsed');
  	});
  }
})(jQuery);  
