<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Jom Skate!" author="Nazrul Kamaruddin" author_email="nazroll@gmail.com" description="An OpenSocial application that lets you post skate sessions at your favourite spots together with your friends.">
  <Require feature="opensocial-0.8" />
  <Require feature="views" />
  <Require feature="dynamic-height" />
  <Require feature="minimessage"/>
  </ModulePrefs>
  
<Content type="html" view="canvas">
<![CDATA[
<script type="text/javascript">
/**
 * Global variables
 *
 */
var appowner = {}
var appviewer = {}
var appownerfriends = {}

function implode( glue, pieces ) { 
  return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

function init(){
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.OWNER), 'owner');
  req.add(req.newFetchPeopleRequest(opensocial.newIdSpec({'userId':'OWNER', 'groupId':'FRIENDS'})), 'ownerFriends');
  req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
  req.send(function(response){
    appowner = response.get('owner').getData();
    appviewer = response.get('viewer').getData();
    appownerfriends = response.get('ownerFriends').getData();
    
    var thumbnail = appowner.getField(opensocial.Person.Field.THUMBNAIL_URL);
    var firstname = appowner.getDisplayName();
    var fname = (appowner.getDisplayName()).split(" ");
    if(fname.length > 2){
      firstname = fname[0] +' '+ fname[1];
    }else{
      firstname = fname[0];
    }    
    
    var msg = '';
    
    // TODO: Remotely fetch list of the next spots to skate
    var nextspot = new Array;
    nextspot.push('Mont Kiara Skatepark');
    nextspot.push('KJ Ledges');
    
    // Build message
    if(nextspot.length > 1){
      msg += ' plans to skate ';
      for(var i=0; i<nextspot.length; i++){
        if( (i == (nextspot.length - 1)) && (i != 0) ){
          msg += ' and ';
        }
        msg += '<a href="javascript:void(0);">'+ nextspot[i] +'</a>';
        if( (nextspot.length != 1) && (i != (nextspot.length - 1)) && (i != (nextspot.length - 2)) ){
          msg += ', ';
        }
      }     
    }else{
      msg += ' plans to skate '+ '<a href="javascript:void(0);">'+ nextspot[0] +'</a>';
    }
    
    var html = new Array;
    
    // Display owner thumbnail & his/her plans
    html.push('<p style="margin-bottom:4px;">'+ firstname + msg +'</p>');
    
    // TODO: Remotely fetch a list of people who joined owner's session. Right now just display a list of friends as an example.
    html.push('<p>These guys are joining him:</p>');
    html.push('<ul style="margin:0; padding:0 0 8px 0; overflow:auto;">');
    appownerfriends.each(function(person){
      if(person.getId()){
        html.push('<li style="float:left; list-style:none; padding:8px;"><img src="'+ person.getField(opensocial.Person.Field.THUMBNAIL_URL) +'" /> <span style="display:block;"><a href="javascript:void(0);">'+ person.getDisplayName() +'</a></span></li>');
      }
    });
    html.push('</ul>');
    
    html.push('<input type="button" value="Jom Skate!" onclick="jomSkate();" style="padding:4px; margin-top:10px; font-size:16px; font-weight:bold; color:#fff; background-color:#000;" />');
    html.push('<input type="button" value="Post a new session" onclick="displayForm(\'show\');" style="padding:4px; margin-left:6px; margin-top:10px; font-size:16px; font-weight:bold;" />');
    html.push('<div style="clear:both;"></div>');
    document.getElementById('gadgetuserinfo').innerHTML = html.join('');
    
    // Clear HTML array
    html = [];
    
    // TODO: Remotely fetch a list of skate spots based on country 
    var skatespot = new Array;
    skatespot.push('Mont Kiara Skate Park');
    skatespot.push('Shah Alam X-Park');
    skatespot.push('Kampung Batu Extreme Park');
    skatespot.push('Putrajaya X-Park');  
    
    // Display skate spots  
    html.push('<select style="font-size:13px;" id="skatespot" multiple size="10" onchange="loadSkatespot();">');
    html.push('<option selected>Select a skate spot...</option>');
    for(var i=0; i<skatespot.length; i++){
      html.push('<option value="'+ i +'">'+ skatespot[i] +'</option>');
    }
    html.push('</select>');
    document.getElementById('listSkatespot').innerHTML = html.join('');
  });
};

function loadSkatespot(){
  var skatespot = document.getElementById('skatespot');
  var selected = skatespot.options[skatespot.selectedIndex].text;
  var value = skatespot.options[skatespot.selectedIndex].value;
 
  var html = new Array;
  if(value >= 0){
    html.push('<h2>'+ selected +'</h2>');
    html.push('<p>Some description about the skate spot. Concrete skate park. Indoor skate park. Covered, outdoor skate park. Great for technical and creative flow.</p>');
    document.getElementById('skatespotinfo').innerHTML = html.join(''); 
  }else{
    document.getElementById('skatespotinfo').innerHTML = '';
  }
}

function jomSkate(){
  statusMsg('You just joined '+ appowner.getDisplayName() +' session.');
  gadgets.window.adjustHeight();
}

function statusMsg(text){
  var msg = new gadgets.MiniMessage(__MODULE_ID__);
  msg.createDismissibleMessage(text);
}

function displayForm(close){
  var current = 'none';
  if(close == 'show'){ current = 'block';}
  document.getElementById('gadgetform').style.display = current;
  gadgets.window.adjustHeight();
}

function postActivity(){

  var thespot = document.getElementById('skatespot').options[document.getElementById('skatespot').selectedIndex].text;
  var thehour = document.getElementById('timeHour').options[document.getElementById('timeHour').selectedIndex].text;
  var themin = document.getElementById('timeMin').options[document.getElementById('timeMin').selectedIndex].text;
  var ampm = document.getElementById('timeAMPM').options[document.getElementById('timeAMPM').selectedIndex].text;  
  
  var errmsg = '';
  if( (themin == '') || (thehour == '') ){
    errmsg += "Please set the time\n"; 
  }
  if( thespot == 'Select a skate spot...' ){
    errmsg += "Please select a skate spot\n";
  }
  if(errmsg){
    return;
  }
    
  msg = 'skating '+ thespot +' @ '+ thehour +':'+ themin + ampm;
  postActivityMsg(msg);  
  
  statusMsg('OK');
  gadgets.window.adjustHeight();
}

function postActivityMsg(text){
  var params = {};  
  params[opensocial.Activity.Field.TITLE] = text;
  var activity = opensocial.newActivity(params); 
  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function(){});
}

