/***********************************************

Yag : This is a program that follows GPL.
Copyright (C) 2006 Miengine Inc.  www.miengine.com,  www.yagne.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.



Yag : 이 프로그램은 GPL을 따르는 프로그램입니다.  
Copyright (C) 2006년 (주)마이엔진,  www.miengine.com, www.yagne.com
 
이 프로그램은 자유 소프트웨어입니다. 소프트웨어의 피양도자는 자유 소프트웨어 재단이 공표한 GNU 일반 공중 사용 허가서 2판 또는 그 이후 판을 임의로 선택해서, 그 규정에 따라 프로그램을 개작하거나 재배포할 수 있습니다. 

이 프로그램은 유용하게 사용될 수 있으리라는 희망에서 배포되고 있지만, 특정한 목적에 맞는 적합성 여부나 판매용으로 사용할 수 있으리라는 묵시적인 보증을 포함한 어떠한 형태의 보증도 제공하지 않습니다. 보다 자세한 사항에 대해서는 GNU 일반 공중 사용 허가서를 참고하시기 바랍니다. 

GNU 일반 공중 사용 허가서는 이 프로그램과 함께 제공됩니다. 만약, 이 문서가 누락되어 있다면 자유 소프트웨어 재단으로 문의하시기 바랍니다. (자유 소프트웨어 재단: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA) 

GPL에 대해서 더 자세한 사항은 www.gnu.org 를 참조하십시오.

***********************************************/

function UFN_ChatStart()
{
	UFN_ChatRequest(UFN_ChatShow);
}

function UFN_ChatStartByClick()
{
	if (YGV_ChatCnt == 0)
	{
		UFN_ChatStart();
	}
}

function UFN_ChatStartByCookie()
{
	if (YGV_ChatCnt == 0)
	{
		if (UFN_GetCookie(YGV_CKchatStart) == "Y")
		{
			UFN_ChatStart();
		}
	}
}

function UFN_ChatRequest(argFunction)
{
	UFN_CheckCookie();

	var mainCook	= UFN_GetCookie(YGV_CKmain);
	var subCook		= UFN_GetCookie(YGV_CKsub);
	var getPost		= "POST";
	var urlAppl		= YGV_PluginURL + "/chat_start.php";
	var trueFalse	= true;
	var sendData	= "";
	sendData += "OWNER=" + YGV_Owner + "&USER=" + YGV_User + "&CNT=" + YGV_ChatCnt + "&HSN=" + YGV_ChatHSN + "&MID=" + mainCook + "&SID=" + subCook;

	UFN_XmlHttpOpenSend(getPost, urlAppl, trueFalse, sendData, argFunction);
}

function UFN_ChatShow(argXmlHttp)
{
	try
	{
		var xmlout = argXmlHttp.responseXML;
		argXmlHttp = null;
		var display = xmlout.getElementsByTagName("display")[0].firstChild.nodeValue;

		if (display == "Y")
		{
			UFN_ChatDisplay();
			UFN_ChatFillupUser(xmlout);
			UFN_ChatFillupHistory(xmlout);

			YGV_ChatCnt	= 1;
			YGV_ChatJoinSelfAlert	= "";
			YGV_ChatJoinUserAlert	= "";
			YGV_ChatOutUserAlert	= "";
			UFN_SetCookie(YGV_CKchatStart, "Y", "/", "");

			YGV_ChatTimeId = setTimeout("UFN_ChatStart()", YGV_ChatInterval);
		}
		else if (display == "Overflow")
		{
			//alert("대화방 최대 접속자 수가 초과되었습니다.\n\n잠시 후 이용하시기 바랍니다.");
			UFN_ChatClose();
		}
		else if (display == "N")
		{
			//alert("Error : xml display N");
			UFN_ChatClose();
		}
		else
		{
			//alert("Error : xml display unknown");
			UFN_ChatClose();
		}
	}
	catch (e)
	{
		//alert("Error : javascript runtime error");
		UFN_ChatClose();
	}
}

function UFN_ChatDisplay()
{
	if (YGV_ChatCnt == 0)
	{
		UFN_GetObjById("div_yag_chat").style.display = "block";
		UFN_ChatTextareaFocus();
	}
}

