var imServer = getParameter("imserver");
var lvid = getParameter("lvid");
var tolvid = getParameter("tolvid");
var groupid = getParameter("groupid");
var teamId = getParameter("teamid");
var sign = getParameter("sign");
imServer = getImServer(imServer);
var ADDTYPE = {
	"friend": 0,
	"group": 1
}
var addType = groupid ? 1 : 0;
var userOut, userOutStr, remark, groupTeam;
$(function() {
	var params = {
		'lvid': lvid,
		'tolvid': tolvid,
		'groupid': groupid,
		'sign': sign
	};
	var reqUrl = addType == ADDTYPE.friend ? requestUrlMap['userInfo']['url'] : requestUrlMap['cloudGroupInfo']['url'];
	$.getJSON(reqUrl, params, callbackMethod);
});

function callbackMethod(data) {
	if (addType == ADDTYPE.friend) {
		printFriend(data);
		if(lvid == tolvid){
			$('.text-info').hide();
			$('#addfir-info').show().css({'width':'140px','margin-left':'70px'}); 
			$('footer.isfir_foot').show();
			$('#addfir-info').html('<p class="areaInfo">不能添加自己为好友。</p>')
			$('p.areaInfo').css({'margin-top':'122px','width':'140px'});
			$('#isfirtalk_btn').hide();
		}
		if (data.isfriend == 1) {
			$('.text-info').hide();
			$('#addfir-info').show();
			$('footer.isfir_foot').show()
			$('#isfirtalk_btn').click(function() {
				privateChatfir(data)
				$("#close_btn").click()
			})
		}
	} else {
		printGroup(data);
		if (data.ismember == 1) {
			$('#addgroup-info').show();
			$('.text-info').hide();
			$('footer.isfir_foot').show()
			$('#addgroup-pro').hide()
			$('#isfirtalk_btn').click(function() {
				privateChatgroup()
				$("#close_btn").click()
			})
		}
		if (data.groupinfo.verifytype == 2) {
			$('.text-info').hide();
			$('footer.isfir_foot').show()
			$('#isfirtalk_btn').hide()
			$('#addgroup-info').hide();
			$('#addgroup-pro').show()
		}
	}
}

function printFriend(result) {
	$(".noList").hide();
	var city, district;
	var userOut = result.userinfo;
	if(result.isfriend){
		var team = result.userinfo.team;
		if (team.length > 6) {
			$("#s_cities").css('overflow-y', 'scroll');
		}
		for (var i = 0; i < team.length; i++) {
			if(team[i].id == userOut.FteamId){
				teamName = team[i].teamName;
			}
			$("#s_cities").append("<dd> <span class=\"currency_type\" groupid=" + team[i].id + ">" + team[i].teamName + "</span> </dd>");
		}
		$('#cityA').text(teamName).attr('groupid',userOut.FteamId);
	}
	$("#s_cities dd").each(function(e,info){
		if($(info).find('span').attr('groupid') == -2){
			$(info).hide();
		}
	})
	var city = userOut['city'] ? userOut['city'] : "保密";
	var district = userOut['district'] ? userOut['district'] : "";
	var imgurl = userOut['headurl'];
	if (imgurl == '') {
		imgurl = userOut['sex'] == 0 ? "../images/classic/HEAD-gg-96.png" : "../images/classic/HEAD-mm-96.png";
	}
	var sex_ = "";
	if (userOut['sex'] == 0) {
		sex_ = "男";
	} else if (userOut['sex'] == 1) {
		sex_ = "女";
	};
	$('#addfir-info p span').text(userOut.nickname);
	$('#addfir-info p span').attr('onclick','lookUserView('+ tolvid +','+ lvid +')')
	imgurl = imgurl.replaceAll('-64', '-96');
	$(".addFrirendMsg_pic").attr("src", imgurl = imgurl.replaceAll('-60', '-96'));
	$(".addFrirendMsg_name").text(userOut.nickname);
	$(".addFrirendMsg_num").html(userOut.lvid);
	$(".addFrirendMsg_sex").html(sex_);
	$(".addFrirendMsg_age").html(userOut.age);
	$(".addFrirendMsg_place").html(city + " " + district).attr('title', city + " " + district); //city为城市而district为地区
	$(".addFrirendMsg_job").text(userOut.profession);
	$(".addFrirendMsg_name").attr("title", userOut.nickname);
	$(".addFrirendMsg_job").attr("title", userOut.profession);
	$('#raname').val(userOut.remark)
	raname = userOut.remark;
	$("#addFrirendMsg").show();
	$(".de_footer").show();
	$("#loading-popup").hide();
	$(".addFrirendMsg_name,article .addFrirendMsg_pic").click(function() {
		lookUserView(userOut.lvid, userOut.userid)
	})
}
//个人资料
function lookUserView(lvid, userid) {
	var params = "{command:1003,params:{userid:" + userid + ",lvid:" + lvid + "}}";
	var resultS = sendCommand(params);
}
//云组资料
function lookGroupView(groupid) {
	var params = "{command:3002,params:{groupid:" + groupid + "}}";
	var resultS = sendCommand(params);
}
//查找云组私聊
function privateChatgroup() {
	var params = "{command:3009,params:{groupid:" + groupid + "}}"
	var resultS = sendCommand(params);
}
//查找个人私聊
function privateChatfir(data) {
	var params = "{command:1011,params:{userid:" + data.userinfo.userid + ",lvid:" + tolvid + ",nickname:" + data.userinfo.nickname + "}}";
	var resultS = sendCommand(params);
}
$('.createG').click(function(){
	var params = "{command:1014,params:{lvid:" + lvid + "}}";
	var resultS = $.parseJSON(sendCommand(params));
	if(resultS['errorid'] == 0){
		$('#cityA').text(resultS.groupname).attr('groupid',resultS.groupid);
		$('#s_cities').append('<dd> <span class="currency_type" groupid='+ resultS.groupid +'>'+ resultS.groupname +'</span> </dd>');
	}
})
//触发关闭按钮

