﻿var webim=new Object();
webim.statusmap={
	'normal':"online",
	'unavailable':"offline",
	'away':"away",
    'xa':"away",
	'online':"online",
	'dnd':"busy"
};
webim.status={
    retry:0,
	logined:false,
	lock:false,
	myname:'',
	nickname:'',
	ischat:false,
	isDelayChat:false
};

webim.messages = new Array();

webim.errmap={
	'503':"所要登录的房间人数已满",
	'401':"所登录的房间需要密码才能进入",
	'403':"禁止进入房间",
	'404':"指定登录的房间不存在",
	'405':"没有权限创建房间",
	'406':"不允许的昵称名字",
	'407':"您目前的角色不允许进入这个房间",
	'409':"房间昵称已有人使用或注册过了"
};


webim.start=function(config){
	this.config=config;
	this.Faqee_IM=this.getFlashMovie(config.flashname);
}
//发送信息
webim.sendMessage=function(msg,to){	
	this.Faqee_IM.sendMessage(msg,to);
}
webim.ping=function(){
	this.Faqee_IM.ping();
}
//发送扩展信息
webim.sendXMessage=function(msg,to,msgProp){	
	this.Faqee_IM.sendXMessage(msg,to,msgProp);
}

//发送信息MSN
webim.sendMessageByMSN=function(msg,to){
	this.Faqee_IM.sendByMSN(msg,to);
}
//发送邀请
webim.postRequest=function(jid,nickname,group){
	this.Faqee_IM.postRequest(jid,nickname,group);
}
//设置状态
webim.setStatus=function(vshow,vstatus){
	this.Faqee_IM.setStatus(vshow,vstatus);
}
//修改好友备注名称或修改好友组
webim.updateContact=function(jid,newNick,newGroup){
	this.Faqee_IM.updateContact(jid,newNick,newGroup);
}
//删除联系人
webim.removeContact=function(jid){
	this.Faqee_IM.removeContact (jid);
}
//登录
webim.login=function(){
	if(typeof(this.Faqee_IM.login) == 'function'){
		this.status.lock = true;
		this.Faqee_IM.login();
		//5秒后检查登录状态
		if(!webim.status.logined){
			window.setTimeout(function(){
				if(webim.status.logined == false){
					webim.login();
					webim.status.retry = webim.status.retry + 1;
				}
			},5000);
		}
	}else{
		if(!this.status.lock){
			window.setTimeout(function(){
				if(webim.status.logined == false){
					webim.login();
				}
				webim.status.retry = webim.status.retry + 1;				
			},2000);
		}
	}
}
//增加联系人
webim.addContact=function(jid,displayName,group){
	this.Faqee_IM.addContact(jid,displayName,group,true);
}
//获取联系人信息
webim.getContactInformation=function(jid){
	this.Faqee_IM.getContactInformation(jid);
}
//拒绝邀请
webim.denySubscription=function(jid){
	this.Faqee_IM.denySubscription(jid);	
}
//退出
webim.exit=function(){
	this.Faqee_IM.exit();
}
//处理邀请
webim.grantRequest=function(jid,flag){
	this.Faqee_IM.grantRequest(jid,flag);
}
//绑定msn
webim.bindMSN=function(msnname,msnpassword,svr){
	svr=svr||"msn";
	this.Faqee_IM.bindMSN(msnname,msnpassword,svr);
}
//解除msn绑定
webim.unbindMSN=function(svr){
	svr=svr||"msn";
	this.Faqee_IM.unbindMSN(svr);
}

//创建加入房间
webim.joinRoom=function(roomid,nick,pass){
	this.Faqee_IM.joinRoom(roomid,nick,pass);
}
//配置房间属性
webim.configureRoom=function(roomid,config){
	this.Faqee_IM.configureRoom(roomid,config);
}
//离开房间
webim.leaveRoom=function(roomid){
	this.Faqee_IM.leaveRoom(roomid);
}
//踢出房间
webim.kickoff=function(roomid,vnick){
	this.Faqee_IM.kickoff(roomid,vnick,"");
}
//房间授权
webim.grant=function(roomid,jid,affication){
	this.Faqee_IM.grant(roomid,jid,affication);
}
//请求我在房间的所属角色
webim.requestAffiliation=function(roomid,affication){
	this.Faqee_IM.requestAffiliation(roomid,affication);
}
//发送房间消息
webim.sendGroupMessage=function(roomid,msg){
	this.Faqee_IM.sendMessageToGroup(roomid,msg);
}
//发送房间私有消息
webim.sendGroupPrivateMessage=function(roomid,msg,to){
	this.Faqee_IM.sendPrivateMessage(roomid,msg,to);
}
//修改房间主题
webim.changeSubject=function(roomid,newSubject){
	this.Faqee_IM.changeSubject(roomid,newSubject);
}
//改名
webim.renameNickRoom=function(roomid,newname){
	this.Faqee_IM.renameNickRoom(roomid,newname);
}
//邀请某人加入房间
webim.roomInviteOne=function(roomid,jid,reason){
	this.Faqee_IM.roomInviteOne(roomid,jid,reason);
}
//拒绝某人邀请我加入一个房间,不起作用
webim.roomDenyOne=function(roomid,from,reason)
{
	this.Faqee_IM.roomDenyOne(roomid,from,reason);
}
//请求房间成员信息
webim.requestRoomMember=function(roomid){
	this.Faqee_IM.requestRoomMember(roomid);
}

