var imServer = getParameter("imserver");
imServer = getImServer(imServer);
var lvid = getParameter("lvid");
var tag = getParameter("tag");
var sign = getParameter("sign");
var key = $.trim(getParameter("key"));
var teamid = getParameter("teamid");
var userOut, userOutStr;
var params;
var pageSizeG;
var reCache;
var reCurrentPage = 1;
var reTotalPage = 0;
$(document).ready(function() {
	if (!key) {
		if (tag == 0) {
			$('.findMain').css('background','none');
			$('.Recommend').show();
			recomm();
		}else{
			$('.findMain').addClass('emptybg');
			$(".noList").html("暂无搜索").show();
		}
		return;
	}
	$("#keyTip").text(key);
	params = {
		'lvid': lvid,
		'key': key,
		'pagesize': 27,
		'pageno': 1,
		'sign': sign
	};
	if (tag == 1) {
		searchGroup();
	} else {
		searchUser();
	}
});
function scrollfun() {
	var cont_hei = $('.findPList').height();
	var info_hei = $('.findPList ul').height() * $('.findPList ul').length;
	if (info_hei > cont_hei) {
		$('.findPList').css({
			'overflow': 'scroll',
			'overflow-x': 'hidden'
		});
	} else {
		$('.findPList').css('overflow', 'hidden');
	}
	var cont_heiG = $('.findGList').height();
	var info_heiG = $('.findGList ul').height() * $('.findGList ul').length;
	if (info_heiG > cont_heiG) {
		$('.findGList').css({
			'overflow': 'scroll',
			'overflow-x': 'hidden'
		});
	} else {
		$('.findGList').css('overflow', 'hidden');
	}
}

function searchUser() {
	$("#userMess").html("");
	$(".noList").html("正在查询请稍候...").show().addClass('top');
	$('.showPage').hide();
	$('.findPList').css('overflow', 'hidden');
	$.getJSON(requestUrlMap['searchFriend']['url'], params, searchUserCallBack);
}

//提交的回调函数
function searchUserCallBack(result) {
	$('.findPeople .searchBack,.findPList').show();
	$('.findPeople .searchBack span b').text($('#key').val());
	$('.Recommend').hide();
	$('.pageFi').show();
	if (result.count == 0) {
		$('.findMain').addClass('emptybg');
		$('.findPeople .searchBack').hide();
		$(".noList").html("暂无搜索").show().removeClass('top');
		$('.findPList').css('overflow', 'hidden');
		$('.showPage').hide();
		return;
	}
	$('.findMain').removeClass('emptybg');
	$(".noList").hide();
	$('.showPage').show();
	var data = result.data;
	var pageTotal = Math.ceil(result.count / 27);
	var pagecon = pageTotal == 0 ? 1 : pageTotal;
	var html = "";
	var friLvid;
	var friendUser, city, district, imgurl, nickNameTran;
	for (var i = 0; i < data.length; i++) {
		friendUser = data[i];
		city = friendUser['city'] ? friendUser['city'] : "保密";
		district = friendUser['district'] ? friendUser['district'] : "";
		imgurl = transferHeadSize(friendUser['headurl'], 64);
		if (friendUser['sex'] == 0) {
			sexinfo = "<img src='../images/blue/sex_nan.png'>";
			if (imgurl == '') {
				imgurl = "../images/classic/HEAD-gg-64.png";
			}
		} else {
			sexinfo = "<img src='../images/blue/sex_nv.png'>";
			if (imgurl == '') {
				imgurl = "../images/classic/HEAD-mm-64.jpg";
			}
		}
		friLvid = !isNaN(friendUser['lvid']) ? friendUser['lvid'] : $(friendUser['lvid']).text();
		html += "<ul class='clearfix'>";
		html += "<li>";
		html += "<div class=\"headImg\" onclick=\"lookUserView(" + friLvid + "," + lvid + ")\">";
		html += "<div class=\"img-find\">";
		if (friendUser['isonline'] == 0) {
			html += "<img src='" + imgurl + "' onerror=\"javascript:this.src='../images/face0.png'\" />";
		} else {
			html += "<img src='" + imgurl + "' />";
		}
		html += "</div>";
		html += "<div class=\"img-find-warp\"></div>";
		html += "</div>";
		html += "<dl>"
		html += "<dt><span onclick=\"lookUserView(" + friLvid + "," + lvid + ")\" class=\"findName\">" + friendUser['nickname'] + "<a></span><span>(" + friendUser['lvid'] + ")</span></a></dt>";
		html += "<dd>";
		html += "<p class='addfirinfo'>" + sexinfo + friendUser['age'] + "岁" + "<i class='split'></i><span>" + city + "</span><span>&nbsp;</span><span>" + district + "</span></p>";
		html += "<div><span class='add' onclick='addMyFriend(" + friLvid + ")'>好友</span></div>";
		html += "</dd>";
		html += "</dl>";
		html += "</li>";
		html += "</ul>";
	}
	$("#userMess").html(html);
	printPage(pageTotal, result);
	scrollfun();
}

