$(function() {
	$("#CommonChat").draggable({handle: '.ChatAvatar'});
});
function showDialog(sender, uid) {
	try {
		window.clearInterval(autoReload);
	} catch(e) {
		
	}
	if (sender == "" || sender == 0) {
		alert('未登陆用户不能使用本站M知道即时聊天功能');
	} else if (sender == uid) {
		alert('您没必要同自己聊天');
	} else {
		$("#CommonChat").css("top", document.documentElement.scrollTop - -38);
		$("#CommonChat").css("display", "");
		$("#ChatContent").html("<p><img src=\"images/icon_loading.gif\" /></p><p>读取中，请稍候</p>");
		$.get("scripts/common_chat.php?Reciever=" + uid + "&SessionId=" + Math.random(), function(data) {
			$("#ChatContent").html(data);
			document.getElementById("ChatInfoContent").scrollTop = document.getElementById("ChatInfoContent").scrollHeight;
			autoReload = setInterval(function() {
								 	reloadMessege(uid)
								 }, 2000);
		});
	}
}
function checkEnter(uid) {
	if (window.event.ctrlKey && window.event.keyCode == 13) {
		document.formchat.content.value += "\n";
	} else if (window.event.keyCode == 13) {
		sendMessege(uid);
	}
}
function CloseDialog() {
	$("#CommonChat").css("display", "none");
	window.clearInterval(autoReload);
}
function sendMessege(uid) {
	var f = document.formchat;
	if (f.content.value == "") {
		alert('请输入聊天内容');
	} else {
		$.post("scripts/Ajax.php?action=SendMessege&Reciever=" + uid + "&SessionId=" + Math.random(), { content: f.content.value }, function(data) {
			f.content.value = "";
			reloadMessege(uid);
		});
	}
}
function reloadMessege(uid) {
	var sender = "{/literal}{$smarty.cookies.AmiUserId}{literal}";
	$.get("scripts/common_chat.php?action=reload&Reciever=" + uid + "&SessionId=" + Math.random(), function(list) {
		$("#ChatInfoContent").html(list);
		document.getElementById("ChatInfoContent").scrollTop = document.getElementById("ChatInfoContent").scrollHeight;
	})
}

