// 以下是每页都会包含的自动请求代码(登录后自动请求)
var SUCCESS_JY = 0;
function refuseInviteTip(id, uname) {
	var ids = id + "_" + uname + "ref";
	$("#" + ids).remove();
	var params = {
		fid : id,
		acceptedUsername : uname
	};
	var url = URL + "/invitemsg!deleteMyInvite.action";
	$.post(url, params, null, 'json');
}

function callbackRequest(result) {
	var n = result.ret; // 4--没有登录
	var myinvite = result.myinvite;
	var inviteme = result.inviteme;
	if (myinvite != null) {
		for (var i = 0; i < myinvite.length; i++) {
			var acceptedId = myinvite[i].acceptedId; // 我邀请的人的id
			var acceptedUsername = myinvite[i].acceptedUsername; // 我所邀请人的uname
			var type = myinvite[i].type; // 0---初始状态 等待接受 1---接收了邀请
											// 2---拒绝了我的邀请

			var id = acceptedId + "_" + acceptedUsername + "ref";
			var content = "";
			if (type == 1) {
				var str1 = "<div id='"
						+ id
						+ "' class=\"chatinvitation_div\"><h5  class=chatinvitation_head>新消息关闭</h5><div class=chatinvitation_content>";
				content = "<b>"
						+ acceptedUsername
						+ "接收了您的聊天邀请</b><br><input type='button' onclick=\"refuseInviteTip("
						+ acceptedId
						+ ",'"
						+ acceptedUsername
						+ "')\" value='知道了'/><input type='button' value='关闭' onclick=\"refuseInviteTip("
						+ acceptedId + ",'" + acceptedUsername + "')\"/>";
				str1 += content;
				str1 += "<div></div>";
				$("body").append(str1);
				var mm = $("#" + id);
				mm.css("top","0px").css("left","0px");
				/*mm.animate({
							top : "12px"
						}, 3000);*/

			} else if (type == 2) {
				var str1 = "<div id='"
						+ id
						+ "' class=\"chatinvitation_div\"><h5  class=chatinvitation_head>新消息关闭</h5><div class=chatinvitation_content>";
				content = "<b>"
						+ acceptedUsername
						+ "拒绝了您的聊天邀请</b><br><input type='button' onclick=\"refuseInviteTip("
						+ acceptedId
						+ ",'"
						+ acceptedUsername
						+ "')\" value='知道了'/><input type='button' value='关闭' onclick=\"refuseInviteTip("
						+ acceptedId + ",'" + acceptedUsername + "')\"/>";
				str1 += content;
				str1 += "<div></div>";
				$("body").append(str1);
				var mm = $("#" + id);
				mm.css("top","0px").css("left","0px");
				/*mm.animate({
							top : "12px"
						}, 3000);*/
			}

		}
	}
	if (inviteme != null) {
		for (var i = 0; i < inviteme.length; i++) {
			var inviteId = inviteme[i].inviteId;
			var inviteUsername = inviteme[i].inviteUsername;
			var type = inviteme[i].type; // 0---初始状态 等待接受 1---接收邀请 2---拒绝邀请
			var date = inviteme[i].date;
			var id = inviteId + "_" + inviteUsername;

			var str1 = "<div id='"
					+ id
					+ "' class=\"chatinvitation_div\"><h5  class=chatinvitation_head>新消息关闭</h5><div class=chatinvitation_content>";
			var content = "您有新请求<br>请求者:"
					+ inviteUsername
					+ "<br>请求时间:"
					+ date
					+ "<br><input type='button' onclick=\"acceptInvite("
					+ inviteId
					+ ",'"
					+ inviteUsername
					+ "')\" value='接受'/><input type='button' value='拒绝' onclick=\"refuseInvite("
					+ inviteId + ",'" + inviteUsername + "')\"/>";
			str1 += content;
			str1 += "<div></div>";
			$("body").append(str1);
			var mm = $("#" + id);
			mm.css("top","0px").css("left","0px");
			/*mm.animate({
						top : "1px"
					}, 3000);*/
			setTimeout("removeDomById('" + id + "')", 25000); // 25秒后删除此DIV
		}
	}
	var userSummary = result.userSummary;
	if(userSummary != null && SUCCESS_JY == 0){
		
		//var singleMatchMsg = userSummary.singleMatchMsg;
		var newSystemMsg = userSummary.newSystemMsg;
		var newGeneralMsg = userSummary.newGeneralMsg;
		var newGoodGiftMsg = userSummary.newGoodGiftMsg;
		var newMailGiftMsg = userSummary.newMailGiftMsg;
		var addFriendsMsg = userSummary.addFriendsMsg;
		var username = userSummary.smallPhoto;
		if(newSystemMsg != 0 || newGeneralMsg != 0 || newGoodGiftMsg != 0 || newMailGiftMsg != 0 || addFriendsMsg != 0){
			
			var id = "system_tip_div_001";
			removeDomById(id);
		    var str1 = "<div id='"+ id + "' class=\"chatinvitation_div\"><h5  class=chatinvitation_head><span style='margin-left:2px'>提示</span><a style='margin-left:230px' href='javascript:void(0)' onclick=removeDomById('"+id+"')>关闭</a></h5><div class=chatinvitation_content>";
			var content = "<font size=10>";	
			if(newSystemMsg != 0){
				content += "收到【"+newSystemMsg+"】个新系统消息<br>";
			}
			if(newGeneralMsg != 0){
				content += "收到【"+newGeneralMsg+"】个新普通消息<br>";
			}
			if(newGoodGiftMsg != 0){
				content += "收到【"+newGoodGiftMsg+"】个新礼物<br>";
			}
			if(newMailGiftMsg != 0){
				content += "收到【"+newMailGiftMsg+"】个新招呼<br>";
			}
			if(addFriendsMsg != 0){
				content += "收到【"+addFriendsMsg+"】个好友请求<br>";
			}
			content += "<a href='"+URL+"/user!init.action?username="+username+"'>进入个人中心查看</a>";
			str1 += content+"</font>";
			str1 += "<div></div>";
			$("body").append(str1);
			var mm = $("#" + id);
			var left = $(document).width()-305;
			var top = $(window).height() + getTop() - mm.height()-5;
			mm.css("top",top).css("left",left);
			//setTimeout("removeDomById('" + id + "')", 30000); // 25秒后删除此DIV
		}
	}
}