//加好友
function addMyFriend(tolvid) {
	var params = "{command:1001,params:{lvid: " + tolvid + ",teamid: " + teamid + "}}";
	var resultS = sendCommand(params);
}

function lookUserView(tolvid, userid) {
	var params = "{command:1003,params:{userid:" + userid + ",lvid:" + tolvid + "}}";
	var resultS = sendCommand(params);
}

function searchGroup() {
	$("#groupMess").html("");
	$(".noList").html("正在查询请稍候...").show().addClass('top');
	$('.showPage').hide();
	$('.findPList').css('overflow', 'hidden');
	pageSizeG = params['pagesize'] = 26;
	$.getJSON(requestUrlMap['searchGroup']['url'], params, searchGroupCallBack);
}
//提交的回调函数
function searchGroupCallBack(result) {
	$('.findGroup').show();
	$('.findGroup .searchBack').show();
	$('.findGroup .searchBack span b').text(key);
	var groupArr = result.data;
	var pageTotal = Math.ceil(result.count / pageSizeG);
	var pagecon = pageTotal == 0 ? 1 : pageTotal;
	if (groupArr.length == 0) {
		$("#groupMess").html("");
		$('.findMain').addClass('emptybg');
		$('.findGroup .searchBack,p.left,p.right').hide();
		$(".noList").html("暂无搜索").show().removeClass('top');
		$('#groupMess').css('overflow', 'hidden');
		$('.showPage').hide();
		return;
	}
	$(".noList").hide();
	$('.pageG').show();
	var html = "";
	var sGroup, grouNameTran, imgurl;
	for (var i = 0; i < groupArr.length; i++) {
		sGroup = groupArr[i];
		imgurl = sGroup['iconurl'] ? sGroup['iconurl'] : "../images/classic/HEAD-Group-60.png";
		html += "<ul class='clearfix'>";
		html += "<li class='clearfix'>";
		html += "<div class=\"findGImg\" onclick=\"lookGroupView(" + sGroup['groupid'] + ")\">";
		html += "<div class=\"img-find\">";
		html += "<img src='" + imgurl + "'  />";
		html += "</div>";
		html += "<div class=\"img-find-warp\"></div>";
		html += "</div>";
		html += "<dl>";
		html += "<dt>";
		html += "<span onclick=\"lookGroupView(" + sGroup['groupid'] + ")\" class=\"findName\">" + sGroup['cloudgroupname'] + "</span>";
		html += "</dt>";
		html += "<dd>" + sGroup.noticedata + "</dd>";
		html += "</dl>";
		html += "</li>";
		html += "<p><img src='../images/addgroup.png'><span style='color: #7f7f7f;'>" + sGroup['num'] + "/500</span> <span class='add' onclick='addMyGroup(" + sGroup['groupid'] + ")'>加组</span></p>";
		html += "</ul>";
	}
	$('.findPeople').hide();
	$("#groupMess").html(html);
	$('.findGList ul:even').css('margin-right', '22px');
	printPage(pageTotal, result);
	scrollfun();
}

$('.Recommend p.right').click(function() {
	if (++reCurrentPage <= reTotalPage) {
		recomm();
	}
});

$('.Recommend p.left').click(function() {
	if (reCurrentPage-- >= 1) {
		recomm();
	}
});

function recomm() {
	if (reCache) {
		printRecomm();
		return;
	}
	var recommparams = {
		lvid: lvid,
		sign: sign,
		pageno: 1,
		pagesize: 45
	}
	$.getJSON(requestUrlMap['recommend']['url'], recommparams, function(result) {
		reTotalPage = Math.ceil(result.data.length / 9);
		reCache = result.data;
		printRecomm();
		$('.Recommend').css('background','#fff');
	});
}

