$(document).ready(function(){
	$.ajax({
		type : "POST",
		dataType: "json",
		url : "getOnlineCustomService.do",
		data : "",
		success : function(result) {
			var html=" <div id=\"onlineKf\" style=\"width:200px;display:none;POSITION: absolute; TOP: 200;left:expression(this.offsetParent.clientWidth-300)\">";
			html+="<div style=\"clear:both\" id=\"kf_tou\"><table width=\"200\" height=\"36\" border=\"0\"  cellspacing=\"0\" cellpadding=\"0\"><tr><td background=\"../images/kf_tou.gif\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"78%\">&nbsp;</td><td width=\"36\" height=\"23\"><a href='#' onclick=\"$('#onlineKf').hide();return false;\" style='color:#000000;text-decoration:none;font-size:12px;'><img src=\"../images/touming.png\"  border=\"0\" /></a></td> </tr></table></td></tr></table></div>";
			html+="<div style=\"background:#e9f2f7; font-size:12px; clear:both; height:25px; line-height:25px\">客服在线["+result.length+"]</div>";
			html+="<div style=\"height:295px; overflow-y:scroll;scrollbar-base-color:#e0f1fa;\">";
			if(result.length>0){
				for(var i=0;(i<result.length||i<5);i++){
					html+="<div style=\"background-image:url(images/kf_bg_line.gif); height:59px;line-height:65px; padding-left:8px\">";
					if(i<result.length){
						html+="<img style=\"cursor:pointer\" src=\"images/kf_face.gif\" onclick=\"connectCustomService('"+result[i].cuid+"','"+result[i].cuname+"');\" /><font style=\"font-size:12px\">"+result[i].cuname+"</font>";
					}
					html+="</div>";
					//html+="<div style=\"cursor:pointer\" onclick=\"connectCustomService('"+result[i].cuid+"');\">"+result[i].cuname+"</div>"
				}
			}
			else{
				html+="<div style=\"background-image:url(images/kf_bg_line.gif); height:59px;line-height:65px; padding-left:8px\">";
				html+="<img style=\"cursor:pointer\" src=\"images/kf_face.gif\" onclick=\"connectCustomService('robot','robot');\" /><font style=\"font-size:12px\">智能机器人</font>";
				html+="</div>";
				for(var i=0;i<4;i++){
					html+="<div style=\"background-image:url(images/kf_bg_line.gif); height:59px;line-height:65px; padding-left:8px\">";
					html+="</div>";
				}
				//html="<div style=\"cursor:pointer\" onclick=\"connectCustomService('robot');\">联系客服</div>";
			}
			html+="</div><div><img src=\"images/kf_wei.gif\"/></div></div>";
			$("body").append(html);
			
			$("#onlineKf").draggable({
				handle : '#kf_tou'
			});
		}
	});
});
function connectCustomService(cuid,cuname){//$("#onlineKf").draggable({ handle: '#kf_tou' });
	
	if(!document.getElementById('dialog'+cuid)){	
		var dialog='<div id="dialog'+cuid+'" style=\"display:none;\"></div>';
		$("body").append(dialog);
		$('#dialog'+cuid).html('<div><textarea id="msgHistory'+cuid+'" rows="18" cols="65" readonly="readonly" style="height:auto"></textarea></div><div style="float:left"><input id="msgInput'+cuid+'" type="input" size="50" onkeydown="pressEnter();"></input><input id="send" onclick="talkToCustomService(\''+cuid+'\');" type="button" value="发送" style="height:25px"/><input id="sendImg" onclick="sendImg(\''+cuid+'\',\''+cuname+'\');" type="button"  value="发送文件" style="height:25px; width:71"/></div>');
		$('#dialog'+cuid).attr('title','<img src="Framework/Images/icon_dialog.gif" align="absmiddle"/>在线服务');
		$("#msgInput"+cuid).focus();
		if(cuid=="robot"){
			$("#sendImg").remove();
			$("#msgInput"+cuid).attr("size",60);
			$("#msgInput"+cuid).autocomplete({
				source:function(request,response){
					$.ajax({
						type : "POST",
						url:"perpareMessageRobot.do", 
						dataType: "json",
						data:"keyword="+request.term,
						success:function(data){
							response($.map(data,function(item){
								return {
									label:item.crquestion,
									value:item.crquestion
								}
							}));
						}
					});
				}
			});
		}
	}
	$('#dialog'+cuid).dialog({
		resizable: false,
		height:360,
		width:500, 
		modal:true,
		hide: 'clip',
		close:function (event,ui){
			$('#dialog'+cuid).dialog('destroy');
		}
	});
	if(cuid!="robot"){
		setInterval("getCustomServiceMessage('"+cuid+"')",2000);
	}
}
function talkToCustomService(cuid){
	var msg=$("#msgInput"+cuid).val();
	if(msg==""){
		alert("消息不允许为空！");
		$("#msgInput"+cuid).focus();
		return false;
	}

	if(cuid=="robot"){
		$.ajax({
			type : "POST",
			dataType : "json",
			url : "messageRobot.do",
			data : "question="+msg,
			success : function(result) {
				$("#msgHistory"+cuid).val($("#msgHistory"+cuid).val()+"游客："+result[0].datetime+"\n"+result[0].question+"\n");
				$("#msgHistory"+cuid).val($("#msgHistory"+cuid).val()+"机器人："+result[0].datetime+"\n"+result[0].answer+"\n");
				$('#msgHistory'+cuid).attr('scrollTop',$('#msgHistory'+cuid).attr('scrollHeight'));
				$("#msgInput"+cuid).val("");
				$("#msgInput"+cuid).focus();
			}
		});
	}
	else{
		$.ajax({
			type : "POST",
			dataType:"json",
			url : "talkToCustomService.do",
			data : "cmcontext="+msg+"&cuid="+cuid,
			success : function(result) {
				$("#msgHistory"+cuid).val($("#msgHistory"+cuid).val()+"游客："+result[0].cmcreatetime+"\n"+result[0].msgcontent+"\n");
				$('#msgHistory'+cuid).attr('scrollTop',$('#msgHistory'+cuid).attr('scrollHeight'));
				$("#msgInput"+cuid).val("");
				$("#msgInput"+cuid).focus();
			}
		});
	}
	
}
function getCustomServiceMessage(cuid){
	$.ajax({
		type : "POST",
		dataType:"json",
		url : "getCustomServiceMessage.do",
		data : "cuid="+cuid,
		success : function(msg) {
			if(msg){
				$("#msgHistory"+cuid).val($("#msgHistory"+cuid).val()+msg[0].username+"："+msg[0].cmcreatetime+"\n"+msg[0].cmcontext+"\n");
				$('#msgHistory'+cuid).attr('scrollTop',$('#msgHistory'+cuid).attr('scrollHeight'));
				$("#msgInput"+cuid).val("");
			}
		}
	});
}
function pressEnter(){
	var e=event.srcElement;     
	if(event.keyCode==13){     
		document.getElementById("send").click();   
	}     
}

function sendImg(cuid,cuname){
	
	$.ajax({
			type:"post",
			url:"/checkLg.do",
			dataType:"text",
			async: false,
			success:function(msg){
  				if(msg=="null"){
					 if(confirm("请先登录，现在是否登录？"))
						  {
						  location.href="/";
						  }
						  else
						  {
						  
						  }	
				}else{
					
					sendImg1(cuid,cuname);
				}
  			}
  		});
	
	
}


function sendImg1(cuid,cuname){
	
	
	if(!document.getElementById('news'+cuid)){	
		var dialog='<div id="news'+cuid+'" style=\"display:;\"></div>';
		$("body").append(dialog);
	}
	var url = "/addCmsNews.do?cuid="+cuid+"&cuname="+cuname+"&sendImg=sendImg";
	$('#news'+cuid).attr('title','<img src="Framework/Images/icon_dialog.gif" align="absmiddle"/>');
	$('#news'+cuid).html('<iframe height="100%" width="100%" src="'+url+'" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto"></iframe>');
	//try{
		$('#news'+cuid).dialog({
			resizable: false,
			height:530,
			width:700, 
			modal:true,
			hide: 'clip',
			close:function (event,ui){
				$('#selectuser').dialog('destroy');
			}
		});
	
}