function removeDomById(id) {
	if($("#" + id).length > 0){
	   $("#" + id).remove();
	}
}

function callbackAgreeFun(result) {

	var type = result.type;
	var ret = result.ret;
	if (ret == 1) {
		if (type == 1) {
			// var ur1 = URL + "/chat/chat.jsp";
			// window.open (ur1, 'chat', 'height=535, width=653, resizable=no,
			// top=50, left=10, toolbar=no, menubar=no,
			// scrollbars=no,location=no, status=no');
		}
	} else if (ret == 0) {
		// alert("接受邀请失败");
	} else if (ret == 4) {
		// alert("请先登录");
	} else if (ret == 3) {
		// alert("权限不够,开通会员");
		// var ur1 = URL+"/upgradeaccount!init.action";
		// window.open(ur1);
	} else if (ret == 5) {
		// alert("不能和自己聊天");
	} else {
		// /alert("失败了");
	}
}
function acceptInvite(id, uname) {
	var ids = id + "_" + uname;
	$("#" + ids).remove();
	var params = {
		fid : id,
		acceptedUsername : uname
	};
	var url = URL + "/invitemsg!addChatters.action";
	$.post(url, params, callbackAgreeFun, 'json');
	// alert("直接同意弹出");
	var ur1 = URL + "/chat/chat.jsp";
	window.open(ur1,'chat','height=535, width=653, resizable=no, top=50, left=10, toolbar=no, menubar=no, scrollbars=no,location=no, status=no');

}

function callRefuseInvite(result) {

}
function refuseInvite(id, uname) {

	var ids = id + "_" + uname;
	$("#" + ids).remove();
	var params = {
		fid : id,
		acceptedUsername : uname
	};
	var url = URL + "/invitemsg!refuseInvite.action";
	$.post(url, params, callRefuseInvite, 'json');
}

var INTERVAL = 30000, START_TIME;
function ajaxRequest() {
	var url = URL + "/invitemsg!read.action";
	var params = {};
	$.post(url, params, callbackRequest, 'json');

}

function ajaxRequest2() {
	ajaxRequest();
	START_TIME = setTimeout("ajaxRequest2()", INTERVAL);
}
 function openPay2(){
	  parent.removeOpenChatWin();
	  var ur1 = URL + "/form/pay.jsp";  //进行充值
	  window.open(ur1);
  }

function getTop(){
	return document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
}
function getLeft(){
	return document.documentElement.clientWidth?document.documentElement.clientWidth:document.body.clientWidth;
}
function openChatWindow(content,type) {
	removeOpenChatWin();
	var str1 = "";
	if(type == 1){
		str1 = "<div id='openchatwin_id' class=chatinvitation_div_tip><div  class=chatinvitation_head_tip><span class=chatinvitation_header_1>提示</span></div><div class=chatinvitation_content_tip>";
	}else if(type == 2){
		str1 = "<div id='openchatwin_id' class=chatinvitation_div_tip><div  class=chatinvitation_head_tip><span class=chatinvitation_header_1>提示</span><span class=chatinvitation_header_2 onclick=openChatWin(2)>&times;</span></div><div class=chatinvitation_content_tip>";
	}else{
		str1 = "<div id='openchatwin_id' class=chatinvitation_div_tip><div  class=chatinvitation_head_tip><span class=chatinvitation_header_1>提示</span><span class=chatinvitation_header_2 onclick=openChatWin(2)>&times;</span></div><div class=chatinvitation_content_tip_1>";
	}
	str1 += content;
	str1 += "<div></div>";
	$("body").append(str1);
	var thisid = $("#openchatwin_id");
	var top = getTop()+220;
	var left = (getLeft()-100)/2;
	thisid.css("top", top).css("left", left);
	thisid.show();

}
function removeOpenChatWin() {
	if ($("#openchatwin_id")[0]) {
		$("#openchatwin_id").remove();
	}
}