/**
 * Initialize gadget
 */
gadgets.util.registerOnLoadHandler(init);
</script>

<div id="gadgetuserinfo" style="overflow:auto; padding:8px 8px 8px 8px; margin:0 0px 8px 0px; color:#333;"></div>
<div id="gadgetform" style="border-top:solid 1px #ddd; padding:8px; background-color:#eee; display:none;">
  <p><strong>Where</strong></p>
  <div style="float:left; width:30%;" id="listSkatespot"></div>
  <div id="skatespotinfo" style="float:left; width:70%;"></div>
  <div style="clear:both; padding:8px;"></div>
  <div style="padding-bottom:8px;">
  <p style="display:inline;"><strong>When</strong></p>
  <div style="padding-bottom:8px; display:inline;"><select id="timeHour">
    <option></option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
  </select> : <select id="timeMin">
    <option></option>
    <option>00</option>
    <option>15</option>
    <option>30</option>
    <option>45</option>
  </select> <select id="timeAMPM">
    <option></option>
    <option>AM</option>
    <option>PM</option>
  </select></div>
  </div>
  <div style="padding-top:8px; border-top:solid 1px #ddd;"><input type="button" value="Post" onclick="postActivity();" /> <input type="button" value="Cancel" onclick="displayForm();" /></div>
</div>

]]>
</Content>

<Content type="html" view="profile,home">
<![CDATA[
<script>
function navigateToCanvas() {
  var canvas = gadgets.views.getSupportedViews()["canvas"];
  gadgets.views.requestNavigateTo(canvas);
}

function init(){
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.OWNER), 'owner');
  req.send(function(response){
    var owner = response.get('owner').getData();
    var owner_thumb = owner.getField(opensocial.Person.Field.THUMBNAIL_URL);
    
    var html = new Array;
    
    // Display owner thumbnail & his/her plans
    // html.push('<img style="float:left; display:block; margin-right:8px;" src="'+ owner_thumb +'" alt="'+ owner.getDisplayName() +'" />');
    html.push('<p>');
    html.push(owner.getDisplayName() +' plans to skate <a href="javascript:void(0);">Mont Kiara Skate Park</a> and <a href="javascript:void(0);">KJ Ledges</a>.');
    html.push('<input type="button" style="display:block; padding:5px; font-weight:bold;" value="Jom Skate!" onclick="navigateToCanvas();" />');
    html.push('</p>');
    document.getElementById('gadgetuserinfo').innerHTML = html.join('');
    
    // Reset HTML array
    html = [];

    // Display recent activities
    // TODO: Fetch a list of recent activites for user
    var activity = new Array;
    activity.push('skated <a href="javascript:void(0);">Shah Alam X-Park</a> yesterday');
    activity.push('skated <a href="javascript:void(0);">Putrajaya Ledges</a> 4 days ago');
    activity.push('suggested a new skate spot: <a href="javascript:void(0);">KJ marble ledges</a>');
    activity.push('skated <a href="javascript:void(0);">Mont Kiara Skate Park</a> 5 days ago');
    activity.push('skated <a href="javascript:void(0);">PBD Ledges</a> last week');
    
    if(activity.length > 0){ html.push('<h3>Recently</h3>'); }
    html.push('<ul>');
    for(var i=0; i<activity.length; i++){
      html.push('<li class="activity">'+ owner.getDisplayName() +' '+ activity[i] +'</li>');
    }
    html.push('</ul>');
    document.getElementById('recently').innerHTML = html.join('');
    gadgets.window.adjustHeight();
  });
};

/**
 * Initialize gadget
 */
gadgets.util.registerOnLoadHandler(init);
</script>

<div id="gadgetuserinfo" style="overflow:auto; padding-bottom:1px;"></div>
<div id="recently"></div>

]]>
</Content>

</Module>
