$(document).ready(function(){
    var options = {
		beforeSubmit:validateComment, 
		success:handleCommentPosted,
		url:'/communities/comment',
		type:'post', 
		clearForm: true,
		resetForm: true
    };
	$('.ajaxForm').ajaxForm(options);
	
	$('.commentToggleBtn').toggle(
		function(){ $(this).children('img:first-child').attr('src','/beauty/img/hn_comment_open_01.png') },
		function(){ $(this).children('img:first-child').attr('src','/beauty/img/hn_comment_close_01.png') }
	)
	$('.commentToggleBtn2').toggle(
		function(){ $(this).children('img:first-child').attr('src','/beauty/img/hn_comment_open_02.png') },
		function(){ $(this).children('img:first-child').attr('src','/beauty/img/hn_comment_close_02.png') }
	)
});

var lastCommentForm;
function validateComment(formData, jqForm, options){
	lastCommentForm = jqForm;
	lastCommentForm.find(':input').attr('disabled','disabled');
	lastCommentForm.find(':input').attr('src','/beauty/img/btn_post_01.png');
	var check = true;
	for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('応援コメントを入力して下さい。');
			lastCommentForm.find(':input').removeAttr('disabled');
            check = false; 
        }
    }
	if(check){
		if(!window.confirm('以下の内容で送信しますか？\n\nコメント : '+formData[1].value)){
			lastCommentForm.find(':input').removeAttr('disabled');
			check = false;
		}
	}
	return check;
}

function handleCommentPosted(responseText, statusText){
	lastCommentForm.find(':input').removeAttr('disabled');
	lastCommentForm.parent().parent().prepend(responseText);
	lastCommentForm.parent().remove();
}
