/**
 * Copyright 2007 SK Communications. All rights reserved
 * @since 07.09.16
 * @author okjungsoo
 * 
 *  이오공감 신고 다이얼로그를 정의합니다. 
 */
Modal.ReportDlg = Class.create();
Object.extend(Modal.ReportDlg.prototype, {
	isLoaded: false,
	isFocused: false,
	
	// 기존의 값이 1부터 시작해서 1,2,3,4,6,9를 사용하여 빈 곳은 ''로 처리 
	reportReason: ['', '음란', '광고/상업성', '욕설', '개인정보유출', '', '명예훼손/비방', '', '', '직접입력'],
	
	initialize: function(_dialogID) {
		this.dialogID = _dialogID;
		this.dialog = $(this.dialogID);

		var bodyDiv = this.dialog.getElementsByClassName("popup_body")[0];
		this.radioButtons = $A(bodyDiv.getElementsByTagName("input")); 
		this.message = this.dialog.getElementsByClassName('popup_message')[0];
		
		this.postIndexElm = $('report_post_index');
		this.permaLinkElm = $('report_permalink');
		this.postTitleElm = $('report_post_title');
		
		this.isLoaded = Modal.addDialog(this);
	},
	
	open: function(_params) {
		Page.EoFeeling.cancelUpdate(); 
		this.dialog.setStyle({ 'zIndex': 9901, 'top': _params.top, 'left': _params.left});
		
		this.initForm(_params);
		this.dialog.show();
	},
	
	close: function() {
		this.dialog.hide();
	},
	
	initForm: function(_params) {
		this.postIndexElm.value = _params.postIndex;
		this.permaLinkElm.value = _params.permaLink;
		
		this.postTitleElm.href = _params.permaLink;
		this.postTitleElm.update(_params.postTitle);	
		
		this.radioButtons.each(function(button){
			if(button.checked) {
				button.checked = false;	
			}
			if(button.type == 'text') {
				button.value = '';
			}
		});
	},
	
	reportPost: function() {
		if(!this.isLoaded) { return;}
		
		var selected = null;
		for(var i=0; i<this.radioButtons.length; i++) {
			if(this.radioButtons[i].checked == true){
				selected = this.radioButtons[i]; 
				break;
			}
		}
		
		if(selected == null) {
			alert('신고사유를 선택해 주세요.');
			return false;
		}
		
		var reason_opt = parseInt(selected.value);
		if(reason_opt == 9) {
			var textInput = Element.next(selected.parentNode, 'input');
			reason = textInput.value.stripScripts().stripTags();
			
			if(reason.blank()) {
				alert('신고사유를 직접 입력해주세요.');
				textInput.focus();
				return false;
			}else if(reason.length > 200) {
				alert('200자 이내로 입력해주세요.');
				textInput.focus();
				return false;
			}
		}else {
			reason = this.reportReason[reason_opt];
		}
		
		var postIndex = this.postIndexElm.value;
		var permaLink = this.permaLinkElm.value;
		
		new Ajax.Request('/exec/eofeeling/accuse_post_exec.php', {
			parameters : $H({'pidx':postIndex, 'permalink':permaLink, 'reason':reason, 'reason_opt':reason_opt}),
			onSuccess: function(response) {
				var xmlDoc = response.responseXML;
				var resultTag = xmlDoc.getElementsByTagName('result')[0];
				var messageTag = xmlDoc.getElementsByTagName('message')[0];
				
				var message = messageTag.childNodes[0].nodeValue.replace(/\\n/g,"\n");
				alert(message);
				this.close();
				
				if(result.childNodes[0].nodeValue == '0') {
					return false;
				}else {
					if(typeof(Page) != 'undefined' && Page.refresh) {
						Page.refresh(); 
					}
					return true;
				}
			}.bind(this),
			onFailure: function() {
				alert('이오공감 사용이 원할하지 않습니다. 잠시 후 이용해 주시기 바랍니다.');
			}
		});
	}, 
		
	showErrorMsg: function(content) {
		this.message.innerHTML = content;
		return false;
	}
});