function printRecomm() {
	if (reCache.length < 9) {
		$('.Recommend p.left,.Recommend p.right').hide();
	} else {
		if (reCurrentPage <= 1) {
			$('.Recommend p.left').hide();
		} else {
			$('.Recommend p.left').show();
		}
		if (reCurrentPage >= reTotalPage) {
			$('.Recommend p.right').hide();
		} else {
			$('.Recommend p.right').show();
		}
	}
	$(".noList").hide();
	var html = "";
	var friendUser, city, district, imgurl, nickNameTran;
	if(reCache.length >0){
		$('.Recommend h2').show();
	}
	var start = (reCurrentPage - 1) * 9;
	var end = start + 9;
	end = end > reCache.length ? reCache.length : end;
	for (var i = start; i < end; i++) {
		friendUser = reCache[i];
		city = friendUser['city'] ? friendUser['city'] : "保密";
		district = friendUser['district'] ? friendUser['district'] : "";
		imgurl = friendUser['headurl'];
		if (friendUser['sex'] == 0) {
			sexinfo = "<img src='../images/blue/sex_nan.png'>";
			if (imgurl == '') {
				imgurl = "../images/face0.png";
			}
		} else {
			sexinfo = "<img src='../images/blue/sex_nv.png'>";
			if (imgurl == '') {
				imgurl = "../images/face1.png";
			}
		}
		html += "<ul class='clearfix'>";
		html += "<li>";
		html += "<div class=\"headImg\" onclick=\"lookUserView(" + friendUser['lvid'] + "," + friendUser['userid'] + ")\">";
		html += "<div class=\"img-find\">";
		if (friendUser['isonline'] == 0) {
			html += "<img src='" + imgurl + "' onerror=\"javascript:this.src='../images/face0.png'\" />";
		} else {
			html += "<img src='" + imgurl + "' />";
		}
		html += "</div>";
		html += "<div class=\"img-find-warp\"></div>";
		html += "</div>";
		html += "<dl>"
		nickNameTran = friendUser['nickname'].replaceAll('<', '&lt').replaceAll('>', '&gt');
		html += "<dt><span onclick=\"lookUserView(" + friendUser['lvid'] + "," + friendUser['userid'] + ")\" class=\"findName\">" + nickNameTran + "<a></span><span>(" + friendUser['lvid'] + ")</span></a></dt>";
		html += "<dd>";
		html += "<p class='addfirinfo'>" + sexinfo + friendUser['age'] + "岁" + "<i class='split'></i><span>" + city + "</span><span>&nbsp;</span><span>" + district + "</span></p>";
		html += "<div><span class='add' onclick='addMyFriend(" + friendUser['lvid'] + ")'>好友</span></div>";
		html += "</dd>";
		html += "</dl>";
		html += "</li>";
		html += "</ul>";
	}
	$(".recommList").html(html);
	scrollfun();
}

$('.searchBack a').click(function() {
	$('.findPeople').show();
	$('.searchBack,.findGroup').hide();
	$('.Recommend').show();
	recomm();
})

