$(document).ready(function(){
	
	
	//
	// Sharing
	//
	
	
	
	// Facebook sharing requires a new window
	$("A.share.facebook").live('click', function() {
		$(this).attr('target', '_blank');
		var win = window.open( $(this).attr('href'), 'Share', 'width=600,height=400' );
		if( !win ) return true; else return false;
	});
	
	// Twitter sharing opens in new window
	$("A.share.twitter").live('click', function() {
		$(this).attr('target', '_blank');
	});
	
	
	
	//
	// Comments
	//
	
	
	
	// Update Gravatar as comment is being filled out
	$("#commentform #email").live('change', function() {
		
		// Remove old previews
		$(".tmp-gravatar-preview").remove();
		
		$.post('/src/ajax/blog.ajax.php', {
			
			cmd: 'get_gravatar_url',
			email: $(this).val()
			
		}, function(r) {
			
			if( r.status == 'success' ) {
				// Append new preview
				$("#reply-title").append('<img src="' + r.src + '" alt="Gravatar" class="tmp-gravatar-preview" />');
			}
			
		}, 'json');		
		
	});
	
	
	// Basic comment validation
	$("#commentform").live('submit', function() {
		
		$(".comment-error").remove();

		if( $("#comment").val().length == 0 || $("#author").val().length == 0 || $("#email").val().length == 0 ) {
			$("#commentform .comment-form-comment").after('<p class="comment-error" style="display: none;">* Please fill out all required fields</p>');
			$(".comment-error").fadeIn();
			return false;
		}
		
	});
	
	
	// Remove comment error on click
	$(".comment-error").live('click', function() {
		$(this).fadeOut( function() {
			$(this).remove();
		});
	});
	
	
	// Append a required asterisk on the comment field
	$("#commentform LABEL[for=comment]").after(' <span class="required">*</span>');
	
	
	// Facebook-style comment starter
	var hiddenCommentFields = $("#commentform").find(".comment-form-author, .comment-form-email, .comment-form-url, .comment-form-comment, .form-submit");
	$(hiddenCommentFields).hide();
	$("#commentform").append('<p class="click-to-comment"><input type="text" size="40" value="Write a comment..." class="text-field watermark" /></p>');
	$(".click-to-comment INPUT").live('focus', function() {
		$(".click-to-comment").remove();
		$(hiddenCommentFields).show();
		
		// Focus on the appropriate field
		if( $("#author").size() > 0 ) {
			$("#author").focus();
		} else {
			$("#comment").focus();
		}
		
		$('html, body').animate({
		    scrollTop: $("#respond").offset().top
		}, 1000);		
		
		return false;
	});
	
	
		
});