var BLOG_URL = 'http://blog.helloproject.tw/';
var API_URL = 'http://blog.helloproject.tw/api/';

function addFriend(uid, obj) {
	$.post(API_URL + 'friend.php', {'mode': 'friend_add', 'uid': uid}, function(res) {
		if (res.ok) {
			alert('已將 ' + uid + ' 加入好友。\n請注意對方可能要一段時間後才有反應。');
			if (obj != undefined) {
				obj.setAttribute('onClick', "delFriend('"+uid+"', this)");
				if (obj.tagName == 'TD') obj.innerHTML = '<b>○</b>';
				if (obj.tagName == 'IMG') obj.src = '/pics/friend_del.png';
			} else {
				window.location.reload();
			}
		} else {
			alert(res.msg);
		}
	}, 'json');
}

function delFriend(uid, obj) {
	if (!confirm('確定要刪除好友 ' + uid + ' 嗎？')) return;

	$.post(API_URL + 'friend.php', {'mode': 'friend_del', 'uid': uid}, function(res) {
		if (res.ok) {
			alert('已刪除原好友 ' + uid + '。\n請注意對方可能要一段時間後才有反應。');
			if (obj != undefined) {
				obj.setAttribute('onClick', "addFriend('"+uid+"', this)");
				if (obj.tagName == 'TD') obj.innerHTML = '';
				if (obj.tagName == 'IMG') obj.src = '/pics/friend_add.png';
			} else {
				window.location.reload();
			}
		} else {
			alert(res.msg);
		}
	}, 'json');
}

function subscribe(uid, obj) {
	$.post(API_URL + 'friend.php', {'mode': 'subscribe', 'uid': uid}, function(res) {
		if (res.ok) {
			alert('已訂閱 ' + uid + ' 的部落格。\n請注意追蹤訂閱部落格的文章需要等一段時間。');
			if (obj != undefined) {
				obj.setAttribute('onClick', "unsubscribe('"+uid+"', this)");
				if (obj.tagName == 'TD') obj.innerHTML = '<b>○</b>';
				if (obj.tagName == 'IMG') obj.src = '/pics/unsubscribe.png';
			} else {
				window.location.reload();
			}
		} else {
			alert(res.msg);
		}
	}, 'json');
}

function unsubscribe(uid, obj) {
	if (!confirm('確定要解除訂閱 ' + uid + ' 的部落格嗎？')) return;

	$.post(API_URL + 'friend.php', {'mode': 'unsubscribe', 'uid': uid}, function(res) {
		if (res.ok) {
			alert('已停止訂閱 ' + uid + ' 的部落格。\n請注意從追蹤訂閱部落格的文章消失需要等一段時間。');
			if (obj != undefined) {
				obj.setAttribute('onClick', "subscribe('"+uid+"', this)");
				if (obj.tagName == 'TD') obj.innerHTML = '';
				if (obj.tagName == 'IMG') obj.src = '/pics/subscribe.png';
			} else {
				window.location.reload();
			}

		} else {
			alert(res.msg);
		}
	}, 'json');
}
