//require prototype.js 1.6.0
//require scriptaculous 1.8.1

//Author: SAKAI Yugo

//update
//2008-04-25: first release
//2008-05-07: <p class="blog-comment-limit-message"> to <p class="entry-comment-limit-message">

var Comment = Class.create();
Comment.prototype = {
	initialize : function(_rootDir, _dir, _limit){
		this.rootDir  = _rootDir; // "/skyg"
		this.dir  = _dir; // "/col2/"
		this.limit = 0;
		if(_limit){
			_today = new Date();
			this.limit = _today - _limit * 24 * 60 * 60 * 1000;
		}
	},
	
	showComment : function(_ids){
		_ids.each(
			function(_id){
				
				//id = comment_2006_10_16_test_save_name
				var idElems = _id.split('_');
				var _comment = idElems.shift();
				var _YYYY = idElems.shift();
				var _MM = idElems.shift();
				var _DD = idElems.shift();
				var _saveName = idElems.join('_');
				if (_comment != 'comment') return;
				var commentFilePath = this.rootDir + this.dir + '__comment/' + _YYYY + '/' + _MM + '/' + _DD + '_' + _saveName + '.txt?';
				
				//form
				var _commentForm = '<form onSubmit="return false;">名前: <input type="hidden" name="commentId" value="' + _id + '">\n<input type="hidden" name="mode" value="blog_comment">\n<input type="hidden" name="dir" value="' + this.dir + '">\n<input type="text" name="commentName" value="" style="width:150px;">\n<input type="button" name="submitButton" value="書き込み" onClick="com.check_and_submit(this.form, \'' + _id + '\');">\n<br /><textarea name="commentText" style="width:100%;height:5em;"></textarea>\n<br /><span class="sub">※改行は削除されます。</span></form>';
				
				//limit
				if(this.limit == 0){
					var _vDate = new Date(_YYYY, _MM - 1, _DD);
					if(_vDate * 1 < this.limit){
						_commentForm = '<p class="entry-comment-limit-message">コメントの受付は終了しました。</p>';
					}
				}
				
				var myAjax = new Ajax.Request(
					commentFilePath, {
						method: 'get',
						parameters: '',
						
						//onFailure
						onFailure: function(){
							var _commentText = '<ol id="ol_' + _YYYY + '_' + _MM + '_' + _DD + '_' + _saveName + '"></ol>';
							new Insertion.Bottom(_id, _commentText);
							new Insertion.Bottom(_id, _commentForm);
						}.bind(this),
						
						//onSuccess
						onSuccess: function(_req){
							var _commentText = '<ol id="ol_' + _YYYY + '_' + _MM + '_' + _DD + '_' + _saveName + '">';
							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(_id, _commentText);
							new Insertion.Bottom(_id, _commentForm);
						}.bind(this)
						
					}
				);//new Agax.Request
				
			}.bind(this) //function(_id)
			
		); //each
		
	},
	
	check_and_submit : function(_form, _id){
		//alert(_id);
		if(_form.commentName.value && _form.commentText.value){
			var originalColor = _form.submitButton.style.backgroundColor;
			_form.submitButton.value = '書き込み中...';
			_form.submitButton.style.backgroundColor = "#FF0000";
			var myAjax = new Ajax.Request(
				this.rootDir + '/index.cgi', {
					method: 'get',
					parameters: Form.serialize(_form),
					
					//onFailure
					onFailure: function(){
						_form.submitButton.value = '書き込み';
						_form.submitButton.style.backgroundColor = originalColor;
						alert('書き込みに失敗しました。');
					},
					
					//onSuccess
					onSuccess: function(_req){
						if(_req.responseText == 'OK'){
							var idElems = _id.split('_');
							var _comment = idElems.shift();
							var _YYYY = idElems.shift();
							var _MM = idElems.shift();
							var _DD = idElems.shift();
							var _saveName = idElems.join('_');
							new Insertion.Bottom('ol_' + _YYYY + '_' + _MM + '_' + _DD + '_' + _saveName, '<li>' + _form.commentText.value + '【' + _form.commentName.value + '】<span class="sub">NOW!</span></li>');
							$(_id).visualEffect('highlight', {duration:1.0});
							_form.commentText.value = '';
							_form.commentName.value = '';
							_form.submitButton.value = '書き込み';
							_form.submitButton.style.backgroundColor = originalColor;
						}else{
							_form.submitButton.value = '書き込み';
							_form.submitButton.style.backgroundColor = originalColor;
							alert('書き込みに失敗しました。' + _req.responseText);
						}
					}
				}
			); //myAjax
		}else{
			alert('名前とコメントは必須です。');
		}
	}
	
};

