// コメントの投稿
var Comment = Class.create();
Comment.prototype = {
	initialize: function(
		inputbox_id,
		name_id,
		comment_id,
		name_confirm_id,
		comment_confirm_id,
		confirm_button_id,
		submib_button_id,
		cancel_button_id
	) {
		this.nameObj = $(name_id);
		this.commentObj = $(comment_id);
		this.nameConfirmObj = $(name_confirm_id);
		this.commentConfirmObj = $(comment_confirm_id);
		this.inputBoxObj = $(inputbox_id);
		this.confirmButtonObj = $(confirm_button_id);
		this.submitButtonObj = $(submib_button_id);
		this.cancelButtonObj = $(cancel_button_id);

		this.initForm();
	},
	initForm: function() {
		this.nameObj.value    = 'your name';
		this.commentObj.value = 'comment this';
		this.visibleB($('commentForm'));
		this.setInputMode();
		$('resultBox').innerHTML ='';
	},
	// confirm
	confirm: function() {
		this.nameConfirmObj.innerHTML    = this.nameObj.value;
		this.commentConfirmObj.innerHTML = this.commentObj.value;
		this.disable(this.nameObj);
		this.disable(this.commentObj);
		this.disable(this.confirmButtonObj);
		this.visibleB(this.nameConfirmObj);
		this.visibleB(this.commentConfirmObj);
		this.visibleI(this.submitButtonObj);
		this.visibleI(this.cancelButtonObj);

	},
	cancel: function() {
		this.setInputMode();
	},
	setInputMode: function(){
		this.visibleB(this.nameObj);
		this.visibleB(this.commentObj);
		this.visibleI(this.confirmButtonObj);

		this.disable(this.submitButtonObj);
		this.disable(this.cancelButtonObj);
		this.disable(this.nameConfirmObj);
		this.disable(this.commentConfirmObj);
	},
	// add comment
	add: function(){
		var url = './ajax/addComment.php';
		var pars = '';
		pars += 'name='    + encodeURI(this.nameConfirmObj.innerHTML);
		pars += '&comment=' + encodeURI(this.commentConfirmObj.innerHTML);
		pars += '&parent_id=' + this.comment_no;
		pars += '&parent_tab=0';
		var myAjax = new Ajax.Updater(
			{success: 'resultBox'}, 
			url, 
			{
				method: 'post', 
				parameters: pars, 
				onFailure: this.reportError,
				onComplete: this.postAdd,
				evalScripts: true
			});
		this.disable($('commentForm'));
	},
	postAdd: function(request){
	},
	reportError: function(request) {
		alert('ごめんなさい、書き込みできませんでした。2度やってだめなら今日は多分無理です。');
	},
	openWindow: function(evt){
		var child = Event.element(evt);
		this.initForm();
		if(child.id.indexOf('addComment') > -1){
			var object_name = child.id.split('.');
			this.comment_no = object_name[1];
			this.visibleB(this.inputBoxObj);
			this.inputBoxObj.style.left = Event.pointerX(evt) - 50  + 'px';
			this.inputBoxObj.style.top  = Event.pointerY(evt) - 10 + 'px';
		}
	},
	closeWindow: function(){ 
		this.disable(this.inputBoxObj); 
		this.disable(this.nameConfirmObj);
		this.disable(this.commentConfirmObj);
		this.disable(this.submitButtonObj);
		this.disable(this.cancelButtonObj);
		this.visibleI(this.confirmButtonObj);
		this.visibleB(this.nameObj);
		this.visibleB(this.commentObj);
	},
	disable: function(obj){ obj.style.display = 'none'; },
	visibleB: function(obj){ obj.style.display = 'block'; },
	visibleI: function(obj){ obj.style.display = 'inline'; }
};	