function openChatWin(type) {
	if (type == 1) {
		var ur1 = URL + "/chat/chat.jsp";
		window
				.open(
						ur1,
						'chat',
						'height=535, width=653, top=50, left=10, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no');
	}
	removeOpenChatWin();
}
function callbackInviteChat(result) {
	var n = result.ret; // 0---发送邀请失败 1---发送邀请成功 2--已经在聊了 3--权限不够升级会员 4--没有登录
						// 5--不能和自己聊天
	var type = result.type; // 是否需要打开聊天窗口 1--需要 2--不需要
	if (n == 0) {
		alert("发送邀请失败");
	} else if (n == 3) {
		var content = "<a href=javascript:void(0) onclick=openUpgrade()>权限不够升级会员</a>";
		openChatWindow(content,2);

	} else if (n == 2) {
		if (type == 1) {
			var content = "<a href=javascript:void(0) onclick=openChatWin(1)>聊天邀请发送成功</a>";
			openChatWindow(content,2);
		}
	} else if (n == 1) {
		var content = "";
		if (type == 1) {
			content = "<a href=javascript:void(0) onclick=openChatWin(1)>聊天邀请发送成功</a>";
			openChatWindow(content,1);
		} else {
			content = "<a href=javascript:void(0) onclick=openChatWin(2)>聊天邀请发送成功</a>";
			openChatWindow(content,2);
		}
	} else if (n == 4) {
		showLogin();
	} else if (n == 5) {
		alert("不能和自己聊天");
	} else {
		alert("参数错误");
	}
}

function inviteChat(userid, username, myname) {
	if (myname == '') {
		showLogin();
	} else if (username == myname) {
		alert("不能和自己聊天");
	} else {
		var params = {
			fid : userid,
			acceptedUsername : username
		};
		var url = URL + "/invitemsg!send.action";
		$.post(url, params, callbackInviteChat, 'json');
	}

}

function openUpgrade() { // MyfriendsInviteChat = 3;
	var ur1 = URL + "/upgradeaccount!init.action";
	window.open(ur1);
	removeOpenChatWin();
}

// 自动登录
function autoLoginCallback(result) {
	var n = result.msg;
	// 1--用户名或密码错误 3--登录成功
	if (n == 3) {
		var username = result.username;
		//var str = "<li>" + username + "&nbsp;&nbsp;欢迎回来！<a href='" + URL+ "/user!init.action?username=" + username+ "'>进入个人中心</a></li>";
		//str += "<li><a href='" + URL + "/out!exit.action'>退出</a></li>";
		var str = "<li class=\"menu1-personal\">"+username+"&nbsp;欢迎回来!&nbsp;&nbsp;<b id=\"copy_my_links_2\">复制我的支持链接</b><span><a href='" + URL+ "/user!init.action?username=" + username+ "'>个人中心</a><a href='" + URL + "/out!exit.action'>退出</a></span></li>";
		$("#login_html").html(str);
	}
}
function autoLogin() {
	var autourl = URL + "/autologin!autoLogin.action";
	var username = $.cookie('jy_autouser');
	if (username != null) {
		var par = {
			username : username
		};
		$.post(autourl, par, autoLoginCallback, 'json');
	}
}

function replaceAll2(streng, soeg, erstat) {
	var st = streng;
	if (soeg == "")
		return st;
	var idx = st.indexOf(soeg);
	while (idx >= 0) {
		st = st.substring(0, idx) + erstat + st.substr(idx + soeg.length);
		idx = st.indexOf(soeg)
	}
	return st
}

function setSystem_Tip_Div(){
	var div_s = $("#system_tip_div_001");
	if (div_s[0]) {
		var left = $(document).width()-305;
		var top = $(window).height() + getTop() - div_s.height()-5;
		div_s.css("top",top).css("left",left);
	}
	setTimeout("setSystem_Tip_Div()", 50);
}
$(document).ready(function() {
	        setTimeout("ajaxRequest2()", 3000);
			autoLogin();
			setSystem_Tip_Div();
		})
