$(document).ready(function() {
	// var $scrollingDiv = $("#scroll-with-me");
	// var $initialHeight = $('#sidebar').offset().top;
	// var $documentHeight = $(document).height();
	// $(window).scroll(function(){			
	// 	if(($initialHeight < $(window).scrollTop()+30) && $documentHeight > ($("#scroll-with-me").height()+$(window).scrollTop()+($initialHeight/2))) {
	// 		$scrollingDiv
	// 			.stop()
	// 			.animate({"marginTop": ($(window).scrollTop() - $initialHeight + 20) + "px"}, "slow" );			
	// 	}
	// });
	
	$('h2.expand-toggle').click(function() {
		$(this).next().slideToggle();
	});
	
	
	$('#article-add-comment').click(function (ev) {
		// ev.preventDefault();
		showComments();
	});
	$('.show-tags').click(function (ev) {
		// ev.preventDefault();
		showTags();
	});
	
	$('.close-overlay').click(function(ev) {
		ev.preventDefault();
		hideOverlays();
	});
	
	// set up images in sidebar
	$("a[href*='.jpg']").each(function () {
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		$(this).attr('rel', 'pretty[content]');
		$('div#article-images ul').append('<li><a rel="pretty[sidebar]" href="'+href+'" title="'+title+'"><img src="'+href+'" style="width:75px; height:75px;" /></a></li>');
	});
	
	// need to look at all of the anchors in the article body and put the images in lightboxes
	//$("a.pretty").prettyPhoto();
	$("a.[rel^='pretty']").prettyPhoto();
	
	// might need to process inline images and set up captions (prototype code from rusty is below)
	// Event.observe(window,'load', function() {
	// 		$$('img.caption-left').each(function(elm) {
	// 			$(elm).wrap('div', { 'class': $(elm).className,'style':'width:'+$(elm).width+'px' });
	// 			$(elm).insert({'after': '<span>'+$(elm).title+'</span>'});			
	// 			$(elm).removeClassName('caption-left');
	// 		});
	// 		$$('img.caption-right').each(function(elm) {
	// 			$(elm).wrap('div', { 'class': $(elm).className,'style':'width:'+$(elm).width+'px' });
	// 			$(elm).insert({'after': '<span>'+$(elm).title+'</span>'});			
	// 			$(elm).removeClassName('caption-right');
	// 		});
	// 	});
	
	
	$("a#add_comment_button").click(function (ev) {
		ev.preventDefault();
		$("#CommentAddForm").submit();
	});
	$("a#add_tag_button").click(function (ev) {
		ev.preventDefault();
		$("#TagAddForm").submit();
	});
	
	
});

function showComments() {
	// create overlay
	var height = $(document).height();
	$('#overlay_background').height(height);
	$('#overlay_background').show();
	
	// display comments and form
	$('#article-comments-overlay').show();
	
}

function showTags() {
	// create overlay
	var height = $(document).height();
	$('#overlay_background').height(height);
	$('#overlay_background').show();
	
	// display comments and form
	$('#article-tags-overlay').show();
	
}

function hideOverlays() {
	$('#overlay_background').hide();
	$('.overlay-box').hide();
}