function printPage(pageTotal, result) {
	var data = result;
	var pagehtml = '';
	var tit;
	pagehtml += '<div class="FbackPaging" title="">';
	pagehtml += '<div class="pagingWarp" id="pagingWarp" title="">';
	if (data['pageno'] == 1) {
		pagehtml += '<span style="cursor:Default" class="page-none"><a class="mouserde" title="已是首页" href="javascript:void(0)"><img src="../images/pageUp.png" title="已是首页" style="cursor:Default"></a></span>';
	} else {
		pagehtml += '<span style="cursor:Default"><a  href="javascript:goPage(' + (data['pageno'] - 1) + ')"><img src="../images/pageUp.png"></a></span>';
	}

	if (data['pageno'] != 1 && data['pageno'] >= 4 && pageTotal != 4) {
		pagehtml += '<span title=""><a href="javascript:goPage(1)">1</a></span>';
	}
	if (data['pageno'] - 2 > 2 && data['pageno'] <= pageTotal && pageTotal > 5) {
		pagehtml += '<span><a href="javascript:void(0)">...</a></span>';
	}
	var start = data['pageno'] - 2,
		end = data['pageno'] + 2;
	if ((start > 1 && data['pageno'] < 4) || data['pageno'] == 1) {
		end++;
	}
	if (data['pageno'] > pageTotal - 4 && data['pageno'] >= pageTotal) {
		start--;
	}
	for (; start <= end; start++) {
		if (start <= pageTotal && start >= 1) {
			if (start != data['pageno']) {
				pagehtml += '<span><a href="javascript:goPage(' + start + ')">' + start + '</a></span>';
			} else {
				pagehtml += '<span class="active"><a href="javascript:">' + start + '</a></span>';
			}
		}
	}
	if (data['pageno'] + 2 < pageTotal - 1 && data['pageno'] >= 1 && pageTotal > 5) {
		pagehtml += '<span><a href="javascript:">...</a></span>';
	}
	if (data['pageno'] != pageTotal && data['pageno'] < pageTotal - 2 && pageTotal != 4) {
		pagehtml += '<span><a href="javascript:goPage(' + pageTotal + ')">' + pageTotal + '</a></span>';
	}
	if (data['pageno'] == pageTotal) {
		pagehtml += '<span class="page-none" style="cursor:Default" title=""><a class="mouserde" title="已是末页" href="javascript:void(0)"><img src="../images/pageDown.png" title="已是末页" alt="" style="cursor:Default"></a></span>';
	} else {
		pagehtml += '<span title=""><a title="" href="javascript:goPage(' + (data['pageno'] + 1) + ')"><img src="../images/pageDown.png" title="" alt=""></a></span>';
	}
	pagehtml += '</div>';
	pagehtml += '</div>';
	if ($('#groupMess').is(':visible')) {
		$('.pageG').html(pagehtml);
	} else {
		$('.pageFi').html(pagehtml);
	}
	$('.pagingWarp span:even').not('.pagingWarp span:last-child').css('border-right', 'none');
	$('.pagingWarp span:odd').not('.pagingWarp span:last-child').css('border-right', 'none');
	var botpage = $('span[class="active"]').text();
	$('.top_pageNo').text(botpage);
	var pagetext = Number($('.top_pageNo').text());
	$('.top_next').click(function() {
		if (pagetext >= 　pageTotal) {
			$('.top_pageNo').text(pageTotal);
			$('.top_next').parent('a').attr('href', 'javascript:');
		} else {
			pagetext = pagetext + 1;
			$('.top_pageNo').text(pagetext);
			$('.top_next').parent('a').attr('href', 'javascript:goPage(' + pagetext + ')');
		}
	})
	$('.top_prev').click(function() {
		if (pagetext != 1) {
			pagetext = pagetext - 1;
			$('.top_pageNo').text(pagetext);
			$('.top_prev').parent('a').attr('href', 'javascript:goPage(' + pagetext + ')');
		}
	})
}

function goPage(num) {
	params['pageno'] = num;
	if ($('#groupMess').is(':visible')) {
		$('.findGList').scrollTop(0)
		$.getJSON(requestUrlMap['searchGroup']['url'], params, searchGroupCallBack);
		return;
	}
	$('.findPList').scrollTop(0)
	$.getJSON(requestUrlMap['searchFriend']['url'], params, searchUserCallBack);
}
//加云组

function addMyGroup(groupid) {
	var params = {
		'lvid': lvid,
		'groupid': groupid,
		'sign': sign
	};
	$.getJSON(requestUrlMap['inGroup']['url'], params, function success(data) {
		inGroupCallBack(data, groupid);
	});
}

function inGroupCallBack(data, groupid) {
	if (data.errorid != 0) {
		var params = "{command:9006, params:{title:'提示', cat:0, content:'加组申请失败，请重试！'}}";
		sendCommand(params);
		return;
	}
	//已经加入该云组或者该云组不允许任何人加入
	if (data['ingroup'] == 1 || data['verifytype'] == 2) {
		var params = "{command:1002, params:{groupid:" + groupid + ",teamid: " + teamid + "}}";
		sendCommand(params);
		return;
	}
	//允许任何人加组 
	if (data['verifytype'] == 1) {
		var params = "{command:1002, params:{groupid:" + groupid + ",teamid: " + teamid + "}}";
		sendCommand(params);
		return;
	}
	var params = {
		'lvid': lvid,
		'groupid': groupid,
		'noticetype': 2,
		'sign': sign,
		'supergroupid':teamid
	};
	$.getJSON(requestUrlMap['groupNotice']['url'], params, function(data) {
		var paramStr = null;
		if (data.errorid == 0) {
			paramStr = "{title:'提示', cat:0, content:'您已成功加入该云组！'}";
		} else {
			paramStr = "{title:'提示', cat:2, content:'" + data.errmsg + "'}";
		}
		var params = "{command:9006,params:" + paramStr + "}";
		sendCommand(params);
	});
}

//查看云组资料
function lookGroupView(groupid) {
	var params = "{command:3002,params:{groupid:" + groupid + "}}";
	sendCommand(params);
}