var geti = 30000;
var getinterval; 

function readMsgs() {	
	var url = '/ajax/webim.php?type=readmsg';
	var pars = 'senderid=' + f_uid;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters:pars, onComplete: displayMsgs});
}

function displayMsgs(response){
	var json = response.responseText.evalJSON();
	if(json.uids != ''){
		var uids = json.uids.split(',');
		var count = uids.length;
		for (i=0; i<count; i++){
			var s_uid = uids[i];
			if(!in_array(s_uid, s_uids)){
				s_uids.push(s_uid);
				createUser(s_uid);
				getUserInfo(s_uid);
			}
			if(s_uid != f_uid){
			$('tab_'+s_uid).className = 'current1';
			}
		}
	}
	var msgs = json.msgs;
	var msgids = new Array();
	msgs.each(function(node){
		msgids.push(node.msgid);
		var now = new Date(node.atime*1000);
		var timeValue = now.getFullYear();
		var month = (now.getMonth()+1);
		timeValue += ((month < 10) ? "-0" : "-") + month;
		var day = now.getDate();
		timeValue += ((day < 10) ? "-0" : "-") + day;
		var hour = now.getHours();
		timeValue += ((hour < 10) ? " 0" : " ") + hour;
		var minute = now.getMinutes();
		timeValue += ((minute < 10) ? ":0" : ":") + minute;
		var second = now.getSeconds();
		timeValue += ((second < 10) ? ":0" : ":") + second;
		var msg ='';
if(isservice == 1 || node.senderid ==113045 || node.senderid ==178 || node.senderid ==68119 || node.senderid ==68118  || node.senderid ==123741  || node.senderid ==179  ){
			msg = '<span class="fontbule">' + node.sender + '  (' + timeValue + ')</span><br/><span class="fontgrey">' + node.content + '</span><br/>';
		 if(msgids.length > 0){		
    		updateRead(uid, msgids.toJSON());	   	 	
    	}
}else{
	if(node.content!=''){			
		if($('servicemsg_'+node.senderid))
		{			
			msg='';
		}else{
			msg += '<span class="fontgreen" id="servicemsg_'+node.senderid+'">';
			msg += '<br><br><br><br>温馨提示：对方给您发了新消息！由于您没有开通我在找你VIP服务，暂时无法查看对方的新消息！'+
						'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="image" src="/image/new/talk/wykt.gif"  onclick="processChat('+uid+');return false;">';
			msg += '</span><br/>';
		}
	}	
	
}
	$('msgs_'+node.senderid).innerHTML = $('msgs_'+node.senderid).innerHTML + msg;
	$('msgs_'+node.senderid).scrollTop = $('msgs_'+node.senderid).scrollHeight;	
    });
    if(msgids.length > 0){		    	
	    geti = 30000;
    }else{
    	if(geti < 40000){
			geti = geti + 2000;
		}
    }
    if(getinterval != 'undefined'){
	    clearTimeout(getinterval);
	}
	getinterval=setTimeout("readMsgs();",geti);
	if(f_uid == null){
		f_uid = s_uids.first();
		if(f_uid == undefined) {
			Close();
		}else{
			displayTab(f_uid);
		}
	}    
}

function updateRead(uid, msgids) {
	var url = '/ajax/webim.php?type=updateread';
	var pars = 'uid=' + uid + '&msgids=' + msgids;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters:pars, onComplete: parseUpdateRead});
}

function parseUpdateRead(response) {
}

function getUserInfo(uid) {
	var url = '/ajax/webim.php?type=userinfo';
	var pars = 'uid=' + uid;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters:pars, onComplete: processUserInfo});
}