webim.getFlashMovie=function(movieName){
	 var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
//显示flash
webim.toFlash=function(config){
	var so = new SWFObject(config.filename, config.flashname, "2", "2", "9", "#FF0066");
		so.addParam("allowScriptAccess", "always");
		so.addParam("play", "true");
		so.addParam("wmode", "transparent");
		so.addVariable("jurl", config.url);
		so.addVariable("jname", config.username);
		so.addVariable("jpassword", config.password);
		so.addVariable("jkey", config.key);
		so.addVariable("imhandler",config.imhandler);
		so.write("flashcontent");
}

//回调处理
var imhandler=new Object();
//链接成功
function ExeonConnect(){

}
//断开链接
function ExeonDisconnect(){
	//luguoUI.leaveRoomMessage();
	sysMessage("你已断开连接！");
}
//错误信息
function ExeonError(errcode,errCate){
/*
	if(typeof(errCate)!='undefined' && ("muc"==errCate)){
		sysMessage("操作失败，可能的原因是您设置了大名后重复登陆，自己连上了自己！");
	}else{
		if(errcode=="403"){
			sysMessage("未经权限允许的操作！");
		}else if(errcode=="502"||errcode=="409"){
			sysMessage("重复登录");
		}else
			sysMessage("onError:"+errcode);
	}
	*/
}

//登录
function ExeonLogin(jid){
	sysMessage('已成功连接上聊天服务器.');
	window.setInterval(function(){webim.ping()},30000);
}

//匿名登录的回调
function ExeonAnonyLogin(jid){
	webim.status.myname = jid.split("@")[0];
	webim.status.nickname = luguoUI.getCookie('nickname')==null?jid.split("@")[0]:luguoUI.getCookie('nickname');
	webim.status.logined = true;
	luguoUI.init();
	if(webim.status.nickname.length != 8)
		sysMessage("您的大名：<font color=blue>"+webim.status.nickname+"</font>");
		//sysMessage("系统分配给的ID："+webim.status.myname);
	//$("#linked").html("(准备就绪)");
	sysMessage("已成功连接上聊天服务器!");
	window.setInterval(function(){webim.ping()},30000);
	$(".boxy-wrapper").hide();
	luguoUI.playSound('online');
	if(webim.status.isDelayChat){
		luguoUI.startChat(true);
	}
}
function ExeOnGetContace(obj){

}
//如果联系人还没载入完，请调用这个函数
function ExeOnNoRosterStatusChange(jid,vshow,statu){
window.setTimeout("ExeonStatusChange(\""+jid+"\",\""+vshow+"\",\""+statu+"\")",3000);
}

//状态改变
function ExeonStatusChange(jid,vshow,statu){
	if((jid == "gtalk."+GCONFIG.url) && vshow=='online'){
		$("#btn_sendinvite").val("发送邀请");
		$("#btn_sendinvite").attr("disabled",false);
	}else if((jid == "msn."+GCONFIG.url) && vshow=='online'){
		$("#btn_sendinvite").val("发送邀请");
		$("#btn_sendinvite").attr("disabled",false);
	}else if((jid == "yahoo."+GCONFIG.url) && vshow=='online'){
		$("#btn_sendinvite").val("发送邀请");
		$("#btn_sendinvite").attr("disabled",false);
	}else if((jid == "icq."+GCONFIG.url) && vshow=='online'){
		$("#btn_sendinvite").val("发送邀请");
		$("#btn_sendinvite").attr("disabled",false);
	}else
		luguoUI.whoChange(jid,vshow,statu);
}

//接收信息
function ExeonMessage(msg){
	var jid = msg.from;
	if((jid == "gtalk."+GCONFIG.url) ){
		alert(msg.body);
		$(".boxy-wrapper").hide();
	}else if((jid == "msn."+GCONFIG.url) ){
		alert(msg.body);
		$(".boxy-wrapper").hide();
	}else if((jid == "yahoo."+GCONFIG.url) ){
		alert(msg.body);
		$(".boxy-wrapper").hide();
	}else if((jid == "icq."+GCONFIG.url)){
		alert(msg.body);
		$(".boxy-wrapper").hide();
	}else if(jid==GCONFIG.url){
		luguoUI.openWnd({title:'系统广播',content:msg.body});
		return;
	}
	luguoUI.resvMessage(msg);
	
}

function ExeonLeaveMessage(msg){
	
}

//键盘事件
function  ExeOnTyping(from,type){
	if(type=="typing")
		typeMessage("对方正在输入信息");
	else if(type=="notyping")
		typeMessage("");
}
//消息扩展属性
function ExeonXMessage(messages,from,msgProp){

}

//更新用户列表
function ExeonUpdateUserList(act,list){
	$("#msnlist").empty();
	if(act=="add"){
		if(list!=null){
			$("#msnlist").append("<div id='inviteList'></div>");
			for(var i=0;i<list.length;i++)
			{
				if(list[i].jid.split("@")[0]=="") return;
				$("#inviteList").append("<p id='myf_"+list[i].jid.split("@")[0]+"'><input type='checkbox' value='"+list[i].jid+"' name='chkinvite'/>"+list[i].displayName+"</p>");
			}
			$("#msnlist").append("<p><textarea rows=5 style='width:90%' id='chkinvitetxt'>这里有个很好玩的“路过的”，赶快来玩吧！http://luguo.faqee.com/</textarea></p>");
			$("#msnlist").append("<center><a href='javascript:luguoUI.selectAll(\"chkinvite\")'>全选</a><input type='button' id='btn_sendinvite' value='正在读取好友列表' disabled onclick='luguoUI.sendInvite(\"chkinvite\")' /></center>");
		}
	}

}

//状态请求回调
function ExeonRequest(jid){

}
//邀请被拒绝,或好友被删除时的回调
function ExeonRequestDenial(jid){
	sysMessage("<font color=red><b>该陌生人目前正在聊天，请换个人吧！</b></font>");
	luguoUI.leaveRoomMessage();
	luguoUI.playSound('message');
}
//取消请求状态
function ExeonRevocation(jid){

}
/**************
*2008-01-17增加
修改房间昵称回调
**************/
function ExeonNickname(room,nickname){
}
function ExeonRoomConfig(room){
	
}
//登录房间回调
function ExeonRoomIn(room){


}
//房间有人离开
function ExeonRoomLeave(room){
	
	//var mess = luguoUI.HTMLMODEL.overinfor;
	//sysMessage(mess);
	webim.leaveRoom(room['roomid']);
}
//某房间有人进来了
function ExeonRoomUserIn(room,nickname){
	if(nickname!=webim.status.myname){
		luguoUI.showEnabled(false);
		sysMessage('现在可以和陌生人('+nickname+')聊天了...');
		firstMessage('你好！');
		luguoUI.playSound('message');
		webim.status.ischat = true;
	}
}
//某房间有人离开了
function ExeonRoomUserLeave(room,nickname){
	if(nickname!=webim.status.myname){
		luguoUI.showEnabled(true);
		var mess = luguoUI.HTMLMODEL.overinfor;
	    sysMessage(mess);
		luguoUI.playSound('message');
		webim.status.ischat = false;
	}
}
//某房间收到您发的邀请被人拒绝
function ExeonRoomInviteDeny(roomid,from,reason){
	sysMessage("你刚才发送的聊天邀请被陌生人拒绝，你可以尝试着换一个");
	luguoUI.leaveRoomMessage();
	luguoUI.playSound('message');
}

function getFocusWindow()
{
     window.focus();
}

//收到某人的房间邀请
function ExeonRoomInvite(roomid,from,reason)
{
	/*
	var temp = luguoUI.getCookie("nochat");
	var temp2 = luguoUI.getCookie("autochat");
	if(temp && temp == "1"){
		webim.denySubscription(from);
	}else if(((temp2==null) || (temp2 == "1")) && !webim.status.ischat){
		luguoUI.startChat(false,roomid);
	}else{
		getFocusWindow();
		if(typeof(reason)=='undefined'||reason==null||reason=='null') reason = '系统随机配对';
		Boxy.ask("有个陌生人想和你聊天，你接受该聊天邀请么？<p><b>邀请理由：</b><font color=blue>"+reason+"</font></p><p><font color=orange style='font-size:12px'><b>特别注意：</b>如果你接受聊天，当前聊天将被中断，如果你想与多个人聊天，请打开多个窗口！</font></p>", ["接受", "拒绝"], function(val) {
			  if(val == "接受"){
				luguoUI.startChat(false,roomid);
			  }else{
				  webim.denySubscription(from);
			  }
			}, {title: "系统信息"});
	}
	*/
	if(!webim.status.ischat){
		luguoUI.startChat(false,roomid);
		luguoUI.playSound('invite');
	}else{
		webim.denySubscription(from);
	}
	
}
//得到房间成员所属角色信息
function ExeonRoomAfflication(arr){

}
//得到房间主题修改回调
function ExeonChangeSubject(roomid,subject){

}
//得到房间成员信息回调
function ExeOnRequestRoomMember(roomid,list){

}
//注册成功
function onRegister(){

}