var idgComment;
document.observe('dom:loaded', function() {
	idgComment = new IdgComment();
});
var IdgComment = Class.create ({
	tail: null,
	removeTarget: null,
	initialize: function() {
		this.form = $('IdgCommentForm');
		this.form.observe('submit', this.ins.bind(this));
		// $('commentContent').observe('focus', this.isLogin.bind(this));
		$$('#IdgCommentList .btnRemove').invoke('observe','click', this.removeToggle.bind(this));
		this.tail = $('idgCommentTail').value;
		this.removeForm = $('IdgCommentRemoveForm');
		this.removeForm.observe('submit',this.remove.bind(this));
		if ( $('realNameForm') ) {
			$('realNameForm').observe('submit',this.authRealName.bind(this));
		} else {
			$('passwordForm').show();
		}
	},
	authRealName: function( ev ) {
		ev.stop();
		if ( $F('name').blank() ) {
			alert('ÀÌ¸§À» ½á ÁÖ¼¼¿ä.');
			return;
		}
		if ( $F('jumin1').blank() ) {
			alert('ÁÖ¹Î¹øÈ£¸¦ ½á ÁÖ¼¼¿ä.');
			return;
		}
		if ( $F('jumin2').blank() ) {
			alert('ÁÖ¹Î¹øÈ£¸¦ ½á ÁÖ¼¼¿ä.');
			return;
		}
		$('realNameForm').submit();
		$('realNameForm').disable();
	},
	authResult: function(result) {
       switch(result){
        case '1':
			$('realNameForm').remove();
			$('passwordForm').show();
			return ;
			break;
		case '2':   
			alert("ÀÔ·ÂÇÏ½Å ¼ºÇÔ°ú ÁÖ¹Î¹øÈ£°¡ ÀÏÄ¡ÇÏÁö ¾Ê½À´Ï´Ù.");
			break;
		case '3':
			alert("ÀÔ·ÂÇÏ½Å ¼ºÇÔ°ú ÁÖ¹Î¹øÈ£°¡ ÀÏÄ¡ÇÏÁö ¾Ê½À´Ï´Ù.");
			break;
		case '4':
			alert("ÁÖ¹Îµî·Ï¹øÈ£¸¦ È®ÀÎÇÏ½ÅÈÄ ´Ù½Ã ÀÔ·ÂÇÏ¼¼¿ä.");
			break;
		case '5':
			alert("ÁÖ¹Îµî·Ï¹øÈ£¸¦ È®ÀÎÇÏ½ÅÈÄ ´Ù½Ã ÀÔ·ÂÇÏ¼¼¿ä.");
			break;
		case '50':
			alert("ÀÔ·ÂÇÏ½Å ÁÖ¹Îµî·Ï¹øÈ£´Â Á¤º¸µµ¿ëÂ÷´ÜÀÌ ½ÅÃ»µÇ¾î ÀÖ´Â ÁÖ¹Îµî·Ï ¹øÈ£ÀÔ´Ï´Ù."); 
		default:
			alert("ÀÎÁõ¿¡ ½ÇÆÐ ÇÏ¿´½À´Ï´Ù. ¸®ÅÏÄÚµå:["+result+"]");
			break;
	   }
		$('realNameForm').enable();
	},
	removeToggle: function( ev ) {
		ev.stop();
		if ( this.removeForm.style.display == 'none')
		{
			this.removeForm.show();
			var el = ev.element();
			var elPos = el.positionedOffset();
			var top = elPos.top;
			var left = elPos.left - 120;
			this.removeForm.setStyle({
				'top': top + 'px',
				'left': left + 'px'
			});
			$('removeTargetIdx').value = el.title;
			this.removeTarget = el.up('li');
		} else {
			this.removeForm.hide();
		}
	},
	remove: function( ev ) {
		ev.stop();
		var idx = $F('removeTargetIdx');
		var removePassword = $F('removePassword');
		new Ajax.Request('/main/IdgComment/del?idgCommentTail=section', {
			parameters: {
				'idx': idx,
				'pass': removePassword
			},
			onSuccess: this.onRemoved.bind(this)
		});
	},
	onRemoved: function( transport) {
		var text = transport.responseText;
		if ( text == '1' ) {
			this.removeTarget.remove();
			this.removeForm.hide();
		} else {
			alert('ºñ¹Ð¹øÈ£¸¦ È®ÀÎÇØ ÁÖ¼¼¿ä.');
		}
	},
	ins: function (ev) {
		ev.stop();
		if ( $F('pass').blank() ) {
			alert('ÆÐ½º¿öµå°¡ ºñ¾î ÀÖ½À´Ï´Ù.');
			return false;
		}
		if ( $F('commentContent').blank() ) {
			alert('³»¿ëÀÌ ºñ¾î ÀÖ½À´Ï´Ù.');
			return false;
		}
		new Ajax.Request('/main/IdgComment/ins?idgCommentTail=section', {
			parameters : {
				'relIdx': encodeURIComponent($F('relIdx')),
				'pass': encodeURIComponent($F('pass')),
				'content': encodeURIComponent($F('commentContent'))
			},
			onSuccess : this.insertRow.bind(this),
			onFailure : this.insFailure.bind(this)
		});
		return false;
	},
	insertRow: function(transport) {
		var text = transport.responseText;
		if ( text === 'Permission Denied' ) {
			alert ( '½Ç¸íÀÎÁõÀ» ÇÏ¼Å¾ß »ç¿ëÇÏ½Ç ¼ö ÀÖ½À´Ï´Ù.' );
			return false;
		}
		if ( $('IdgCommentList').down('ul') ) {
			$('IdgCommentList').down('ul').insert({'top':text});
			this.observeAfterInsertRow.bind(this)();
		}
	},
	observeAfterInsertRow: function() {
		$('IdgCommentList').down('ul .btnRemove').observe('click', this.removeToggle.bind(this));
	},
	insFailure: function() {
		alert('ÀÔ·Â¿¡ ½ÇÆÐÇÏ¿´½À´Ï´Ù.');
	}
});


