(function($) {

$(document).ready(function() {

	$('div#comment_header a').bind('click', viewComments);
	
	$('form#commentform input.required').bind('blur',validateComment);
	$('form#commentform textarea.required').bind('blur', validateComment);
	$('form#commentform').bind('submit', validateAllComments);
		
});

//-- Comments functions ---------------------------------------------//

function viewComments(e) {
	if($(this).hasClass('hide_comments')) {
		$('div#comment_content').show('fast');
		$('a.hide_comments').removeClass('hide_comments').html('<a href="#">[ - ] Comments</a>');
	} else {
		$('div#comment_content').hide('fast');
		$(this).addClass('hide_comments').html('<a href="#">[ + ] Comments</a>');
	}
	return false;
}

function validateComment(e) {
	if( $(this).val() == "" ) {
		shake($(this));
	}
}

function validateAllComments(e) {
	var valid = true;
	if($('form#commentform input#author').val() == "" ) {
		shake($('form#commentform input#author')); valid = false; }
		
	if($('form#commentform input#email').val() == "" ) { 
		shake($('form#commentform input#email')); valid = false; }
		
	if($('form#commentform textarea#comment').val() == "" ) {
		shake($('form#commentform textarea#comment')); valid = false; }
		
	return valid;
}

function shake(e) {
	$(e).stop(true);
	var delay = Math.floor(Math.random()*101)
	$(e).animate({left: '-5px'}, 100 + delay)
		.animate({left: '5px'}, 100 + delay)
		.animate({left: '-10px'}, 100)
		.animate({left: '10px'}, 100)
		.animate({left: '-5px'}, 100)
		.animate({left: '5px'}, 100)
		.animate({left: '0px'}, 100);
}

})(jQuery);