<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs title="Hello, Friends!">
		<Require feature="opensocial-0.8" />
		<Require feature="minimessage" />
	</ModulePrefs>
	<Content type="html"><![CDATA[

		<h1>Hello, Friends!</h1>

		<dl>

			<dt>マイミクを招待</dt>
			<dd>
				<div>
					<input id="invite" type="button" value="マイミクを招待"/>
				</div>
			</dd>

			<dt>メッセージ送信</dt>
			<dd>
				<div>
					<input id="title" type="text"/><br/>
					<textarea id="body"></textarea><br/>
					<input id="send" type="button" value="送信"/>
				</div>
			</dd>

			<dt>カウントアップ(<span id="counter">0</span>)</dt>
			<dd>
				<div>
					<input id="countup" type="button" value="カウントアップ"/>
				</div>
			</dd>

			<dt>マイミク一覧</dt>
			<dd>
				<div id="wrapper" style="display:none;">
					<div repeat="${people}" class="person">
						<input type="radio" name="to" value="${Cur.id}"/>
						<img alt="${Cur.displayName}" src="${Cur.thumbnailUrl}"/>
						${Cur.nickname}(<span class="counter">未インストール</span>)
					</div>
				</div>
			</dd>

		</dl>
		<div id="debug"></div>
			
		<script type="text/javascript" src="http://scripts.lrlab.to/opensocial-jquery-1.3.2.5.min.js"></script>
		<script type="text/javascript" src="http://scripts.lrlab.to/opensocial-jquery.templates-0.1.0.min.js"></script>
		<script type="text/javascript" src="http://scripts.lrlab.to/opensocial-jquery.mixi-1.0.0.min.js"></script>
		<script type="text/javascript" src="http://scripts.lrlab.to/opensocial-jquery.minimessage-1.0.0.min.js"></script>
		<script type="text/javascript" src="http://feed.suz-lab.com/other/js/ext/prettyprint.js"></script>

		<script type="text/javascript">

			jQuery(function($) {
				var owner_id = "";
				var owner_counter  = 0;

				$('<span/>').text('Dismissible Message').minimessage();

				// 自分の取得
				$.getData("/people/@owner/@self", function(data) {
					owner_id = data[0].id;
				});

				// マイミクの表示
				$.getData("/people/@owner/@friends", function(data) {
					$("#wrapper").render({ people: data }).show();
					$.each(data, function(i, friend) {
						if(friend.hasApp == "true") {
							$.getData("/appdata/" + friend.id + "/@self", function(data) {
								if(data[friend.id] != null && data[friend.id].counter != null) {
									friend_counter = data[friend.id].counter;
									$(".person .counter").eq(i).html(friend_counter);
								}
							});
						}
					});
				});

				// カウンターの表示
				$.getData("/appdata/@viewer/@self", function(data) {
					if(data[owner_id] != null && data[owner_id].counter != null) {
						owner_counter = data[owner_id].counter;
						$("#counter").html(owner_counter);
					}
				});

				// マイミクの招待
				$("#invite").click(function(){
					$.invite("@friends", function(ids) {
						$.each(ids, function(i, id) {
							alert("「" + id + "」 を招待しました。");
						});
					});
				});

				// メッセージの送信
				$("#send").click(function(){
					$.postData("/messages/@viewer/@outbox", {
						recipients: $("input:radio:checked").val(),
						title     : $("#title").val(),
						body      : $("#body").val()
					}, function() {
						alert("送信しました。");
					});
				});

				// カウントアップ
				$("#countup").click(function(){
					$.postData("/appdata/@viewer/@self", {
						counter: owner_counter + 1
					}, function() {
						owner_counter++;
						$("#counter").html(owner_counter);
						alert("カウントアップしました。");
					});
				});

//				$.getFeed("http://feeds.feedburner.com/suz-lab-blog", function(data) {
//					alert(data.Title);
//					$("#debug").html(prettyPrint(data));
//				});

			});

		</script>
	]]></Content>
</Module>

