/* 글쓰기 란에 있는 태그 */
var special = /[\s\?\.\/\\\+\^\*\|\[\]\{\}!@#$%&()`\'\"<>,:;=_]/g;
var addWritePostTag = function() {
	var tagInput = document.getElementById('egbAddTag');
	var target = tagInput.value.replace(special, '');
	if(!target) alert('태그를 입력해주시기 바랍니다.');

	var tag = document.getElementById('egbTag');
	tag_arr = tag.value.split(' ');
	tag_arr[tag_arr.length] = target;
	tag.value = tag_arr.join(' ');
	listWritePostTag();

	tagInput.value = '';
	tagInput.focus();
};
var delWritePostTag = function(value) {
	var tag = document.getElementById('egbTag'), result = new Array();
	tag_arr = tag.value.split(' ');
	for(var i=0; i<tag_arr.length; ++i) {
		if(tag_arr[i] != value) result.push(tag_arr[i]);
	}
	tag.value = result.join(' ');

	listWritePostTag();
};
var listWritePostTag = function() {
	var list = document.getElementById('egbTagList'), tag = document.getElementById('egbTag');
	tag_arr = tag.value.split(' ');
	var content = '';
	for(var i=0; i<tag_arr.length; ++i) {
		content += '<a href="javascript:delWritePostTag(\''+tag_arr[i]+'\')">'+tag_arr[i]+'</a> ';
	}
	list.innerHTML = content;
};

/* 내용 안에 있는 태그 */
var restorePostTag = function(po_idx) {
	$('#postTag_' + po_idx).load(root_path + '/ajax/restorePostTag.php', {table: table, po_idx: po_idx});
};
var showEditPostTag = function(po_idx) {
	$('#postTag_' + po_idx).load(root_path + '/ajax/showEditPostTag.php', {table: table, po_idx: po_idx});
};

var addPostTag = function(po_idx) {
	if(po_idx < 1) { addWritePostTag(); return; }

	var input = document.getElementById('tagAddBox_'+po_idx);
	if(!input) return;
	if(!input.value) { alert('등록하실 태그를 입력해주세요'); input.focus(); return; }

	$.post(root_path + '/ajax/addPostTag.php', {table: table, po_idx: po_idx, tg_name: input.value}, addPostTagCheck, 'json');
};
var addPostTagCheck = function(res) {
	if(res.error) alert(res.error);
	else showEditPostTag(res.po_idx);
};
var lockPostTag = function(tg_idx, locked) {
	$.post(root_path + '/ajax/lockPostTag.php', {table: table, tg_idx: tg_idx, locked: locked}, removePostTagCheck, 'json');
};
var removePostTag = function(tg_idx) {
	if(!confirm('정말로 해당 태그를 지우시겠습니까?')) return;
	$.post(root_path + '/ajax/removePostTag.php', {table: table, tg_idx: tg_idx}, removePostTagCheck, 'json');
};
var removePostTagCheck = function(res) {
	if(res.error) alert(res.error);
	else {
		showEditPostTag(res.po_idx);
		if(res.message) MoePopup.show(res.message);
	}
};

/* 키입력 체크 */
var keyPostTag = function(e, po_idx) {
	var keyCode = (e.keyCode) ? e.keyCode : e.charCode;
	if(keyCode == 13 || keyCode == 188 || keyCode == 190 || keyCode == 32) addPostTag(po_idx);
	else if(keyCode == 27) document.getElementById('tagAddBox_' + po_idx).value = '';
};
var disabledEntry = function(e) {
	var keyCode = (e.keyCode) ? e.keyCode : e.charCode;
	if(keyCode == 13) return false;
};


/* 태그 자동 완성 */
var _TagAutoComplete = function() {
	this.po_idx = null;
	this.ul = null;
	this.timer = null;
	this.content = '';

	this.init();
};
_TagAutoComplete.prototype.init = function() {
	this.ul = document.createElement('ul');
	this.ul.className = 'tag_auto_complete';
};
_TagAutoComplete.prototype.start = function(input, po_idx) {
	var self = this, pos = $(input).position();
	this.po_idx = po_idx;
	this.ul.style.left = pos.left + 'px';
	this.ul.style.top = (pos.top+20) + 'px';

	document.body.appendChild(this.ul);
	this.timer = setInterval(function() {self.check();}, 500);
};
_TagAutoComplete.prototype.end = function() {
	if(this.po_idx == null) return;
	clearInterval(this.timer);
	document.body.removeChild(this.ul);
	this.content = null;
	this.timer = null;
	this.po_idx = null;
}
_TagAutoComplete.prototype.check = function() {
	var input = (this.po_idx > 0) ? document.getElementById('tagAddBox_' + this.po_idx) : document.getElementById('egbAddTag');
	if(!input) return;
	if(!input.value) { this.content = null; this.ul.innerHTML = ''; return; }
	if(this.content == input.value) return;
	this.content = input.value;
	$(this.ul).load(root_path + '/ajax/autoCompletePostTag.php', {table: table, po_idx: this.po_idx, td_name: input.value});
};
var putPostTag = function(po_idx, tg_name) {
	var input = (po_idx > 0) ? document.getElementById('tagAddBox_' + po_idx) : document.getElementById('egbAddTag');
	if(!input) return;
	input.value = tg_name;
	addPostTag(po_idx);
	input.blur();
};

var TagAutoComplete = new _TagAutoComplete();