var CoPlayers = new (function() {

  var h;
  this.coplayersInfo = {}; // Used on the profile page

  this.update = function(hash) {
    h = hash.evalJSON();
  };

  this.relation = function(id) {
    return h[id] || 0;
  };

  this.addCoPlayer = function(id, t) { // TODO: implement via Comm
    Comm.enqueue('friends', 'add_coplayer', [id, t])
    h[id] = t;
  };

  this.complain = function(id, r) {
    new Ajax.Request("/coplayers/complain", {
      method: 'post',
      parameters: {'complained_id': id, 'reason': r, 'game_id': (typeof Game != "undefined") ? Game.gid : undefined }
    });
  };

  this.allInRelationInfo = function(rel) {
    return Object.keys(h).select(function(k) {
      return h[k] == rel && this.coplayersInfo[k]; // A player can be in h but not in this.coplayersInfo (when his account is deleted), filter those out.
    }, this).map(function(k) {
      return {'avatar': this.coplayersInfo[k][1], 'playerName': this.coplayersInfo[k][0], 'playerID': k, 'rel': rel};
    }, this);
  };

  this.anyIsStar = function(ids) {
    var any = false;
    ids.each(function(id) {
      any = any || (h[id] == 1);
    }, this);
    return any;
  };

  this.anyIsBlock = function(ids) { 
    var any = false;
    ids.each(function(id) {
      any = any || (h[id] == 5);
    }, this);
    return any;
  };

})();
