<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
-->
<Module>
	<ModulePrefs title="ActivityStreams Gadget!">
		<Require feature="opensocial-0.9"/>
		<Require feature="osapi"/>
		<Require feature="dynamic-height"/>
	</ModulePrefs>
	
	<Content type="html">
	<![CDATA[
		<script type="text/javascript" src="OpenSocialWrapper.js"></script>
		<script type="text/javascript" src="ActivityStreamsRender.js"></script>
		
		<script type="text/javascript">
			social = new OpenSocialWrapper();
			render = new ActivityStreamsRender();
			
			// TODO: move this stuff into ActivityStreamRender (if you can...)
			// Renders retrieval of an ActivityEntry by ID
			function renderActivityEntryId(div, callback) {
				var html = "<h2>Work with an ActivityEntry</h2>";
				html += "ActivityEntry ID: <input type='text' size=40 id='activityEntryId'>";
				html += "<input type='button' value='Retrieve' onclick='retrieveActivityEntryId()'>";
				html += "<input type='button' value='Delete' onclick='deleteActivityEntryId()'><br>";
				html += "Note: you must be the owner of the ActivityEntry to retrieve it.";
				html += "<textarea id='activityEntryText' cols=75 rows=10>No entry to display...</textarea><br>";
				document.getElementById(div).innerHTML = html;
				callback();
			}
			
			// Deletes the activity entry
			function deleteActivityEntryId() {
				social.deleteActivityEntryById(document.getElementById('activityEntryId').value, function(response) {
					document.getElementById('activityEntryText').value = 'No entry to display...';
					render.renderActivityEntries('activityEntryies', refresh);
				});
			}
			
			// Retrieves the activity entry
			function retrieveActivityEntryId() {
				social.getActivityEntryById(document.getElementById('activityEntryId').value, function(response) {
					document.getElementById('activityEntryText').value = JSON.stringify(response);
				});
			}
			
			// Creation form for activity entry
			function renderCreateActivityEntry(div, callback) {
				var htmlCreateActivityEntry = "<h1>ActivityStreams</h1>";
				htmlCreateActivityEntry += "Demonstrates use of the ActivityStream service in Apache Shindig.  This implementation follows the JSON draft specfication: http://activitystrea.ms/head/json-activity.html<br>";
				htmlCreateActivityEntry += "<h2>Create an ActivityEntry</h2>";
				htmlCreateActivityEntry += "<table>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Title</td>";
				htmlCreateActivityEntry += "<td><input type='text' size=50 id='activityEntryTitle'></td>"
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Body</td>";
				htmlCreateActivityEntry += "<td><input type='text' size=50 id='activityEntryBody'></td>"
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Object Name</td>";
				htmlCreateActivityEntry += "<td><input type='text' size=50 id='activityObjectName'></td>"
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Object Summary</td>";
				htmlCreateActivityEntry += "<td><input type='text' size=50 id='activityObjectSummary'></td>"
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Object Permalink</td>";
				htmlCreateActivityEntry += "<td><input type='text' size=50 id='activityObjectPermalink'></td>"
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "</table>";
				htmlCreateActivityEntry += "<table>";
				htmlCreateActivityEntry += "<tr style='color:blue'>";
				htmlCreateActivityEntry += "<td colspan='2'> Note: Per the ActivityStreams specification, only a single verb and object type are now supported.</td>";
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td>Verbs</td>";
				htmlCreateActivityEntry += "<td>Object Types</td>";
				htmlCreateActivityEntry += "</tr>";
				htmlCreateActivityEntry += "<tr>";
				htmlCreateActivityEntry += "<td><select MULTIPLE id='selectVerbs' size=10>";
				htmlCreateActivityEntry += "<option value='markAsFavorite'>Mark as Favorite</option>";
				htmlCreateActivityEntry += "<option value='startFollowing'>Start Following</option>";
				htmlCreateActivityEntry += "<option value='markAsLiked'>Mark as Liked</option>";
				htmlCreateActivityEntry += "<option value='makeFriend'>Make Friend</option>";
				htmlCreateActivityEntry += "<option value='join'>Join</option>";
				htmlCreateActivityEntry += "<option value='play'>Play</option>";
				htmlCreateActivityEntry += "<option value='post'>Post</option>";
				htmlCreateActivityEntry += "<option value='save'>Save</option>";
				htmlCreateActivityEntry += "<option value='share'>Share</option>";
				htmlCreateActivityEntry += "<option value='tag'>Tag</option>";
				htmlCreateActivityEntry += "<option value='update'>Update</option>";
				htmlCreateActivityEntry += "</select></td>";
				htmlCreateActivityEntry += "<td><select MULTIPLE id='selectObjectTypes' size=10>";
				htmlCreateActivityEntry += "<option value='article'>Article</option>";
				htmlCreateActivityEntry += "<option value='audio'>Audio</option>";
				htmlCreateActivityEntry += "<option value='bookmark'>Bookmark</option>";
				htmlCreateActivityEntry += "<option value='comment'>Comment</option>";
				htmlCreateActivityEntry += "<option value='file'>File</option>";
				htmlCreateActivityEntry += "<option value='folder'>Folder</option>";
				htmlCreateActivityEntry += "<option value='group'>Group</option>";
				htmlCreateActivityEntry += "<option value='list'>List</option>";
				htmlCreateActivityEntry += "<option value='note'>Note</option>";
				htmlCreateActivityEntry += "<option value='person'>Person</option>";
				htmlCreateActivityEntry += "<option value='photo'>Photo</option>";
				htmlCreateActivityEntry += "<option value='photoAlbum'>Photo Album</option>";
				htmlCreateActivityEntry += "<option value='place'>Place</option>";
				htmlCreateActivityEntry += "<option value='playlist'>Playlist</option>";
				htmlCreateActivityEntry += "<option value='product'>Product</option>";
				htmlCreateActivityEntry += "<option value='review'>Review</option>";
				htmlCreateActivityEntry += "<option value='service'>Service</option>";
				htmlCreateActivityEntry += "<option value='status'>Status</option>";
				htmlCreateActivityEntry += "<option value='video'>Video</option>";
				htmlCreateActivityEntry += "</select></td>";
				htmlCreateActivityEntry += "</table>";
				htmlCreateActivityEntry += "<input type='button' value='Submit' onclick='createActivityEntry()'>";
				htmlCreateActivityEntry += "<br><br>";
				document.getElementById(div).innerHTML = htmlCreateActivityEntry;
				callback();
			}
			function createActivityEntry() {
				// Gather selected verbs
				verbOptions = document.getElementById('selectVerbs');
				selVerbs = [];
				count = 0;
				for(i = 0; i < verbOptions.options.length; i++) {
					if(verbOptions.options[i].selected) {
						selVerbs[count] = verbOptions.options[i].value;
						count++;
					}
				}
				
				// Gather selected types
				typeOptions = document.getElementById('selectObjectTypes');
				selTypes = [];
				count = 0;
				for(i = 0; i < typeOptions.options.length; i++) {
					if(typeOptions.options[i].selected) {
						selTypes[count] = typeOptions.options[i].value;
						count++;
					}
				}
				
				var title = blankToNull(document.getElementById('activityEntryTitle').value);
				var body = blankToNull(document.getElementById('activityEntryBody').value)
				var objectName = blankToNull(document.getElementById('activityObjectName').value);
				var objectSummary = blankToNull(document.getElementById('activityObjectSummary').value);
				var objectPermalink = blankToNull(document.getElementById('activityObjectPermalink').value);
				social.postActivityEntry(title, body, selVerbs[0], viewer.id, viewer.name.formatted,
												objectName, objectSummary, objectPermalink, selTypes[0],
												function(response) {
					render.renderActivityEntries('activityEntries', refresh);
				});
			}
			function blankToNull(str) {
				return (str == '' ? null : str);
			}
		
			// Adjusts the window height
			function refresh() {
				gadgets.window.adjustHeight();
			}
		
			// Initializes the gadget
			function init() {
				render.renderWelcome('welcome', refresh);
				render.renderActivities('activities', refresh);
				render.renderActivityEntries('activityEntries', refresh);
				renderActivityEntryId('htmlGetEntry', refresh);
				renderCreateActivityEntry('htmlCreateEntry', refresh);
			}
			
			gadgets.util.registerOnLoadHandler(init);
		</script>
		<div id='welcome'></div>
		<div id='activities'></div>
		<div id='htmlCreateEntry'></div>
		<div id='htmlGetEntry'></div>
		<div id='activityEntries'></div>
	]]>
	</Content>
</Module>