function processUserInfo(response) {
	var json = response.responseText.evalJSON();
	var uid = json.uid;	
	if(json.isvalid == 1){
		$('tn_'+uid).innerHTML = '<img src="/attachments/face/30_30/'+json.face+'" alt="'+json.nickname+'的形象照" />'+json.nickname;
		var rz = '';
		if(json.issincere == 1 && json.type == 0){
			rz = '<img src="image/new/rz.gif" width="14" height="18" />';
		}
		var idma ='';
		if( json.type == 0){
			idma = '<br>ID编号：'+uid;
		}
		var status = '<span>在线</span>';
		var statusclass = 'linkonl';
		if(json.status == 0 && json.bindsms == 0){
			status = '暂时不在线';
			statusclass = 'linkoffl';
		}
		var nameflag = '';
		if(json.type == 1){
			nameflag = '婚恋顾问：';
		}
		var html = '<div class="name">'+
			          '<ul class="nameul3">'+
			            '<li><input type="hidden" id="'+uid+'_type" value="'+json.type+'" /><strong>'+nameflag+'<a href="/zone/index/'+uid+'.html" class="'+statusclass+'" target="_blank">'+json.nickname+'</a></strong>'+rz+idma+'</li>'+
					  '</ul>'+
					'</div>'+
					'<div class="photo"><a href="/zone/index/'+uid+'.html" target="_blank"><img src="/attachments/face/160_160/'+json.face+'" alt="'+json.nickname+'的形象照" border="0" width="160" height="160"/></a></div>'+
			        '<div class="name">'+
			          '<ul class="nameul1">';
		if(json.type != 1){	            
			html += 	'<li>' + json.sex + '，'+json.age+'岁</li>'+
			            '<li>'+json.work_province + json.work_city+'</li>';
		}else{
			html += 	'<li>值班时间：9:00-19:00</li>'+
			            '<li>客服热线：010-'+json.phone+'</li>';
		}            
		html += 	    '<li>对方状态：'+status+'</li>'+
			          '</ul>';
		if(json.type != 1){	          
			html += '<ul class="nameul2">'+
					'<li>';
			html += ' <input type="image" src="image/new/talk/mszq.gif" alt="语音聊天" onclick="window.open(\'/zone/misc.php?do=recruit&u='+ json.uid +'\', \'recruit'+ json.uid +'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" />';
			html += '</li><li>'+
			              '<input onclick="window.open(\'/zone/flower.php?u='+uid+'\', \'flower_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk2_14.gif" />'+
			            '</li>'+
		            '<li>'+
		             '<input onclick="window.open(\'/zone/throwball.php?&u='+uid+'\', \'throwball_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk_24.gif" />'+
		            '</li>'+
			            '<li>'+
			              '<input onclick="window.open(\'/implead.php?uid='+uid+'\', \'implead_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk2_14-07.gif" />'+
			            '</li>'+
						'<li>'+
			              '<input onclick="if(confirm(\'您确定要把对方加入黑名单吗？加入黑名单后，对方将无法继续给您发送消息。\'))window.open(\'/done.php?type=blacklist&uid='+uid+'\', \'blacklist_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');else return false;" type="image" src="image/new/talk/talk08.gif" />'+
			            '</li>'+
			          '</ul>';
		}	          
		html +=  '</div>';
		$('info_'+uid).innerHTML = html;
			if(json.inblack == true){
				html = '<div class="blacklist">对方已列入你的黑名单<br />'+
						  '<label>'+
							'<input type="image" onclick="delIMBlacklist('+uid+')" src="image/new/talk/talk07.gif" />'+
						  '</label>'+
						'</div>';
				$('cnt_'+uid).innerHTML = html;		
			}
	}else{
		closeForm(uid);
	}   
}

function checkMsgs() {
	var url = '/ajax/webim.php?type=checkmsg';
	var myAjax = new Ajax.Request(url, {method: 'post', parameters:'', onComplete: displayTips});
}

function displayTips(response){
	var json = response.responseText.evalJSON();
	var imhisdiv = $('imhisdiv');
	if(json.num > 0)
	{
		$("fly_layer").innerHTML = '<a style="cursor:pointer;" onclick="openIm('+json.uid+');$(\'fly_layer\').style.display = \'none\';"><img src="/image/new/imtip.gif" alt="有新消息" border="0" /></a>';
		$("fly_layer").style.display = '';
	}
	else
	{
		$("fly_layer").style.display = 'none';
	}
}

