var globalGivenGifts = {};
var globalFriends = {};
var globalGiftList = ['a cashew nut', 'a peanut', 'a hazelnut', 'a red pistachio nut'];
function postActivity(nut, friend) {
var title = 'gave ' + globalFriends.getById(friend).getDisplayName() + ' ' + globalGiftList[nut];
var params = {};
params[opensocial.Activity.Field.TITLE] = title;
var activity = opensocial.newActivity(params)
opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function() {});
}
function updateReceivedList(viewer, data, friends) {
var viewerId = viewer.getId();
var html = new Array();
html.push('You have received:
');
friends.each(function(person) {
if (data[person.getId()]) {
var json = data[person.getId()]['gifts'];
var gifts = {}
if (!json) {
gifts = {};
}
try {
gifts = gadgets.json.parse(gadgets.util.unescapeString(json));
} catch (e) {
gifts = {};
}
for (i in gifts) {
if (i.hasOwnProperty && i == viewerId) {
for (j in gifts[i]) {
if (j.hasOwnProperty) {
html.push('
', globalGiftList[gifts[i][j]], ' from ', person.getDisplayName(), '
');
}
}
}
}
}
});
html.push('
');
document.getElementById('received').innerHTML = html.join('');
}
function updateGiftList(viewer, data, friends) {
var json = null;
if (data[viewer.getId()]) {
json = data[viewer.getId()]['gifts'];
}
if (!json) {
globalGivenGifts = {};
}
try {
globalGivenGifts = gadgets.json.parse(gadgets.util.unescapeString(json));
} catch (e) {
globalGivenGifts = {};
}
var html = new Array();
html.push('You have given:');
html.push('
');
for (i in globalGivenGifts) {
if (i.hasOwnProperty) {
for (j in globalGivenGifts[i]) {
if (j.hasOwnProperty) {
html.push('
', friends.getById(i).getDisplayName(), ' received ', globalGiftList[globalGivenGifts[i][j]], '
');
}
}
}
}
html.push('
');
document.getElementById('given').innerHTML = html.join('');
}
function giveGift() {
var nut = document.getElementById('nut').value;
var friend = document.getElementById('person').value;
if (!globalGivenGifts) {
globalGivenGifts = {};
}
if (!globalGivenGifts[friend]) {
globalGivenGifts[friend] = new Array();
}
globalGivenGifts[friend].push(nut);
var json = gadgets.json.stringify(globalGivenGifts);
var req = opensocial.newDataRequest();
req.add(req.newUpdatePersonAppDataRequest("VIEWER", 'gifts', json));
req.add(req.newFetchPersonRequest("VIEWER"), 'viewer');
var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
var opt_params = {};
opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'viewerFriends');
var viewer = opensocial.newIdSpec({ "userId" : "VIEWER" });
req.add(req.newFetchPersonAppDataRequest(viewer, 'gifts'), 'data');
req.add(req.newFetchPersonAppDataRequest(viewerFriends, 'gifts', opt_params), 'viewerFriendData');
req.send(onLoadFriends);
postActivity(nut, friend);
}
function makeOptionsMenu() {
var html = new Array();
html.push('');
document.getElementById('gifts').innerHTML = html.join('');
}
function loadFriends() {
var req = opensocial.newDataRequest();
req.add(req.newFetchPersonRequest("VIEWER"), 'viewer');
var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
var opt_params = {};
opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'viewerFriends');
var viewer = opensocial.newIdSpec({ "userId" : "VIEWER" });
req.add(req.newFetchPersonAppDataRequest(viewer, 'gifts'), 'data');
req.add(req.newFetchPersonAppDataRequest(viewerFriends, 'gifts', opt_params), 'viewerFriendData');
req.send(onLoadFriends);
}
function onLoadFriends(data) {
var viewer = data.get('viewer').getData();
var viewerFriends = data.get('viewerFriends').getData();
var giftData = data.get('data').getData();
var viewerFriendData = data.get('viewerFriendData').getData();
html = new Array();
html.push('');
document.getElementById('friends').innerHTML = html.join('');
globalFriends = viewerFriends;
updateGiftList(viewer, giftData, viewerFriends);
updateReceivedList(viewer, viewerFriendData, viewerFriends);
}
function init() {
loadFriends();
makeOptionsMenu();
}
gadgets.util.registerOnLoadHandler(init);