﻿<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="Likey" height="35" author="NebWeb" author_email="wave-gadget@nebweb.com.au">
    <Require feature="wave" />
  </ModulePrefs>
  <Content type="html">
    <![CDATA[

<script type="text/javascript" src="http://wave-api.appspot.com/public/wave.js"></script>
<style type="text/css">
  .gadgetContainer {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 11em;
    text-align: right;
    border-top: 1px solid #ccc;
    padding-top: 2px;
    font-size: small;
    white-space:nowrap;
    background: none;
  }
  .like {
    color: #696;
  }
  .dislike {
    color: #966;
  }
  .gadgetContainer a {
    color: #003EA8;
    font-family: arial, sans-serif;
    text-decoration: underline;
  }
  #userChoice {
    display:block;
    clear:both;
  }
</style>

<script type="text/javascript">

  /**
   * @fileoverview A simple like/dislike Wave gadget 
   *  that can be added to a blip for intuitive user rating.
   *
   * @author wave-gadget@nebweb.com.au (Ben Griffiths)
   * @version 1.2b
   * 
   * Credit to elizabethford@google.com (Elizabeth Ford) for
   *  her rating gadget which was a valuable reference
   *  for this improved version of rateget/Likey.
   *  http://wave-samples-gallery.appspot.com/about_app?app_id=23006
   */
   
  /**
   * Converts the binary representation of data to hex
   * (used here as a one-way hash function)
   * Taken from http://www.navioo.com/
   * @param s The input to be converted
   * @deprecated Part of legacy support since version 1.2
   */
  function bin2hex(s){  
    // Converts the binary representation of data to hex
    var i, f = 0, a = [];
    s += '';
    f = s.length;
    for (i = 0; i<f; i++) {
      a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
    }
    return a.join('');
  }
  
  /**
   * Creates and returns a new delta objects
   * (for passing to wave.getState().submitDelta())
   * @param {string} key The key value to be set in the new delta.
   * @param {string} value The value to be assigned in the new delta
   */
  function newDelta(key,value) {
    var delta = {};
    delta[key] = value;
    return delta;
  }

  /**
   * Registers that a user has voted and tallies their input accordingly.
   * @param {string} type The type of vote - ie. 'like' or 'dislike'.
   */
  function vote(type) {
    if (type != 'like' && type != 'dislike') return void(0);
    var otherType = ( type == 'like' ? 'dislike' : 'like' );
    var user = wave.getViewer().getId();
    var voteKey = user+'-vote';
    var countKey = type+'Count';
    var otherCountKey = otherType+'Count';
    var pastVote = wave.getState().get(voteKey);
    if (!pastVote) {
      var legacyVote = wave.getState().get(bin2hex(voteKey));
      if (legacyVote) {
        wave.getState().submitDelta(newDelta(voteKey,legacyVote));
        pastVote = legacyVote;
      }
    }
    var voteCount = parseInt(wave.getState().get(countKey));
    
    if (!pastVote) {
      if (!voteCount) voteCount = 0;
      voteCount += 1;
      wave.getState().submitDelta(newDelta(countKey,voteCount));
      wave.getState().submitDelta(newDelta(voteKey,type));
    } else if (pastVote == otherType) {
      wave.getState().submitDelta(newDelta(voteKey,type));
      var otherVoteCount = parseInt(wave.getState().get(otherCountKey));
      if (!voteCount) voteCount = 0;
      if (!otherVoteCount) otherVoteCount = 0;
      voteCount += 1;
      otherVoteCount -= 1;
      wave.getState().submitDelta(newDelta(countKey,voteCount));
      wave.getState().submitDelta(newDelta(otherCountKey,otherVoteCount));
    }
  }

  /**
   * Updates the gadget frontend to reflect the current state 
   *  (ie. vote counts and current user vote).
   */
  function stateUpdated() {
    if (wave.getState()) {
      var likeCount = wave.getState().get('likeCount');
      if (likeCount) {
        document.getElementById('likeCount').innerHTML = likeCount;
      }
      var dislikeCount = wave.getState().get('dislikeCount');
      if (dislikeCount) {
        document.getElementById('dislikeCount').innerHTML = dislikeCount;
      }
      var voteKey = wave.getViewer().getId()+'-vote';
      var pastVote = wave.getState().get(voteKey);
      if (!pastVote) {
        var legacyVote = wave.getState().get(bin2hex(voteKey));
        if (legacyVote) {
          wave.getState().submitDelta(newDelta(voteKey,legacyVote));
          pastVote = legacyVote;
        }
      }
      if (pastVote) {
        var userChoice = document.getElementById('userChoice');
        userChoice.innerHTML = 'You '+pastVote+' this.';
      }
    } else {userChoice.innerHTML = 'State unavailable';}
  }

  function main() {
    if (wave && wave.isInWaveContainer()) {
      wave.setStateCallback(stateUpdated);
      if (wave.getState()) stateUpdated();
    }
  }
  
  gadgets.util.registerOnLoadHandler(main);

</script>
<div class="gadgetContainer">
  <a href="javascript:vote('like');">Like</a>&nbsp;(<span id="likeCount" class="count like">0</span>)&nbsp;&nbsp;
  <a href="javascript:vote('dislike');">Dislike</a>&nbsp;(<span id="dislikeCount" class="count dislike">0</span>)
  <span id="userChoice">&nbsp;</span>
</div>
    
    ]]>
  </Content>
</Module>
