var Comment = Class.create();
Comment.prototype = {
	initialize : function(_name){
		this.name = _name;
		
	},
	
	showComment : function(){
		document.getElementsByClassName('comment').each(
			function(_node){
				//id = comment_2006_10_16_test
				var idElems = _node.id.split('_');
				var commentFilePath = '/skyg/col2/archives/comment' + idElems[1] + '/' + idElems[2] + '/' + idElems[3] + '_' + idElems[4] + '.txt?';
				//alert(commentFilePath);
				var myAjax = new Ajax.Request(
					commentFilePath, {
						method: 'get',
						parameters: '',
						onFailure: function(){
							var _commentText = '<ol id="ol_' + idElems[1] + '_' + idElems[2] + '_' + idElems[3] + '_' + idElems[4] + '">';
							_commentText += '</ol>';
							new Insertion.Bottom(_node, _commentText);
							//form
							if(!location.href.match(/adsfaadfasdfasdf/)){
								var formId = 'form_' + idElems[1] + '_' + idElems[2] + '_' + idElems[3] + '_' + idElems[4];
								new Insertion.Bottom(_node, '<form onSubmit="return false;">名前: <input type="hidden" name="id" value="' + _node.id + '"><input type="text" name="name" value="" style="width:150px;"><input type="button" value="書き込み" onClick="com.check_and_submit(this.form, \'' + _node.id + '\');"><br /><textarea name="comment" style="width:100%;height:5em;"></textarea><br /><span class="sub">※改行は削除されます。</span></form>');
							}
						},
						onSuccess: function(_req){
							var _commentText = '<ol id="ol_' + idElems[1] + '_' + idElems[2] + '_' + idElems[3] + '_' + idElems[4] + '">';
							if (_req.responseText){
								var _comments = _req.responseText;
								if(navigator.appVersion.indexOf('KHTML') > -1){
									var esc = escape(_comments);
									_comments = (esc.indexOf("%u") < 0 && esc.indexOf("%") > -1) ? decodeURIComponent(esc) : _comments;
								}
								_comments.split(/\n/).each(
									function(_comment){
										if (_comment){
											_commentElem = _comment.split(/\t/);
											_commentText +=  '<li>' + _commentElem[2] + '【' + _commentElem[1] + '】<span class="sub">' + _commentElem[0] + '</span></li>';
										}
									}
								);
							}
							_commentText += '</ol>';
							new Insertion.Bottom(_node, _commentText);
							//form
							if(!location.href.match(/dasdgasdgasdgfasd/)){
								var formId = 'form_' + idElems[1] + '_' + idElems[2] + '_' + idElems[3] + '_' + idElems[4];
								new Insertion.Bottom(_node, '<form onSubmit="return false;">名前: <input type="hidden" name="id" value="' + _node.id + '"><input type="text" name="name" value="" style="width:150px;"><input type="button" value="書き込み" onClick="com.check_and_submit(this.form, \'' + _node.id + '\');"><br /><textarea name="comment" style="width:100%;height:5em;"></textarea><br /><span class="sub">※改行は削除されます。</span></form>');
							}
						}
					}
				);
			}
		);
		
	},
	
	check_and_submit : function(_form, _nodeId){
		//alert(_nodeId);
		if(_form.name.value && _form.comment.value){
			var myAjax = new Ajax.Request(
				'/skyg/col2/comment.cgi', {
					method: 'get',
					parameters: Form.serialize(_form),
					onFailure: function(){
						alert('書き込みに失敗しました。');
					},
					onSuccess: function(_req){
						if(_req.responseText == 'OK'){
							var idElems = _nodeId.split('_');
							new Insertion.Bottom('ol_' + idElems[1] + '_' + idElems[2] + '_' + idElems[3] + '_' + idElems[4], '<li>' + _form.comment.value + '【' + _form.name.value + '】<span class="sub">NOW!</span></li>');
							$(_nodeId).visualEffect('highlight',{duration:1.0});
							_form.comment.value = '';
							_form.name.value = '';
						}else{
							alert('書き込みに失敗しました。');
						}
					}
				}
			);
		}else{
			alert('名前とコメントは必須です。');
		}
	}
	
};