function closeForm(uid) {
	del_array(uid, s_uids);
	removeUser(uid);
	f_uid = s_uids.first();
	if(f_uid == undefined) {
		Close();
	}else{
		displayTab(f_uid);
	}
}

function sendMsg(uid){
	f_uid = uid;
	if(!checkIsNotEmpty($('content_'+uid).value)){
		alert("请输入聊天信息");
		$('content_'+uid).focus();
		return false;
	}else{
		//先显示内容
		var now = new Date();
		var timeValue = now.getFullYear();
		var month = (now.getMonth()+1);
		timeValue += ((month < 10) ? "-0" : "-") + month;
		var day = now.getDate();
		timeValue += ((day < 10) ? "-0" : "-") + day;
		var hour = now.getHours();
		timeValue += ((hour < 10) ? " 0" : " ") + hour;
		var minute = now.getMinutes();
		timeValue += ((minute < 10) ? ":0" : ":") + minute;
		var second = now.getSeconds();
		timeValue += ((second < 10) ? ":0" : ":") + second;
		var oneMsg = document.createElement("span");
		var msg = '<span class="fontred">' + nickname + ' (' + timeValue + ')</span><br/>' + UBB(nl2br(StripHtml($('content_'+f_uid).value))) + '<br/>';
		oneMsg.innerHTML = msg;
		$('msgs_'+f_uid).appendChild(oneMsg);
		$('msgs_'+f_uid).scrollTop = $('msgs_'+f_uid).scrollHeight;
		//请求数据库处理
		var content = encodeURIComponent(UBB($('content_'+uid).value));
		var url = '/ajax/webim.php?type=sendmsg';
		var pars = 'uid=' + uid +'&content=' + content;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters:pars, onComplete: processMsg});	
		//清空内容
		$('content_'+f_uid).value = "";
		$('content_'+f_uid).focus();
	}
}

function processMsg(response){	
	var resStr = response.responseText;	
	if(resStr == 'true'){
		//$('send_'+f_uid).disabled = false;
	}else{
		//Close();
	}
}

function displayTab(uid) {
	f_uid = uid;
	readMsgs();
	var panelElm = $('panel_'+uid);
	var panelContainerElm = panelElm ? panelElm.parentNode : null;
	var tabElm = $('tab_'+uid);
	var tabContainerElm = tabElm ? tabElm.parentNode : null;
	if (tabElm && tabContainerElm) {
		var nodes = tabContainerElm.childNodes;
		for (var i=0; i<nodes.length; i++) {
			if (nodes[i].nodeName == "LI")
				nodes[i].className = 'tab';	
		}
		var discount=s_uids.length;
		for (i=0; i<discount; i++){
			var s_uid = s_uids[i];
			if(s_uid != f_uid){
				$('tab_'+s_uid).className = 'current3';
			}else{	
				$('tab_'+s_uid).className = 'current2';
			}
		}
	}
	if (panelElm && panelContainerElm) {
		var nodes = panelContainerElm.childNodes;
		for (var i=0; i<nodes.length; i++) {
			if (nodes[i].nodeName == "DIV" && nodes[i].className != "top")
				nodes[i].className = 'panel';
		}
		panelElm.className = 'current2';
	}
	if($('msgs_'+uid)){
		$('msgs_'+uid).scrollTop = $('msgs_'+uid).scrollHeight;
	}
}

