<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Has app installed" description="" author="" author_email="" thumbnail="" screenshot="" height="380">
   <Require feature="opensocial-0.7"/>
   <Require feature="dynamic-height"/>
 </ModulePrefs>
 <Content type="html">

 <![CDATA[

<script type="text/javascript">

// this is a gadget example on how to filter on persons who have the app installed
function loadData() {
        var params = {};
        params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;

        var req = opensocial.newDataRequest();
        req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer');

		// You can also check all of your friends with VIEWER_FRIENDS
		//req.add(req.newFetchPersonRequest('VIEWER_FRIENDS', params), 'viewer');
        req.send(onLoadData);
}

function onLoadData(data) {
        var viewer = data.get('viewer').getData();
		if(viewer != null) {
        	var result = "You (the viewer) have the app installed";
		} else {
        	var result = "You (the viewer) do not have the app installed";
		}
        document.getElementById('message').innerHTML = result;
        gadgets.window.adjustHeight();
}

function viewerinfo() {
	document.getElementById('viewernotvisible').style.display = "none";
    function getViewer(responseItem) {
		if(responseItem.hadError()) {
			var sErrorcode = responseItem.getErrorCode();
			if(sErrorcode == 'forbidden') {
				document.getElementById('message').innerHTML = "You are not logged in, please log in first.";
			} else if(sErrorcode == 'unauthorized') {
				document.getElementById('message').innerHTML = "This application can't see your information because you will have to give permission to share your information. Please click the button below.";
			}
		}	
        if (opensocial.hasPermission(opensocial.Permission.VIEWER)) {
                loadData();
        }
    }
    opensocial.requestPermission([opensocial.Permission.VIEWER], 'This reason sentence is not used at the moment', getViewer);
}

function init() {
    if (opensocial.hasPermission(opensocial.Permission.VIEWER)) {
	    loadData();
    } else {
		document.getElementById('viewernotvisible').style.display = "block";
    }
}

gadgets.util.registerOnLoadHandler(init);

  </script>
  <div id="viewernotvisible" style="display:none;"><input type="button" onclick="viewerinfo();" value="Ask for viewer permission"></div>
  <div id="message"></div>
  ]]>
  </Content>
</Module>