function UFN_ChatCloseByClick()
{
	UFN_GetObjById("span_yag_chatcnt").innerHTML = parseInt(UFN_GetObjById("span_yag_chatcnt").innerHTML) - 1;
	UFN_ChatClose();
}

function UFN_ChatClose()
{
	clearTimeout(YGV_ChatTimeId);
	YGV_ChatCnt = 0;
	YGV_ChatHSN = 0;
	YGV_ChatJoinSelfAlert	= "";
	YGV_ChatJoinUserAlert	= "";
	YGV_ChatOutUserAlert	= "";
	UFN_SetCookie(YGV_CKchatStart, "N", "/", "");
	UFN_GetObjById("div_yag_chat_frame").innerHTML = "";
	UFN_GetObjById("div_yag_chat_user_guest").innerHTML = "";
	UFN_GetObjById("textarea_yag_chat_write").value = "";
	UFN_GetObjById("div_yag_chat").style.display = "none";

	UFN_CheckCookie();
	var mainCook	= UFN_GetCookie(YGV_CKmain);
	var subCook		= UFN_GetCookie(YGV_CKsub);
	var getPost		= "POST";
	var urlAppl		= YGV_PluginURL + "/chat_end.php";
	var trueFalse	= true;
	var sendData	= "";
	sendData += "OWNER=" + YGV_Owner + "&USER=" + YGV_User + "&CNT=" + YGV_ChatCnt + "&HSN=" + YGV_ChatHSN + "&MID=" + mainCook + "&SID=" + subCook;

	UFN_XmlHttpOpenSend(getPost, urlAppl, trueFalse, sendData, null);
}

function UFN_ChatFillupUser(argXml)
{
	var chatUser	= argXml.getElementsByTagName("yag_chatuser");
	var chatUserCnt	= chatUser.length;

	UFN_ChatFillupUserCnt(chatUserCnt);
	UFN_ChatFillupUserSelf("alert");
	UFN_ChatFillupUserGuest(chatUser, chatUserCnt);
	UFN_ChatUserReInit(chatUser, chatUserCnt);
}

function UFN_ChatFillupUserCnt(argCnt)
{
	var objCnt;
	objCnt = UFN_GetObjById("span_yag_chat_user_cnt");
	objCnt.innerHTML = argCnt + 1;

	objCnt = UFN_GetObjById("span_yag_chatcnt");
	objCnt.innerHTML = argCnt + 1;

	UFN_GetObjById("span_yag_chatcnt_max").innerHTML = YGV_MaxChatCnt;
	UFN_GetObjById("span_yag_chat_user_max").innerHTML = YGV_MaxChatCnt;
}

function UFN_ChatFillupUserSelf(argAlert) //argAlert="noalert" (From: UFN_NickChangeAction) 
{
	var objSelf = UFN_GetObjById("div_yag_chat_user_self");
	var str = "";
	var self_mid = UFN_GetCookie(YGV_CKmain);
	var self_nick = UFN_GetCookie(YGV_CKnick);

	if (self_nick == "")
	{
		self_nick = "나";
	}
	str += "<img src=\"" + YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_i.gif\" width=\"9\" height=\"11\" border=\"0\" alt=\"나\" />&nbsp;"
	str += self_nick + " (" + self_mid.substr(0, 4) + ")";
	objSelf.innerHTML = str;

	if (argAlert == "alert" && YGV_ChatCnt == 0)
	{
		if (UFN_GetCookie(YGV_CKchatStart) != "Y")
		{
			YGV_ChatJoinSelfAlert += "<div class=\"yag_chat_alert\">입장: " + self_nick + " (" + self_mid.substr(0, 4) + ") 님</div>";
		}
	}
}

