var followType = null;
function openFollow() {
	followType = 'follow';
	changeHidden('floatDiv', 'block');
	$('#floatTitle').html('加为关注');
	$('#floatBody').html($('#follow').html());
}
function openFriend() {
	followType = 'friend';
	changeHidden('floatDiv', 'block');
	$('#floatTitle').html('加为好友');
	$('#floatBody').html($('#follow').html());
}
function newFollowGroup() {
	$('#followGroupName').css('display', 'block'); 
	$('#followGroupNew').css('display', 'none');
	$('#followGroupName input').focus(); 
}
function closeFollowGroup() {
	$('#followGroupName input').val('');
	$('#followGroupName').css('display', 'none'); 
	$('#followGroupNew').css('display', 'block');
}
function doFollow(spaceOwnerId) {
	if (followType == 'follow') {
		toFollow(spaceOwnerId);
	} else {
		addFriend(spaceOwnerId);
	}
}
function toFollow(spaceOwnerId) {
	var groupId = $("#newGroup input:radio:checked").val();
	var toFollowUID = spaceOwnerId;
	var newGroupName = $("#newGroupName").val();
	var url = WEB_PATH + "/ajax.php?mod=Space&do=toFollow"
	$.post(url, {groupId:groupId, toFollowUID:toFollowUID, newGroupName:newGroupName}, function(data){toFollowResult(data)}, 'json');
	
	function toFollowResult(data) {
		if (data.status == 'SUCCESS') {
			str = "<a href='javascript:void(0)' onclick='unFollow(" + spaceOwnerId + ")'><img align='absmiddle' src='misc/images/icon/click.gif'> 取消关注</a>";
			$('#toFollow').html(str)
			showMsg('操作成功');
		} else {
			alert(data.message);
		}
	}
}
function addFriend(spaceOwnerId) {
	var groupId = $("#newGroup input:radio:checked").val();
	var friendUID = spaceOwnerId;
	var newGroupName = $("#newGroupName").val();
	var url = WEB_PATH + "/ajax.php?mod=Space&do=addFriend"
	$.post(url, {groupId:groupId, friendUID:friendUID, newGroupName:newGroupName}, function(data){addFriendResult(data)}, 'json');
	
	function addFriendResult(data) {
		if (data.status == 'SUCCESS') {
			str = "<a href='javascript:void(0)' onclick='unFollow(" + spaceOwnerId + ")'><img align='absmiddle' src='misc/images/icon/click.gif'> 取消关注</a>";
			$('#toFollow').html(str)
			showMsg('请求发送成功。');
		} else {
			alert(data.message);
		}
	}
}
function unFollow(spaceOwnerId) {
	if (!confirm('您确认要取消关注么？')) {
		return;
	}
	var unFollowUID = spaceOwnerId;
	var url = WEB_PATH + "/ajax.php?mod=Space&do=unFollow"
	$.post(url, {unFollowUID:unFollowUID}, function(data){unFollowResult(data)}, 'json');
	
	function unFollowResult(data) {
		if (data.status == 'SUCCESS') {
			str = "<a href='javascript:void(0)' onclick='openFollow()'><img align='absmiddle' src='misc/images/icon/click.gif'> 加为关注</a>";
			$('#toFollow').html(str)
			showMsg('操作成功');
		} else {
			alert(data.message);
		}
	}
}
function delFriend(spaceOwnerId) {
	if (!confirm('您确认要删除该好友么？')) {
		return;
	}
	var unFollowUID = spaceOwnerId;
	var url = WEB_PATH + "/ajax.php?mod=Space&do=unFollow"
	$.post(url, {unFollowUID:unFollowUID,type:'ajax'}, function(data){delFriendResult(data)}, 'json');
	
	function delFriendResult(data) {
		if (data.status == 'SUCCESS') {
			str = "<a href='javascript:void(0)' onclick='openFollow()'><img align='absmiddle' src='misc/images/icon/click.gif'> 加为好友</a>";
			$('#addFriend').html(str)
			str = "<a href='javascript:void(0)' onclick='openFollow()'><img align='absmiddle' src='misc/images/icon/click.gif'> 加为关注</a>";
			$('#toFollow').html(str)
			showMsg('操作成功');
		} else {
			alert(data.message);
		}
	}
}
function openMessage() {
	followType = 'message';
	changeHidden('floatDiv', 'block');
	$('#floatTitle').html('小纸条');
	$('#floatBody').html($('#message').html());
}

replyCommentID = 0;
function doReply(feedId, username, userId) {
	url = 'ajax.php?mod=Feed&do=reply&feedId='+feedId;
	content = $('#replyContent').val();
	reship = $('#reshipCk').attr('checked');

	if (content.length == 0) {
		alert('请填写评论内容');
		$('#replyContent').focus()
		return;
	}

	$('#replyContent').val('');
	$('#replySub').attr('disabled', 'true');
	$.post(url, {content:content, reship:reship, replyCommentID:replyCommentID}, function(data){appendReply(userId, username, data, content)}, 'json');
	replyCommentID = 0;

	function appendReply(userId, username, data, content) {
		if (data.status == 'SUCCESS') {
			date = getDate();
			commentId = data.data;
			comment = '<div id="commentRow_' + commentId + '" class="row"><div class="headPic">';
			comment += '<a href="javascript:void(0)"><img src='+IMAGE_PROCESSING_PATH+'"/avatar.php?uid=' + userId + '" width="35" height="35"></a></div>';
			comment += '<div class="commentContent"><div>';
			comment += '<a href="javascript:void(0)">' + username + '</a>&nbsp;&nbsp;&nbsp;[' + date + ']';
			comment += ' - <a href="javascript:void(0)" onclick="setReplyTo(' + commentId + ', \'' + username + '\')">回复</a>';
			comment += ' - <a href="javascript:void(0)" onclick="delComment(' + commentId + ')">删除</a>';
			comment += '</div><div id="commentContent_' + commentId + '">' + content + '</div></div></div>';
			$('#reply').before(comment);
			$('#replyNum').html(parseInt($('#replyNum').html()) + 1);
		} else {
			alert(data.message);
		}

		$('#replySub').removeAttr('disabled');
		unsetReply();
	}
}

function delComment(commentId) {
	if (confirm('您确认要删除这条回复信息么？(本操作不可恢复)')) {
		url = 'ajax.php?mod=Comment&do=del&commentId=' + commentId + '&' + new Date();
		$.getJSON(url, function(data){
			if (data.status == 'SUCCESS') {
				$('#commentRow_' + commentId).remove();
			} else {
				alert(data.message)
			}
		});
	}
}

// 设置回复指定的对象
function setReplyTo(commentId, userName) {
	html = '回复：' + userName + '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="unsetReply()">X</a>';
	$('#replyTo').css('display', 'block');
	$('#replyTo').html(html);
	$("#replyContent").focus();
	replyCommentID = commentId;
}
function unsetReply() {
	$('#replyTo').css('display', 'none');
	$('#replyTo').html('');
	$("#replyContent").focus();
	replyCommentID = 0;
}

function delBlog(blogId) {
	if (confirm('您确认要删除这条日志么？(本操作不可恢复)')) {
		location.href="?mod=Space_Blog&do=doDelete&blog=" + blogId;
	}
}
function delMBlog(mbId) {
	if (confirm('您确认要删除这条日志么？(本操作不可恢复)')) {
		location.href="?mod=Space_MBlog&do=doDelete&mblog=" + mbId;
	}
}