function createUser(uid) {
	var html = '<ul><li id="tab_'+uid+'"  class="current2"><span><a id="tn_'+uid+'" href="javascript:displayTab(\''+uid+'\');" onMouseDown="return false;">'+uid+'</a></span></li></ul>'
	$('prrname').innerHTML = $('prrname').innerHTML + html;
	element = document.createElement('DIV');
	element.id = 'panel_' + uid;
	element.className = 'panel';
	var html = '<div class="pleft" id="info_'+uid+'">'+
				'<div class="name">'+
		          '<ul class="nameul3">'+
		            '<li><strong><a href="#" class="linkonl">'+uid+'</a></strong><img src="image/new/rz.gif" width="14" height="18" /></li>'+
				  '</ul>'+
				'</div>'+
		        '<div class="photo"><a href="#" target="_blank"><img src="image/new/temp/61769.jpg" alt="'+uid+'" /></a></div>'+
		        '<div class="name">'+
		          '<ul class="nameul1">'+
		            '<li>载入中...</li>'+
		            '<li>载入中...</li>'+
		            '<li>载入中...</li>'+
		          '</ul>'+
		          '<ul class="nameul2">'+
		            '<li>'+
		              '<input onclick="window.open(\'/zone/misc.php?do=flower&u='+uid+'\', \'flower_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk2_14.gif" />'+
		            '</li>'+
		            '<li>'+
		             '<input onclick="window.open(\'/zone/misc.php?do=throwball&u='+uid+'\', \'throwball_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk_24.gif" />'+
		            '</li>'+
		            '<li>'+
		              '<input onclick="window.open(\'/implead.php?uid='+uid+'\', \'implead_'+uid+'\',\'height=342, width=474, top=200, left=200, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no\');" type="image" src="image/new/talk/talk2_14-07.gif" />'+
		            '</li>'+
		          '</ul>'+
		        '</div>'+

		      '</div>'+

		      '<div class="pright" id="cnt_'+uid+'">'+
			  '<div class="top">点击 <input onclick="openIm(0);" type="image" src="image/new/talk/talk_103.gif" width="99" height="27" alt="缘分大转盘"/>系统将自动为您速配当前在线会员'+
			  '</div> '+

		        '<div class="mesbox"><div class="note" id="msgs_'+uid+'"></div></div>'+

		        '<div class="face">' +

		        '<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=01]\'" src="/image/smiley/love/01.gif" alt="握手" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=02]\'" src="/image/smiley/love/02.gif" alt="可爱" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=03]\'" src="/image/smiley/love/03.gif" alt="飞吻" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=04]\'" src="/image/smiley/love/04.gif" alt="大哭" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=05]\'" src="/image/smiley/love/05.gif" alt="害羞" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=06]\'" src="/image/smiley/love/06.gif" alt="汗" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=07]\'" src="/image/smiley/love/07.gif" alt="色眯眯" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=08]\'" src="/image/smiley/love/08.gif" alt="喔耶" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=09]\'" src="/image/smiley/love/09.gif" alt="坏笑" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=10]\'" src="/image/smiley/love/10.gif" alt="偷笑" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=11]\'" src="/image/smiley/love/11.gif" alt="疑问" border="0"/>'+

				'<img style="cursor:pointer;" onclick="$(\'content_'+uid+'\').value = $(\'content_'+uid+'\').value + \'[EM=12]\'" src="/image/smiley/love/12.gif" alt="拜拜" border="0"/>'+

		        '</div>'+

		        '<div class="send">'+

		          '<textarea name="content_'+uid+'" id="content_'+uid+'" onkeydown="if(event.ctrlKey && event.keyCode == 13 && $(\'send_'+uid+'\').disabled == false) sendMsg(\''+uid+'\');" rows="5" class="seinput"></textarea>'+

		          '<input id="send_'+uid+'" type="image" class="button01" onclick="javascript:sendMsg(\''+uid+'\');return false;" src="image/new/talk/send1.jpg" />'+

		        '</div>'+

		        '<div class="close">'+

		          '<input onclick="closeForm(\''+uid+'\')" type="image" src="image/new/talk/talk2_21.gif" />'+

		         '</div>'+

		      '</div>';

	element.innerHTML = html;

	$('panels').appendChild(element);		  

}



function removeUser(uid) {
	var panelElm = $('panel_'+uid);
	var panelContainerElm = panelElm ? panelElm.parentNode : null;
	var tabElm = $('tab_'+uid);
	var tabContainerElm = tabElm ? tabElm.parentNode : null;
	if (tabElm && tabContainerElm) {
		tabContainerElm.removeChild(tabElm);
	}
	if (panelElm && panelContainerElm) {
		panelContainerElm.removeChild(panelElm);
	}
}

