//
// jquery版 コメントAjaxモジュール
//
function commentReset(){
//	$('comment-author').val('');
//	$('comment-email').val('');
	$('#comment-url').val('');
	$('#comment-delete-key').val('');
	$('#comment-text').val('');
//	$('comment-captcha-input').val('');
	$('#comments-error').html('');

}

function commentDelete(){
	var blog_id  = $('#blog_id').val();
	var diary_id = $('#diary_id').val();
	var accept_comment = $('#accept_comment').val();
	var del_id = $('#delete-id').val();
	var del_key = $('#delete-key').val();
	var myajax = $.ajax({
		url:"xmlRequest.php",
		type:'post',
		data:{
			'mode':'delete',
			'blog_id':blog_id,
			'diary_id':diary_id,
			'accept_comment':accept_comment,
			'del_id':del_id,
			'del_key':del_key },
		success:
			commentRead
	});
}

function commentWrite(){
	var blog_id  = $('#blog_id').val();
	var diary_id = $('#diary_id').val();
	var accept_comment = $('#accept_comment').val();
	var name    = $('#comment-author').val();
	var email   = $('#comment-email').val();
	var url     = $('#comment-url').val();
	var text    = $('#comment-text').val();
	var del_key = $('#comment-delete-key').val();
	var captcha_input = $('#comment-captcha-input').val();
	var captcha_key = $('#comment-captcha-key').val();
	var myajax = $.ajax({
		url: "xmlRequest.php",
		data: {
			'mode':'save',
			'blog_id':blog_id,
			'diary_id':diary_id,
			'accept_comment':accept_comment,
			'name':name,
			'email':email,
			'url':url,
			'text':text,
			'del_key':del_key,
			'captcha_input':captcha_input,
			'captcha_key':captcha_key },
		type:'post',
		beforeSend:
			function(){
				$('#comments-status').html('');
				$('#comments-status').html('送信中');
			},
		error:
			function(){
				$('#comments-status').html('');
				$('#comments-error').html('送信エラーが発生しました。');
			},
		success:
			writeResult
	});
}

function commentRead(){
if($("#blog_id")){
	var blog_id = $("#blog_id").val();
	var diary_id = $('#diary_id').val();
	var comment_page = 1;
	var myajax = $.ajax({
		url:"xmlRequest.php",
		type:'post',
		data: {
			'mode':'load',
			'blog_id':blog_id,
			'diary_id':diary_id,
			'page':comment_page
			}, 
		success:showResult
	});
}
}

function commentBack(){
	var blog_id = $("#blog_id").val();
	var diary_id = $('#diary_id').val();
	var comment_page = $('#com-nowpage').val();
	if (comment_page >1)  comment_page --;

	var myajax = $.ajax({
		url:"xmlRequest.php",
		type:'post',
		data:{
			'mode':'update',
			'blog_id':blog_id,
			'diary_id':diary_id,
			'page':comment_page
			},
		beforeSend:
			function(){
				showStatus("読込み中")
			},
		error:
			function(){
				showStatus("受信エラーが発生しました")
			},
		success:
			showResult
	});
}

function commentNext(){
	var blog_id = $('#blog_id').val();
	var diary_id = $('#diary_id').val();
	var comment_page = $('#com-nowpage').val();
	comment_page ++;
	
	var myajax = $.ajax({
		url:"xmlRequest.php",
		type:'post',
		data:{
			'mode':'update', 
			'blog_id':blog_id,
			'diary_id':diary_id,
			'page':comment_page
			}, 
		beforeSend:
			function(){
				showStatus("読込み中")
			},
		error:
			function(){
				showStatus("受信エラーが発生しました")
			},
		success:
			showResult
	});
}

function writeResult(request){
//	res = request.responseText.split(";");
	res = request.split(";");

	$('#comments-status').html('');

	if(res[0] == 0 || res[0] == 3) {
//		$('#comment-author').val('');
//		$('#comment-email').val('');
//		$('#comment-url').val('');
//		$('#comment-text').html('');
		$('#comment-text').val('');
	}
	if(res[0] == 0)
		commentRead();
	else
		$('#comments-error').html(code_convert(res[1]));
}

function showResult(request){
	$('#comments-content').html(code_convert(request));
	$('#comments-error').html('');
	if($('#comments-loading1').length)
				$('#comments-loading1').html('');
	if($('#comments-loading2').length)
				$('#comments-loading2').html('');
//	if($('#comment-back-up').length)
		$('#comment-back-up').click(function(){commentBack()});
//	if($('#comment-next-up').length)
		$('#comment-next-up').click(function(){commentNext()});
//	if($('#comment-back-dn').length)
		$('#comment-back-dn').click(function(){commentBack()});
//	if($('#comment-next-dn').length)
		$('#comment-next-dn').click(function(){commentNext()});
}

function code_convert( text ) {
	if ( navigator.appVersion.indexOf( "KHTML" ) > -1 ) {
		var esc = escape( text );
		if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
			text = decodeURIComponent( esc );
		}
	}
	return text;
}

function showStatus(request){
	if($('#comments-loading1'))
		$('#comments-loading1').html(request);
	if($('#comments-loading2'))
		$('#comments-loading2').html(request);
	$('#comments-error').html('');
}

window.onload = function(){
	commentRead();
	$('#comment-reset').click(function(){commentReset()});
	$('#comment-post').click(function(){commentWrite()});
	$('#comment-delete').click(function(){commentDelete()});
}