$('#close_btn').click(function(){
	var params = "{command:9001,params:''}";
	var resultS = sendCommand(params);
})
$("#isfirclose_btn").click(function() {
	var updateNickName = $('#raname').val();
	var groupid = $('#cityA').attr('groupid');
	if(lvid == $('.addFrirendMsg_num').text()){
		$('#close_btn').click();
	}
	if($('#addFrirendMsg').is(':hidden')){
		$('#close_btn').click();
		return;
	}
	if(updateNickName == raname && groupTeam == $('#cityA').text()){
		$('#close_btn').click();
		return;
	}
	if( updateNickName != raname ){
		var params = {
			lvid:lvid,
			tolvid:tolvid,
			friendnickname:updateNickName,
			sign:sign
		}
		$.getJSON(requestUrlMap['friendName']['url'],params,function(data){
			if(data.errorid == 0){
				var params = '{command:1015,params:{"tolvid":' + tolvid + ',"updaterename": '+ updateNickName +' ,"teamid": '+ groupid +' }}';
				var result = $.parseJSON(sendCommand(params));
				if(result.errorid == 0){
					$('#close_btn').click();
				}
			}
		});
	}
	if( groupTeam != $('#cityA').text() ){
		var params = {
			lvid:lvid,
			tolvid:tolvid,
			friendgroupid:groupid,
			sign:sign
		}
		$.getJSON(requestUrlMap['updateFriendGroup']['url'],params,function(data){
			if(data.errorid == 0){
				var params = '{command:1015,params:{"tolvid":' + tolvid + ',"updaterename": '+ updateNickName +' ,"teamid": '+ groupid +' }}';
				var result = $.parseJSON(sendCommand(params));
				if(result.errorid == 0){
					$('#close_btn').click();
				}
			}	
		})
	}
}) 

//触发发送按钮
$("#send_btn").click(function() {
	if (addType == ADDTYPE.friend) {
		sendAddUserNotice();
	} else {
		sendAddGroupNotice();
	}
})
//加好友验证通知
function sendAddUserNotice() {
	var f_msg = $("#testingMsg_Friend").val();
	if (f_msg == "") {
		f_msg = "申请加我为好友";
	}

	var params = {
		'lvid': lvid,
		'noticetype': 1,
		'receiverlvid': tolvid,
		'sign': sign,
		'verifymsg': encodeURIComponent(f_msg),
		'supergroupid':teamId
	};
	$.getJSON(requestUrlMap['groupNotice']['url'], params, createsNoticeCallBack);
}