function Flayer()
{
	window.onscroll	=	FlyLayer;
	window.onresize	=	FlyLayer;
	FlyLayer();
}

function FlyLayer()
{
	$("fly_layer").style.top	= (document.documentElement.scrollTop + 450)	+	'px';
	$("fly_layer").style.right	= (document.documentElement.scrollLeft + 10)	+	'px';
}

function openIm(uid) {
	if(uid==0){
		var url = '/ajax/webim.php?type=getMatchedId';
		var pars = 'senderid=0' ;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters:pars, onComplete: displayMatchedId});
	}else{
		try	{ 
			win = window.open('', 'webim','height=555, width=955, top=100, left=100, toolbar=no, menubar=no, resizable=yes,location=no, status=no');
			if(win.uid == undefined){
				win.location='/webim.php?uid='+uid;
			}else{			
				if(uid == win.uid) {
					alert('您不能和自己聊天');
					return;
				}
				if(!win.in_array(uid, win.s_uids)){
					win.s_uids.push(uid);
					win.createUser(uid);
					win.getUserInfo(uid);
				}			
				win.displayTab(uid);
			}
		} catch(e) {   
			win.location='/webim.php?uid='+uid;
		}
		win.focus();
	}
}

function displayMatchedId(response){
	var uid = response.responseText;
	if(uid==0){
		alert("温馨提示：当前在线会员中暂无适合您速配条件的异性会员");
	}else{
		try	{ 
			win = window.open('', 'webim','height=555, width=955, top=100, left=100, toolbar=no, menubar=no, resizable=yes,location=no, status=no');
			if(win.uid == undefined){
				win.location='/webim.php?uid='+uid;
			}else{			
				if(uid == win.uid) {
					alert('您不能和自己聊天');
					return;
				}
				if(!win.in_array(uid, win.s_uids)){
					win.s_uids.push(uid);
					win.createUser(uid);
					win.getUserInfo(uid);
				}			
				win.displayTab(uid);
			}
		} catch(e) {   
			win.location='/webim.php?uid='+uid;
		}
		win.focus();
	}
}

function openImForA() {
	try	{ 
		win = window.open('', 'webim','height=555, width=955, top=100, left=100, toolbar=no, menubar=no, resizable=yes,location=no, status=no');
		if(win.uid == undefined){
			win.location='/webim.php';
		}else{
			if(uid == win.uid) {
				alert('您不能和自己聊天');
				return;
			}
			if(!win.in_array(uid, win.s_uids)){
				win.s_uids.push(uid);
				win.createUser(uid);
				win.getUserInfo(uid);
			}
			win.displayTab(uid);
		}
	} catch(e) {   
		win.location='/webim.php';
	}
	win.focus();
}

//黑名单
function delIMBlacklist(uid){
	var url = '/ajax/friend.php?type=delblacklist';
	f_uid = uid;
	var pars = 'friendid=' + uid;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:parseIMBlacklist});
}

function parseIMBlacklist(response){
	var resStr = response.responseText;
	if(resStr=="true"){
		var html = '<div class="mesbox"><div class="note" id="msgs_'+f_uid+'"></div></div>'+
		        '<div class="face">' +
		        '</div>'+
		        '<div class="send">'+
		          '<textarea name="content_'+f_uid+'" id="content_'+f_uid+'" rows="5" class="seinput"></textarea>'+
		          '<input id="send_'+f_uid+'" type="image" class="button01" onclick="javascript:sendMsg(\''+f_uid+'\');return false;" src="image/new/talk/send1.jpg" />'+
		        '</div>'+
		        '<div class="close">'+
		          '<input type="image" src="image/new/talk/talk2_21.gif" />'+
		          '<a href="#" target="_blank"><img src="image/new/talk/talk2_21-09.gif" alt="聊天记录" class="button03" /></a></div>';
		$('cnt_'+f_uid).innerHTML = html;
		readMsgs();    
	}else{
		alert('取消黑名单失败');
	}
}