// BLOG
// toggle the comments
$('a.view_comments').each(function(){
	var count = $(this).parent('.links').next('.comments').find('.comment').length;
	$(this).text($(this).text()+' ('+count+')');
}).click(function(){
	$(this).trigger('toggle', true);
	return false;
}).bind('toggle', function(event, jumpTo){
	$link = $(this);
	// slide the comments into place with a callback
	$(this).parent('.links').next('.comments').slideToggle('fast',function(){
		// toggle view/leave with hide
		text = $link.text();
		if(text.indexOf("View/Leave")>=0){
			text = text.replace("View/Leave","Hide");
			if(jumpTo)
				$link.trigger('jumpTo');
		} else {
			text = text.replace("Hide","View/Leave");
			// dont need a scroll to since we are already at the links
		}
		$link.text(text);
	});
	return false;	
}).bind('jumpTo', function(){
	$.scrollTo($(this).parent(),'fast');	
});
// if there's only one blog post show the comments automatically
if($('div.blog_post').length==1){
	$(this).find('a.view_comments').trigger('toggle',false);
}
// SUBMITTING COMMENTS
$('.comment_form').submit(function(){
	if(!$(this).find('input[name="email"]').val()){
		alert('You must supply your email address');
		return false;
	} else if(!$(this).find('textarea').val()){
		alert('You must supply a comment');
		return false;
	}
	// save the parent div.comments for inserting new comment to proper place
	var $comments = $(this).parents('.comments');
	var i = $(this).find('input[name="i"]').val() * 1;
	var new_i = i;
	new_i = new_i + 1;
	
	var form_data = $(this).serialize();
	$.ajax({
		url:'blog_includes/ajax_comment.php',
		data: form_data,
		type: "POST",
		beforeSend: function(){
			$('body').css('cursor','wait');
			$('.comment_form input, .comment_form textarea').attr('disabled','disabled');
		},
		success: function(data){
			if(data!="0"){
				// insert the comment
				if($('.comment:last',$comments).length)
					$('.comment:last',$comments).after(data);
				else if($('.none',$comments).length)
					$('.none',$comments).replaceWith(data);
				else
					window.location.reload();// if there's neither of the two options just refresh cause I don't know whats going on

				// update the i count
				$('input[name^="i"]',$comments).val(new_i);

				// now update the comment link
				var text = $comments.prev('.links').children('a.view_comments').text();
				if(text.indexOf("(")>=0){
					text = text.replace(/\(\d+\)/, "("+i+")")
				} else {
					text += ' ('+i+')';
				}
				$link.text(text);
			} else {
				alert('Sorry, but there was an error and you comment could not be posted at this time. Please try again later');
			}
			$('body').css('cursor','auto');
			$('.comment_form input, .comment_form textarea').removeAttr('disabled').not('input[type="submit"]').not('input[type="hidden"]').val('');
		}
	});

	return false;
});
// ARCHIVE LIST
$('#blog_archives li.year').click(function(){
	$(this).children('ul').toggle();
}).find('li.month').click(function(){
	$(this).children('ul').toggle();
	return false;
}).find('a').bind('click',function(e){
	e.stopPropagation();
});
