var Comment = {
   TT_FORM : '<input type="text" name="name" id="name-#{id}" size="7" /><br /><textarea name="comment" id="comment-#{id}" rows="3" cols="40" /></textarea><br /><input type="button" value="コメントする" onclick="Comment.submit(#{id});" /> <input type="button" value="キャンセル" onclick="Comment.cancel(#{id});" />',
   TT_COMMENT : '<li class="log-comment"><span class="black">#{name} / #{ctime}</span><br /><p>#{data}</p></li>',

   init : function() {
       $('.comments').each(
	   function() {
	       var id = $(this).attr('id').substr('comments-'.length);
	       $(this).prepend('<div class="comment-cont" id="comment-cont-'+id+'">'
			      + '<a href="javascript:Comment.form('+id+')">comment</a></div>');
	   });
   },
   form : function(id) {
       $('#comment-cont-'+id).html('<input type="text" id="input-name-'+id+'" /><br /><textarea class="t-comment" id="input-comment-'+id+'" rows="3" /></textarea><br /><input type="button" value="comment" onclick="Comment.submit('+id+');" /> <input type="button" value="cancel" onclick="Comment.cancel('+id+');" />');
   },
   cancel : function(id) {
       $('#comment-cont-'+id).html('<div class="comment-cont-" id="comment-cont-'+id+'">'
				   + '<a href="javascript:Comment.form('+id+')">comment</a></div>');
   },
   submit : function(id) {
       Dialog.show('please wait...');
	$.ajax({type:'post',
		url:'/diary/op/comment?'+new Date().getTime(),
		data:$.param({id:id,
			      name:$('#input-name-'+id).val(),
			      data:$('#input-comment-'+id).val()}),
		success:function(ret) {
		    if(ret.err) {
			Dialog.close();
			alert(ret.errstr);
			return;
		    }
		    // add comment
		    $('#comments-'+id).append('<li class="comment">'+ret.name+' / '+ret.ctime+'<br /><p>'+ret.data+'</p></li>');
		    Comment.cancel(id);
		    Dialog.close();
		}
	       });
   }
};
$(document).ready(Comment.init);

