<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="appData example" description="" author="" author_email="" thumbnail="" screenshot="" height="380">
	<Require feature="opensocial-0.7"/>
</ModulePrefs>
<Content type="html">
	<![CDATA[
	<br>
	<div id='content'></div>
	<br>
	<br>
	&nbsp;<input type="button" value="Set appdata" onClick="request();">&nbsp;
	<input type="button" value="Get appdata" onClick="get_request();">
	<script type="text/javascript">
	
	function get_callback(response) {
		// response.get("oViewer").getData().getId();
		var userid = response.get("oViewer").getData().getId();
		
		if (response.get("get_data").hadError()) {
		        /* the fetch failed ... insert code to handle the error */
		        console.log("get callback failed");
		} else {
		        var data = response.get("get_data").getData();
		        alert(data[userid]["myKey"]);
		        /* the fetch was successful ... "data" contains the app data */
		        console.log("get callback suceeded");
		}
	};
	
	function set_callback(response) {
		if (response.get("set_data").hadError()) {
			/* The update failed ... insert code to handle the error */
		    console.log("set callback failed"+response.get("set_data").getErrorMessage());
		} else {
			/* The update was successful ... continue execution */
			console.log("set callback succeeded");
		}
	};
	
	function get_request() {
		var getViewer = function() {
			if(opensocial.hasPermission(opensocial.Permission.VIEWER)) {
				var req = opensocial.newDataRequest();
				req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), "oViewer");
				req.add(req.newFetchPersonAppDataRequest("VIEWER", "myKey"), "get_data");
				req.send(get_callback);
			} else {
				document.getElementById('content').innerHTML = 'You did not give permission to see the viewer';
			}
		}		
		opensocial.requestPermission([opensocial.Permission.VIEWER], 'This reason sentence is not used at the moment', getViewer);
	};
	
	// set data with the current time as value and with key "myKey"
	function request() {
		var getViewer = function() {
			if(opensocial.hasPermission(opensocial.Permission.VIEWER)) {
				var req = opensocial.newDataRequest();
				var sTime = getCurrentTime().toString();
				req.add(req.newUpdatePersonAppDataRequest("VIEWER", "myKey", sTime), "set_data");
				req.send(set_callback);
			} else {
				document.getElementById('content').innerHTML = 'You did not give permission to see the viewer';
			}
		}		
		opensocial.requestPermission([opensocial.Permission.VIEWER], 'This reason sentence is not used at the moment', getViewer);
	};
	
	function getCurrentTime() {
		var currentTime = new Date()
		return currentTime;
	}
	</script>
	]]>
</Content>
</Module>