var CommentJS = Class.create();
var MyComment;

CommentJS.prototype = {
	initialize: function() {
		this.check_page = "/member/member_check.php";
		this.values = {
			element_class: 'comment_write_s2',
			board_id: false, // board_id
			action: false, // open,close,edit,delete
			board_type: false, // remark,qna,....
			navi: false, // 01,03,...
			join_navi: false,
			category_id: false,
			goods_seq: false,
			type: false, // comment,hit
			comment_id: false, // comment id
			mynavi_type: '', // board,comment, mynavi only
			//id_add: false, //
			token: false,
			login: false
		};

		this.element = {
			button: false,
			comment: false,
			hit: false,
			comment_hit: false, // <div class='comment_write_s2'></div>
			form: false, // <form name='login_form'>
			notice: false
		};

		this.edit_element = {
			div: false, // <div id='comment_view_85655'>
			form: false, // <form name='comment_edit'>
			notice: false // <div title='notice'...>
		};

		this.button = {
			comment: false,
			hit: false
		};

		this.error = {
			msg: '',
			field: false
		};

		/*
		token 은 uniq_token_board_id 를 공통으로 사용
		insert,update,delete 후 갱신된 token 을 가져오므로, updateObjet()를 사용할 것
		*/
	},

	calRowIndex: function(element,num) {
		num = (num) ? num : 3;
		var temp = 'element';
		for(var i=0; i<num; i++) {
			temp += '.parentNode';

			var msg = i+':==>';
			msg += eval(temp+'.tagName');
			msg += ' '+eval(temp+'.className');

			alert(msg);
		}
	},

	updateObject: function(element,action,type,navi,join_navi,board_id,board_type,category_id,goods_seq,mynavi_type,comment_id) {
		if(element) {
			this.values.board_id = board_id;
			this.values.action = action;
			this.values.type = type;
			this.values.navi = navi;
			this.values.join_navi = join_navi;
			this.values.category_id = category_id;
			this.values.goods_seq = goods_seq;
			this.values.board_type = board_type;
			this.values.comment_id = (typeof comment_id != 'undefined' && comment_id != '') ? comment_id : '';
			//this.values.mynavi_type = (typeof mynavi_type != 'undefined' && mynavi_type != '') ? mynavi_type : '';
			//this.values.id_add = (this.values.mynavi_type != '') ? 'my'+mynavi_type+'_' : '';

			var id_layer = ($('simple_'+this.values.board_id)) ? 'simple_'+this.values.board_id : 'detail_'+this.values.board_id;
			this.element.comment_hit = $(id_layer).getElementsBySelector('[title="comment_hit"]')[0];
			this.element.button = $(id_layer).getElementsBySelector('[title="button"]')[0];
			this.element.hit = $(this.element.button).getElementsBySelector('[title="hit"]')[0];
			this.element.comment = $(this.element.button).getElementsBySelector('[title="comment"]')[0];
			this.values.token = this.getToken();
			this.values.login = this.checkLogin();

			if(this.values.action == 'open' || this.values.action == 'close') {
				if(this.values.action == 'open') this.getInnerHtml();
				this.controlButton();

//			} else if(this.values.action == 'illegal') {
//				this.requestDeletement();

			} else if(this.values.action == 'edit') {
				this.getInnerHtml();
				if($(this.edit_element.form).contents) $(this.edit_element.form).contents.focus();

			} else if(this.values.action == 'submit') {
				if(this.values.login != '1') return;
				this.edit_element.div = 'comment_view_'+this.values.comment_id;
				this.edit_element.form = $(this.edit_element.div).getElementsBySelector('[name="comment_edit"]')[0];
				this.edit_element.notice = $(this.edit_element.div).getElementsBySelector('[title="notice"]')[0];

				if(this.checkByte($(this.edit_element.form).contents,'4000','edit') != false ) { this.editComment(); }

			} else if(this.values.action == 'delete') {
				if(this.values.login != '1') return;
				this.deleteComment();
			}
		}
	},

	checkLogin: function() {
		var QueryStrings = '?search=login&val=';
		var ajax_index = NAjax.length;
		NAjax.push(new XmlParser(true));
		NAjax[ajax_index].ReadXMLPage(this.check_page+QueryStrings, 'row');
		var login = NAjax[ajax_index].GetPageValue(0,'');

		return login;
	},

	controlButton: function() { // ucc_comment of communi_js.php 
		var script = {
			hit: false,
			comment: false
		};

		var open = {
			hit: false,
			comment: false
		};

		var newClass = {
			hit: false,
			comment: false,
			button: false
		};

		if(this.values.action == 'open') { // open
			//this.element.notice = (this.values.mynavi_type != '') ? 'my'+this.values.mynavi_type+'_' : '';
			this.element.notice = '';
			this.element.notice += 'entry_notice2_'+this.values.board_id;

			var display = (this.values.type == 'hit') ? 'none' : '';
			var temp_arr = $(this.element.comment_hit).getElementsBySelector('[title="comment"]');
			for(var i=0; i<temp_arr.length; i++) {
				var ele = temp_arr[i];
				$(ele).style.display = display;
			}

			if(this.values.type == 'hit') {
				open.hit = 'close';
				open.comment = 'open';

				newClass.hit = 'uclist_r_cbtn2_close';
				newClass.comment = 'uclist_r_cbtn1';

				if(this.values.login == '0') {
					if($(this.element.form).userid) $(this.element.form).userid.focus();
				}

			} else {
				open.hit = 'open';
				open.comment = 'close';

				newClass.hit = 'uclist_r_cbtn2';
				newClass.comment = 'uclist_r_cbtn1_close';

				if(this.values.login == '0') {
					if($(this.element.form).userid) $(this.element.form).userid.focus();
					else if($(this.element.form).contents) $(this.element.form).contents.focus();

				} else {
					if($(this.element.form).contents) $(this.element.form).contents.focus();
				}
				$(this.element.form).contents.value = $(this.element.form).contents.value; // 포커스 위치 맨 마지막으로 보내기
			}
			newClass.button = 'bal_btn2';

		} else { // close
			open.hit = 'open';
			open.comment = 'open';

			newClass.hit = 'uclist_r_cbtn2';
			newClass.comment = 'uclist_r_cbtn1';
			newClass.button = 'bal_btn';

			$(this.element.comment_hit).style.display = 'none';
			$(this.element.comment_hit).innerHTML = '';
		}

		// setAttribute is not working under IE8
		$(this.element.hit).className = newClass.hit;
		$(this.element.comment).className = newClass.comment;
		$(this.element.button).className = newClass.button;

		script.hit = "MyComment.updateObject(this,'"+open.hit+"','hit','"+this.values.navi+"','"+this.values.join_navi+"','"+this.values.board_id+"','"+this.values.board_type+"','"+this.values.category_id+"','"+this.values.goods_seq+"','"+this.values.mynavi_type+"');";
		script.comment = "MyComment.updateObject(this,'"+open.comment+"','comment','"+this.values.navi+"','"+this.values.join_navi+"','"+this.values.board_id+"','"+this.values.board_type+"','"+this.values.category_id+"','"+this.values.goods_seq+"','"+this.values.mynavi_type+"');";

		$(this.element.hit).innerHTML = '<a style="cursor: pointer; cursor: hand;" onclick="'+script.hit+'">'+g_comment_string_hit+'</a>';
		$(this.element.comment).innerHTML = '<a style="cursor: pointer; cursor: hand;" onclick="'+script.comment+'">'+g_comment_string_close+'</a>';

		if(this.values.action == 'open' && this.values.type == 'comment' && this.values.comment_id != '') this.checkByte(this.element.form.contents,'4000');
	},

	getInnerHtml: function() {
		if(this.values.action == 'open') {
			var QueryString = '&type='+this.values.type+'&id='+this.values.board_id+'&open='+this.values.action;
			QueryString += '&navi='+this.values.navi+'&join_navi='+this.values.join_navi+'&board_type='+this.values.board_type+'&category_id='+this.values.category_id+'&goods_seq='+this.values.goods_seq+'&mynavi_type='+this.values.mynavi_type+'&comment_id='+this.values.comment_id;
			var ajax_index = NAjax.length;
			NAjax.push(new AjaxDiv('','','None',true));
			NAjax[ajax_index].setDataX('/common/ajax_page/Comment.php','',QueryString,'',$(this.element.comment_hit));
			$(this.element.comment_hit).style.display = '';

			this.element.form = $(this.element.comment_hit).getElementsBySelector('[name="login_form"]')[0];

		} else if(this.values.action == 'edit') {
			this.edit_element.div = 'comment_view_'+this.values.comment_id;
			var QueryString = '&mode=edit&navi='+this.values.navi+'&join_navi='+this.values.join_navi+'&id='+this.values.board_id+'&comment_id='+this.values.comment_id+'&board_type='+this.values.board_type+'&mynavi_type='+this.values.mynavi_type;
			var ajax_index = NAjax.length;
			NAjax.push(new AjaxDiv('','','None',true));
			NAjax[ajax_index].setDataX('/common/ajax_page/Comment.php', this.edit_element.div, QueryString);
			this.edit_element.form = $(this.edit_element.div).getElementsBySelector('[name="comment_edit"]')[0];
			this.edit_element.notice = $(this.edit_element.div).getElementsBySelector('[title="notice"]')[0];
		}

	},

	selectRegLogin: function(mode) { // login, register, reg_login_chk of communi_js.php
		var email_part = $(this.element.comment_hit).getElementsBySelector('[title="email part"]')[0];
		$(email_part).style.display = (mode == 'login') ? 'none' : '';
		var required = $(this.element.form).required;
		if($(required)) {
			$(required).value = $(required).value.replace(/,useremail/g, '');
			$(required).value += (mode == 'register') ? ',useremail' : '';
		}

		var indicator = '_check_indicator_'+this.values.board_id;
		var display = (mode == 'login') ? 'none' : '';

		var required = $(this.element.form).required;
		var required_arr = required.value.split(/,/);
		for(var i=0; i<required_arr.length; i++) {
			var ele = required_arr[i];
			if(ele == 'contents') continue;
			if($(ele+indicator)) $(ele+indicator).style.display = display;
			$(this.element.form)[ele].style.background = '#FFFFFF';
		}

		$(this.element.form).userid.focus();
	},

/*
	requestDeletement: function(gubun,b_id,b_type,navi,join_navi,page) {
		GetIndex(NAjax);
		NAjax[ajax_index] = new XmlParser();

		if(confirm('削除依頼しますか?')==true) {
			if(b_id && b_id.search(/^[\d]+$/) !== -1) {
				NAjax[ajax_index].callfunction = function() {
					var ReturnMsg = NAjax[ajax_index].GetPageValue(0, '');
					if(ReturnMsg == "login_do") {
						alert(NAjax[ajax_index].GetPageValue(1, ''));
						if(page) document.location.href = '<?=Environment::$LOGIN_URL;?>?fwd='+encodeURIComponent(page);

					} else {
						alert(ReturnMsg);
					}

				}

				NAjax[ajax_index].ReadXMLPage("/common/ajax_page/UserRecomVote.php?type=ajax&Gubun="+gubun+"&Navi="+navi+"&JoinNavi="+join_navi+"&BoardType="+b_type+"&BoardId="+b_id, "row");
			}
		}
	},
*/

	editComment: function() { // comm_edit_ok of communi_js
		var form = $(this.edit_element.form);

		if(form) {
			//var token = form.token.value;
			var token = this.values.token;
			var navi = form.navi.value;
			var board_id = form.board_id.value;
			var comment_id = form.comment_id.value;
			var board_type = form.board_type.value;
			var contents = form.contents.value;
			//var mynavi_type = form.mynavi_type.value;
			var mynavi_type = '';

			if(this.nullCheckedMessage(form.contents,'edit')) {
				var QueryString = '&mode=modify&type=&Navi='+navi+'&JoinNavi='+this.values.join_navi+'&BoardType='+board_type+'&BoardId='+board_id+'&CommentId='+comment_id+'&contents='+encodeURIComponent(contents)+'&token='+token+'&mynavi_type='+mynavi_type;
				var ajax_index = NAjax.length;
				NAjax.push(new AjaxDiv('','','None',true));
				NAjax[ajax_index].setDataX('/common/ajax_page/Communi_Comment.php','',QueryString,'',$('comment_'+this.values.board_id));
				this.values.token = this.getToken();
			}
		}
	},

	deleteComment: function() { // comm_del of communi_js
		if(confirm(g_comment_string_do_you_want_to_delete)==true) {
			var token = this.values.token;
			var QueryString = '&mode=del&type=&Navi='+this.values.navi+'&JoinNavi='+this.values.join_navi+'&BoardType='+this.values.board_type+'&BoardId='+this.values.board_id+'&CommentId='+this.values.comment_id+'&token='+token+'&mynavi_type='+this.values.mynavi_type;
			var ajax_index = NAjax.length;
			NAjax.push(new AjaxDiv('','','None',true));
			NAjax[ajax_index].setDataX('/common/ajax_page/Communi_Comment.php','',QueryString,'',$('comment_'+this.values.board_id));
			this.values.token = this.getToken();
		}
	},

	getToken: function() {
		var token = $('uniq_token_'+this.values.board_id).value;
		return token;
	},

	checkByte: function(element,fsize,action) { // byte_chk of communi_js.php 
		var obj = $(element);

		var str = obj.value;
		var tmp=0;
		var tmp_onechar;
		var tmp_count = 0;

		var tmp_str = new String(str);
		tmp = tmp_str.length;
	 
		for (k=0;k<tmp;k++){
			tmp_onechar = tmp_str.charAt(k);

			if (escape(tmp_onechar).length > 4) {
				tmp_count += 2;

			} else if (tmp_onechar!='\\r') {
				tmp_count++;
			}
		}

		var contents_cnt = $(element.form).contents_cnt;
		$(contents_cnt).value = tmp_count;
		notice = (action == 'edit') ? this.edit_element.notice : this.element.notice;

		if(tmp_count>fsize) {
			//this.displayNotice(g_comment_string_over_the_maximum_string,notice);

			this.error.msg = g_comment_string_over_the_maximum_string;
			//$(element).style.background = '#FFDDEB';
			//$(element).focus();
			this.errorText(obj);
			return false;

		} else {
			this.clearText(obj, '');
			$(notice).style.display = 'none';
		}
	},

	errorText: function(element,msg,now,focus) {
		focus = (typeof focus != 'undefined' && focus != '') ? true : false;

		if(now) {
			alert(msg);
			element.style.background = '#FFDDEB';
			if(focus) element.focus();

		} else {
			if(msg) this.error.msg += msg + '\n';
			if(! this.error.field) this.error.field = element;
			element.style.background = '#FFDDEB';
			if(focus) this.error.field.focus();

			notice = (this.values.action == 'edit') ? this.edit_element.notice : this.element.notice;
			this.displayNotice(this.error.msg,notice);
		}
	},

	clearText: function(element) {
		element.style.background = '#FFFFFF';
	},

	checkText: function(element,msg) {
		var isValid = false;

		if(element.value == '') {
			this.errorText(element,msg,'',true);
			isValid = false;

		} else {
			this.clearText(element);
			isValid = true;
		}

		return isValid;
	},

	nullCheckedMessage: function(element) {
		var msg = '';
		if(element.name.match(/userid/gi))		msg = g_comment_string_input_a_id;
		else if(element.name.match(/userpw/gi))		msg = g_comment_string_input_a_password;
		else if(element.name.match(/email/gi))		msg = g_comment_string_input_a_email;
		else if(element.name.match(/re_userpw/gi))	msg = g_comment_string_input_a_repassword;
		else if(element.name.match(/subject/gi))	msg = g_comment_string_input_a_title;
		else if(element.name.match(/content/gi))	msg = g_comment_string_input_a_contents;
		else if(element.name.match(/new_kind_id/gi))	msg = g_comment_string_select_a_kind;
		else if(element.name.match(/board_title/gi))	msg = g_comment_string_input_a_title;
		else if(element.name.match(/image_content'/gi))	msg = g_comment_string_input_a_contents;
		else if(element.name == 'image[]')		msg = g_comment_string_select_a_image;

		return this.checkText(element,msg);
	},

	displayNotice: function(msg,element) {
		element = (typeof element != 'undefined' && element != '') ? element : this.element.notice
		if($(element)) $(element).innerHTML = msg;
		if($(element)) $(element).style.display = '';
	}
}

document.observe('dom:loaded', function () { MyComment = new CommentJS(); });