function UFN_ChatFillupUserGuest(argUser, argCnt)
{
	var objGuest		= UFN_GetObjById("div_yag_chat_user_guest");
	var objGuestLength	= objGuest.getElementsByTagName("div").length;
	var str, obj, tn, len;
	var u_mid, u_sid, u_nick;

	for (var i = 0; i < objGuestLength; i++)
	{
		objGuest.removeChild(objGuest.getElementsByTagName("div").item(0));
	}
	objGuest.innerHTML = "";

	for (var i = 0; i < argCnt; i++)
	{
		u_mid	= argUser[i].childNodes[0].firstChild.nodeValue;
		u_sid	= argUser[i].childNodes[1].firstChild.nodeValue;
		u_nick	= argUser[i].childNodes[2].firstChild.nodeValue;

		obj = document.createElement("div");
		obj.id = u_mid;
		str = "";

		if (YGV_AdmMid != "" && u_mid == YGV_AdmMid)
		{
			str += "<img src=\"" + YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_sysop.gif\" width=\"9\" height=\"11\" border=\"0\" alt=\"사이트지기\" />&nbsp;";
		}
		else
		{
			str += "<img src=\"" + YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_guest.gif\" width=\"9\" height=\"11\" border=\"0\" alt=\"손님\" />&nbsp;";
		}

		str += "<span onclick=\"UFN_MemoWriteShow(\'" + u_mid + "\', \'" + u_sid + "\', \'" + u_nick + "\');\" onmouseover=\"UFN_TextDecoration(this, \'u\');\" onmouseout=\"UFN_TextDecoration(this, \'n\');\" title=\"쪽지보내기\">";
		str += u_nick + " (" + u_mid.substr(0, 4) + ")";
		str += "</span> ";

		tn = UFN_ChatDenyUserCheck(u_mid);
		if (tn == -1)
		{
			str += "<img src=\"" + YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_speak_on.gif\" width=\"11\" height=\"11\" border=\"0\" onclick=\"UFN_ChatDenyUserChange(\'" + u_mid + "\', this);\" onmouseover=\"UFN_TextDecoration(this, \'u\');\" onmouseout=\"UFN_TextDecoration(this, \'n\');\" />";
		}
		else
		{
			str += "<img src=\"" + YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_speak_off.gif\" width=\"11\" height=\"11\" border=\"0\" onclick=\"UFN_ChatDenyUserChange(\'" + u_mid + "\', this);\" onmouseover=\"UFN_TextDecoration(this, \'u\');\" onmouseout=\"UFN_TextDecoration(this, \'n\');\" />";
		}

		obj.innerHTML = str;
		objGuest.appendChild(obj);
	}

	obj = null;
}

function UFN_ChatUserReInit(argUser, argCnt)
{
	var newUser, outUser;
	var u_mid, u_sid, u_nick;
	var a_mid, a_nick, a_str;
	var tmp = new Array();
	var len = YGV_ChatJoinUserArray.length;
	var sep = "[##]";

	for (var i = 0; i < argCnt; i++)
	{
		newUser		= "Y";
		changeNick	= "N";
		u_mid		= argUser[i].childNodes[0].firstChild.nodeValue;
		u_sid		= argUser[i].childNodes[1].firstChild.nodeValue;
		u_nick		= argUser[i].childNodes[2].firstChild.nodeValue;

		tmp[tmp.length] = u_mid + sep + u_nick;

		for (var j = 0; j < len; j++)
		{
			YGV_ChatJoinUserArray[j] += "";
			a_str	= YGV_ChatJoinUserArray[j].split(sep);
			a_mid	= a_str[0];
			a_nick	= a_str[1];

			if (u_mid == a_mid)
			{
				newUser = "N";

				if (u_nick != a_nick)
				{
					changeNick = "Y";
				}
				break;
			}
		}

		if (newUser == "Y")
		{
			YGV_ChatJoinUserAlert += "<div class=\"yag_chat_alert\">입장: " + u_nick + " (" + u_mid.substr(0, 4) + ") 님</div>";
		}

		if (changeNick == "Y")
		{
			YGV_ChatJoinUserAlert += "<div class=\"yag_chat_alert\">변경: " + a_nick + " (" + a_mid.substr(0, 4) + ") 님 → " + u_nick + "</div>";
		}
	}

	for (var i = 0; i < len; i++)
	{
		outUser = "Y";
		YGV_ChatJoinUserArray[i] += "";
		a_str	= YGV_ChatJoinUserArray[i].split(sep);
		a_mid	= a_str[0];
		a_nick	= a_str[1];

		for (var j = 0; j < argCnt; j++)
		{
			u_mid	= argUser[j].childNodes[0].firstChild.nodeValue;
			u_sid	= argUser[j].childNodes[1].firstChild.nodeValue;
			u_nick	= argUser[j].childNodes[2].firstChild.nodeValue;

			if (a_mid == u_mid)
			{
				outUser = "N";
				break;
			}
		}

		if (outUser == "Y")
		{
			YGV_ChatOutUserAlert += "<div class=\"yag_chat_alert\">퇴장: " + a_nick + " (" + a_mid.substr(0, 4) + ") 님</div>";
		}
	}

	YGV_ChatJoinUserArray = tmp;
}