function createsNoticeCallBack(data) {
	if (data.errorid != 0) {
		var paramStr = "{title:'提示',cat:0,content:'" + data['errmsg'] + "！'}";
		var params = "{command:9006,params:" + paramStr + "}";
		var resultS = sendCommand(params);
		return;
	} else {
		//关闭验证框
		var params = "{command:9001}";
		var resultS = sendCommand(params);
	}
}

function printGroup(result) {
	$(".noList").hide();
	$('#addgroup-pro').hide()
	var groupArr = result.groupinfo;
	var imgurl = groupArr['iconurl'];
	if (imgurl == '') {
		imgurl = "../images/classic/HEAD-Group-96.png";
	}
	var crator_name = result.creatorinfo;
	$(".addGroupMsg_pic").attr({"src": transferHeadSize(imgurl, 96),'onclick':'lookGroupView('+ groupid +')'});
	$(".addGroupMsg_name").text(groupArr.cloudgroupname);
	$(".addGroupMsg_num").html(groupArr.groupid);
	$(".addGroupMsg_harem").text(crator_name.nickname);
	$(".addGroupMsg_list").html(groupArr.num);
	$(".addGroupMsg_name").attr("title", groupArr.cloudgroupname);
	$(".addGroupMsg_harem").attr("title", crator_name.nickname);
	$("#addGroupMsg").show();
	$(".de_footer").show();
	$("#loading-popup").hide();
	$(".addGroupMsg_name").click(function() {
		lookGroupView(groupid)
	})
};
//加群组验证通知
function sendAddGroupNotice() {
	var g_msg = $("#testingMsg_Group").val();
	var params = {
		'lvid': lvid,
		'noticetype': 2,
		'groupid': groupid,
		'sign': sign,
		'verifymsg': encodeURIComponent(g_msg),
		'supergroupid':teamId
	};
	$.getJSON(requestUrlMap['groupNotice']['url'], params, createNoticeCallBack);
}
//加群组验证通知的回调
function createNoticeCallBack(data) {
	if (data.errorid != 0) {
		var paramStr = "{title:'提示',cat:0,content:'" + data['errmsg'] + "！'}";
		var params = "{command:9006,params:" + paramStr + "}";
		var resultS = sendCommand(params);
		return;
	} else {
		var paramStr = "{title:'提示',cat:0,content:'加组申请发送成功！'}";
		var params = "{command:9006,params:" + paramStr + "}";
		var resultS = sendCommand(params);
		var params = "{command:9001}";
		var resultS = sendCommand(params);
	}
}
$('.TareaSpanj').show();
$('textarea').keyup(function() {
	var thisval = $(this).val()
	$('textarea').focus(function() {
		$(this).val(thisval)
	})
})
$('textarea').blur(function() {
	$('.TareaSpanj').show();
	$('textarea').css({
		'border': '1px solid #00ABE8'
	})
	if ($(this).val() == '我是') {
		$(this).val('')
		$('.TareaSpanj').show();
	} else {
		$('.TareaSpanj').hide();
	}
})
$('textarea').focus(function() {
	$('textarea').css({
		'color': '#000',
		'border': '1px solid #00ABE8'
	})
	$('.TareaSpanj').hide();
	$(this).val('我是')
})
$('.TareaSpanj').click(function() {
	$(this).hide();
	$('textarea').focus()
})
//title
$('body').on('mouseover', function() {
	getalt();
})
$('body').on('mouseout', function() {
	restorealt();
})
$(".cities-open").click(function() {
	$(this).addClass('citiesact');
	$('.select_list').toggle();
	$('.cityBox .open_select_box').toggleClass('actSelect');
});
$(".cityBox").on('click','.currency_type',function() {
	var test = $(this).text();
	$('#cityA').text(test);
	$('.select_list').hide();
	$('#cityA').attr('groupid',$(this).attr('groupid'));
	$('.cityBox .open_select_box').removeClass('actSelect');
	$('.cities-open').removeClass('citiesact');
	$('.dressBox a.inputBox').css('border-color','#d2d2d2');
})
$(document).bind("click", function(e) {
	var target = $(e.target);
	//隐藏地址弹窗
	if (target.closest(".cityBox").length == 0) {
		$('.select_list').hide();
		$(".cities-open").removeClass('citiesact');
		$(".open_select_box").removeClass('actSelect');
	}
})