<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="What's up?"
	     description="What's up? allows you to easily share what you're currently doing with your friends. Add this to your profile and tell your friends What's Up!"
	     screenshot="http://static.zaling.com/os/wu-screen.png"
             author="Zaling"
             thumbnail="http://os.zaling.com/wu-thumb.png"
             author_email="contactus@zaling.net"
             author_affiliation="Zaling.com">
	<Require feature="opensocial-0.7" />
	<Require feature="dynamic-height" />
        <Require feature="analytics" />
        <Require feature="skins" />
</ModulePrefs>
<Content type="html">
<![CDATA[
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
if(!console) {
  var console = {}
  console.log = function(text){
    return
  }
}

if (!NING) { var NING = {}; }
NING.openSocial = NING.openSocial || {};
NING.openSocial.WU = function() {
	var $D = YAHOO.util.Dom, $E = YAHOO.util.Event, $ = $D.get;
	var req = opensocial.newDataRequest();
	var val = '';
	var objOwner = {};
	return me = {
		uiDrawMainPanel: function(obj) {
			var html = "";
			$("mainpanel").innerHTML = html;
			gadgets.window.adjustHeight();
		},
		uiToggleSettings: function() {
			var isVisible = ($D.getStyle("statuspanel", "display") == "block");
			$D.setStyle("statuspanel", "display", (isVisible ? "none" : "block"));
		        $D.setStyle("statusbar", "display", "none");  // ADDED
			gadgets.window.adjustHeight();
		},
		onGetLocation: function(resp) {
			var userid = objOwner.getId();
			if (resp.get("get_data").hadError()) {
				alert("get callback failed");
			} else {
				var data = resp.get("get_data").getData();

				if (data[userid]) {
					val = data[userid]["loc"];
                                        $("mainpanel").innerHTML = val;
                                        gadgets.window.adjustHeight();
				} else {
                                        var html = "You haven't posted anything, post a status below. :) "
					$("mainpanel").innerHTML = html;
                                        gadgets.window.adjustHeight();
				}
			}
                        gadgets.window.adjustHeight();
		},

		getUserLocation: function() {
			console.log("getUserLocation");
			req.add(req.newFetchPersonAppDataRequest("OWNER", "loc"), "get_data");
			req.send(this.onGetLocation);
		},

		onSetLocation: function(resp) {
			console.log("onSetLocation");
			if (resp.get("set_data").hadError()) {
				alert("set error");
			} else {
				alert("The update was successful!");
			}
		},

		setUserLocation: function() {
  var params = {};
  params[opensocial.Activity.Field.TITLE] = "" + $("loc").value;
  var activity = opensocial.newActivity(params);
  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH);
			console.log("setUserLocation");
			var locval = $("loc").value;
			req.add(req.newUpdatePersonAppDataRequest("VIEWER", "loc", locval), "set_data");
			req.send(this.onSetLocation);
			me.uiToggleSettings();
		        $D.setStyle("statusbar", "display", "block");
			me.getUserLocation();
		},

		onGetUserObjects: function(resp) {
			objOwner = resp.get("oOwner").getData();
			if (objOwner.isViewer()) {
				$D.setStyle("statusbar", "display", "block");
				$E.on("settings", "click", me.uiToggleSettings);
				$E.on("locbtn", "click", me.setUserLocation);
			}
		},

		getUserObjects: function() {
			req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER), "oOwner");
			req.send(this.onGetUserObjects);
		},

		init: function() {
			$D.setStyle("mainpanel", "background-color", gadgets.skins.getProperty(gadgets.skins.Property.BG_COLOR));
			$D.setStyle("statusbar", "background-color", gadgets.skins.getProperty(gadgets.skins.Property.BG_COLOR));
			$D.setStyle("statuspanel", "background-color", gadgets.skins.getProperty(gadgets.skins.Property.BG_COLOR));
			$D.setStyle("handler", "background-color", gadgets.skins.getProperty(gadgets.skins.Property.BG_COLOR));
			$D.setStyle("mainpanel", "color", gadgets.skins.getProperty(gadgets.skins.Property.FONT_COLOR));
			$D.setStyle($("mainpanel").getElementsByTagName("A"), "color", gadgets.skins.getProperty(gadgets.skins.Property.ANCHOR_COLOR));
			$D.setStyle($("statusbar").getElementsByTagName("A"), "color", gadgets.skins.getProperty(gadgets.skins.Property.ANCHOR_COLOR));
			$D.setStyle("statuspanel", "color", gadgets.skins.getProperty(gadgets.skins.Property.FONT_COLOR));
			this.getUserObjects();
			this.getUserLocation();
		}
	};
};
gadgets.util.registerOnLoadHandler(function() {
	oW = NING.openSocial.WU();
	oW.init();
});
</script>
<div id="handler" style="">
	<div id="mainpanel" style="text-align:center; padding:10px 0; font: 20px Verdana;"></div>
	<div id="statusbar" style="display:none;width:100%;text-align:right;"><a id="settings" href="#">Update Your Status!</a></div>
	<div id="statuspanel" style="border-top:1px dotted #ccc; font:11px Verdana;display:none;clear:both;">
		<label for="loc">What's up?</label>
		<input type="textbox" id="loc" style="width:70%;">
		<input type="submit" value="Update!" id="locbtn">
	</div>
	<!-- <script>_IG_Analytics("CodeHere", "/wu");</script> -->
</div>
]]>
</Content>
</Module>