function UFN_ChatDenyUserCheck(argMid)
{
	var len = YGV_ChatDenyUserArray.length;

	for (var i = 0; i < len; i++)
	{
		if (argMid == YGV_ChatDenyUserArray[i])
		{
			return i;
		}
	}
	return -1;
}

function UFN_ChatDenyUserChange(argMid, argObj)
{
	var len = YGV_ChatDenyUserArray.length;
	var deny = "N";

	for (var i = 0; i < len; i++)
	{
		if (argMid == YGV_ChatDenyUserArray[i])
		{
			deny = "Y";
			break;
		}
	}

	if (deny == "Y")
	{
		argObj.src = YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_speak_on.gif"
		UFN_ChatDenyUserReInit(argMid);
	}
	else if (deny == "N")
	{
		argObj.src = YGV_PluginURL + "/skin/" + YGV_Skin + "/yag_speak_off.gif"
		YGV_ChatDenyUserArray[len] = argMid;
	}
}

function UFN_ChatDenyUserReInit(argMid)
{
	var tmp = new Array();
	var len = YGV_ChatDenyUserArray.length;

	for (var i = 0; i < len; i++)
	{
		if (YGV_ChatDenyUserArray[i] == argMid)
		{
			YGV_ChatDenyUserArray[i] = "";
		}

		if (YGV_ChatDenyUserArray[i] != "")
		{
			tmp[tmp.length] = YGV_ChatDenyUserArray[i];
		}
	}

	YGV_ChatDenyUserArray = tmp;
}

function UFN_ChatFillupHistory(argXml)
{
	var c_history, c_cnt, obj, len, denyUser;
	var sn, u_mid, u_sid, u_nick, msg;
	var chatframe = UFN_GetObjById("div_yag_chat_frame");
	var scroll = "N";

	if (YGV_ChatJoinUserAlert != "" && YGV_ChatCnt != 0)
	{
		UFN_ChatUserAlert(YGV_ChatJoinUserAlert);
		scroll = "Y";
	}

	if (argXml.getElementsByTagName("yag_exhistory")[0].firstChild.nodeValue == "Y")
	{
		c_history = argXml.getElementsByTagName("yag_chathistory");
		c_cnt = c_history.length;

		for (var i = 0; i < c_cnt; i++)
		{
			sn		= c_history[i].childNodes[0].firstChild.nodeValue;
			u_mid	= c_history[i].childNodes[1].firstChild.nodeValue;
			u_sid	= c_history[i].childNodes[2].firstChild.nodeValue;
			u_nick	= c_history[i].childNodes[3].firstChild.nodeValue;
			msg		= c_history[i].childNodes[4].firstChild.nodeValue;

			denyUser = "N";
			len = YGV_ChatDenyUserArray.length;
			for (var j = 0; j < len; j++)
			{
				if (u_mid == YGV_ChatDenyUserArray[j])
				{
					denyUser = "Y";
					break;
				}
			}

			if (denyUser != "Y")
			{
				obj = document.createElement("div");
				obj.innerHTML = "<span>" + u_nick + "&nbsp;(" + u_mid.substr(0, 4) + ")</span> " + UFN_HtmlReg(msg);
				chatframe.appendChild(obj);
				scroll = "Y";
			}
		}

		obj = null;
	}


	YGV_ChatHSN = argXml.getElementsByTagName("yag_maxsn")[0].firstChild.nodeValue;

	if (YGV_ChatOutUserAlert != "" && YGV_ChatCnt != 0)
	{
		UFN_ChatUserAlert(YGV_ChatOutUserAlert);
		scroll = "Y";
	}

	if (YGV_ChatJoinSelfAlert != "")
	{
		UFN_ChatUserAlert(YGV_ChatJoinSelfAlert);
		scroll = "Y";
	}

	if (scroll == "Y")
	{
		UFN_ChatScrollMove();
	}
}

