<%@ page contentType="text/html; charset=utf-8"
import = "java.io.*"
import = "cn.js.fan.util.*"
%>
<%@ page import="com.redmoon.chat.ChatClient"%>
<%@ page import="java.util.Properties" %>
<%
//本文件用于刷新各种数据
request.setCharacterEncoding("utf-8");
String nick,room;
nick = ParamUtil.get( request, "nick" );
room = ParamUtil.get( request, "room" );
%>
<html>
<head>
<title>refresher</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript" type="text/javascript">
var nick,room;
nick = "<%=nick%>"
room = "<%=room%>"

function GetData(url) {
	try {
    	DataLoader.src = url;
    } catch(e) {
        return false;
    }
}

function GetMsg(url) {
	try {
    	MsgLoader.src = url;
    } catch(e) {
        return false;
    }
}

function GetUserList() {
	try {
    	UserListLoader.src = "chatclient.jsp?mode=list&user=" + getUserUTF8() + "&room=" + getRoomUTF8();
    } catch(e) {
        return false;
    }
}

var timeoutid
var count = 0;

function getUser() {
	return nick;
}

function getRoom() {
	return room;
}

var timeoutid;
var timespace;
var oldtime = 0
function refresh()
{
	var d = new Date();
	sec = d.getSeconds()
	timespace = sec-oldtime
	if(timespace<0)
		timespace += 60
	if (timespace>=6) {
		oldtime = timespace;
		GetUserList();
	}

	var url = "chatclient.jsp?mode=poll&room=" + getRoomUTF8() + "&user=" + getUserUTF8();
	GetData( url );
	timeoutid = window.setTimeout("refresh()", 3000);//每隔6秒钟刷新一次
}

function getRoomUTF8()
{
  return myroom.innerHTML;
}

function getUserUTF8()
{
	return mynick.innerHTML;
}

function addnicks(users)
{
  var ary = users.split(",");
  var len = ary.length;
  var str = "", user="";
  for (i=0; i<len; i++)
  {
  	user = ary[i];	
	str += "<a href=javascript:window.parent.talkarea.getname('" +user+ "')>[" +user+ "]</a><br>"
  }
  if (typeof(window.parent.nickarea)=="object")
	  if (typeof(window.parent.nickarea.s_nicknamelist)=="object")
		window.parent.nickarea.s_nicknamelist.innerHTML = str;
}

function rename(newuser)
{
    if (typeof(window.parent.senderFrame.rename)=="function")
		window.parent.senderFrame.rename(getUser(), getRoom(), newuser);
}

function jumptoroom(room)
{
    if (typeof(window.parent.senderFrame.JumpToRoom)=="function")
		window.parent.senderFrame.JumpToRoom(getUser(), getRoom(), room);
}

function sendAnnounce(str)
{
    if (typeof(window.parent.senderFrame.sendAnnounce)=="function")
		window.parent.senderFrame.sendAnnounce(getUser(), getRoom(), str);
}

function kicksb(sb)
{
    if (typeof(window.parent.senderFrame.KickSb)=="function")
		window.parent.senderFrame.KickSb(getUser(), getRoom(), sb);
}

function getRoomInfo() {
    if (typeof(window.parent.senderFrame.getRoomInfo)=="function")
		window.parent.senderFrame.getRoomInfo(getUser(), getRoom());
}

function getUserInfo(user) {
    if (typeof(window.parent.senderFrame.getUserInfo)=="function")
		window.parent.senderFrame.getUserInfo(getUser(), getRoom(), user);
}

function showtalk(msg)
{
	if (typeof(window.parent.showchatarea)=="object")
		if (typeof(window.parent.showchatarea.parsemyshow)=="function")
			window.parent.showchatarea.parsemyshow(msg);
}

function roomlist() {
	window.parent.nickarea.location.href = "roomlist.jsp?user=" + getUserUTF8() + "&room=" + getRoomUTF8();
}

function showmsg(msg)
{
  msg = "<table><tr><td>"+msg+"</td></tr></table>"
  if (typeof(window.parent.showchatarea.showchat)=="function")
    window.parent.showchatarea.showchat(msg,1);
}

function send(msg)
{
  if (typeof(window.parent.senderFrame.SendMsg)=="function")
    window.parent.senderFrame.SendMsg(getUser(), getRoom(), msg);
}

function window_onload()
{
  refresh();

  if (typeof(window.parent.nickarea)=="object")
  	if (typeof(window.parent.nickarea.room)=="object")
	  window.parent.nickarea.room.innerText = getRoom();

  if (typeof(window.parent.talkarea)=="object")  
	  if (typeof(window.parent.talkarea.setmynick)=="function") {
		window.parent.talkarea.setmynick(getUser())
	  }
  if (typeof(window.parent.talkarea)=="object")  
	  if (typeof(window.parent.nickarea.setroom)=="function")
		window.parent.nickarea.setroom(getRoom());
}

function window_onunload() {
<%
	String reloadreason = ParamUtil.get( request, "reloadreason" );
	//当jumptoroom reload时并不logout
	if (!reloadreason.equals("jumptoroom")) {
%>	
	LogoutScript.src = "chatclient.jsp?mode=Logout&user=" + getUserUTF8() + "&room=" + getRoomUTF8();
<%}%>	
}
//-->
</SCRIPT>
<script id="DataLoader" language="JavaScript" type="text/javascript" defer></script>
<script id="MsgLoader" language="JavaScript" type="text/javascript" defer></script>
<script id="UserListLoader" language="JavaScript" type="text/javascript" defer></script>
<script id="LogoutScript" language="JavaScript" type="text/javascript" defer></script>
</head>
<body onLoad="return window_onload()" onUnload="return window_onunload()">
<jsp:useBean id="cfgparser" scope="page" class="cn.js.fan.util.CFGParser"/>
用户: <span id="mynick" name="mynick"><%=StrUtil.UrlEncode(nick)%></span>
房间: <span id="myroom" name="myroom"><%=StrUtil.UrlEncode(room)%></span>
<%
// 登录聊天室
ChatClient cc = new ChatClient();
try {
	cfgparser.parse("config.xml");
	Properties props = cfgparser.getProps();
	// 当jumtptoroom时，因为室中已有nick，所以不需要再login
	// System.out.println("chatapplet.jsp:" + " nick=" + nick + " room=" + room);
	if ( !reloadreason.equals("jumptoroom") && cc.login( request, nick, room ) ) {
		out.print( StrUtil.ShowStatus( cc.getReturnMsg() ) );
	}
}
catch ( ErrMsgException e ) {
	out.println( StrUtil.ShowStatus(e.getMessage()) );
	return;
}
%>
</body>
</html>