function UFN_ChatUserAlert(argStr)
{
	var frame = UFN_GetObjById("div_yag_chat_frame");
	var obj = document.createElement("div");
	obj.innerHTML = argStr;
	frame.appendChild(obj);
	obj = null;
}

function UFN_ChatKeyDown(argEvent)
{
	if (argEvent.keyCode == 13)
	{
		UFN_ChatAction();
	}
}

function UFN_ChatAction()
{
	UFN_CheckCookie();

	var mid		= UFN_GetCookie(YGV_CKmain);
	var sid		= UFN_GetCookie(YGV_CKsub);
	var nick	= UFN_GetCookie(YGV_CKnick);
	var objTextarea	= UFN_GetObjById("textarea_yag_chat_write");
	var valTextarea	= UFN_ContentCheck(objTextarea);
	var chatframe	= UFN_GetObjById("div_yag_chat_frame");
	var obj;

	if (valTextarea != "")
	{
		var getPost		= "POST";
		var urlAppl		= YGV_PluginURL + "/chat_proc.php";
		var trueFalse	= true;
		var sendData	= "";
		sendData += "OWNER=" + YGV_Owner + "&MID=" + mid + "&SID=" + sid;
		sendData += "&MSG=" + encodeURIComponent(valTextarea);
		UFN_XmlHttpOpenSend(getPost, urlAppl, trueFalse, sendData, null);

		if (nick == "")
		{
			nick = "나";
		}

		obj = document.createElement("div");
		obj.innerHTML = "<span>" + nick + "&nbsp;(" + mid.substr(0, 4) + ")</span> " + UFN_HtmlReg(valTextarea);
		chatframe.appendChild(obj);
		obj = null;
		UFN_ChatScrollMove();
	}

	objTextarea.blur();
	objTextarea.value = "";
	setTimeout("UFN_ChatTextareaFocus()", 10);
}

function UFN_ChatTextareaFocus()
{
	UFN_GetObjById("textarea_yag_chat_write").focus();
}

function UFN_ChatScrollMove()
{
	var chatframe = UFN_GetObjById("div_yag_chat_frame");

	YGV_ChatScrollTop	= chatframe.scrollHeight;
	chatframe.scrollTop = chatframe.scrollHeight;
}

function UFN_ChatScrollCheck()
{
	YGV_ChatScrollTop = UFN_GetObjById("div_yag_chat_frame").scrollTop;
}

function UFN_ChatLocationCheck()
{
	UFN_ChatMagicShow();
	setTimeout("UFN_ChatLocationCheck()", 500);
}

function UFN_ChatMagicShow()
{
	UFN_InnerWidth();
	UFN_PageHeight();
	UFN_ScrollingOffset();

	var top, left;

	switch(YGV_ChatLocation)
	{
		case 1:
			top		= YGV_ScrollTop + YGV_CrossY;
			left	= YGV_ScrollLeft + YGV_CrossX;
			break;
		case 2:
			top		= YGV_ScrollTop + YGV_CrossY;
			left	= YGV_WinInnerWidth + YGV_ScrollLeft - YGV_ChatWidth - YGV_CrossX;
			break;
		case 3:
			top		= YGV_WinInnerHeight + YGV_ScrollTop - YGV_ChatHeight - YGV_CrossY;
			left	= YGV_ScrollLeft + YGV_CrossX;
			break;
		default: // 4포함
			top		= YGV_WinInnerHeight + YGV_ScrollTop - YGV_ChatHeight - YGV_CrossY;
			left	= YGV_WinInnerWidth + YGV_ScrollLeft - YGV_ChatWidth - YGV_CrossX;
			break;
	}

	var container = UFN_GetObjById("div_yag_chat");
	container.style.top			= top + "px";
	container.style.left		= left + "px";
	container.style.width		= YGV_ChatWidth + "px";
	//container.style.height		= YGV_ChatHeight + "px";
	container.style.position	= "absolute";
}