// Copyright 2009 Google Inc.
// All Rights Reserved.
// Owner: webgroup-emea@google.com

/**
 * @fileoverview Javascript for Things to do.
 * @author amcgrath@google.com (Adam Mcgrath)
 */

/**
 * Namespace ttd (Things to do).
 */
var ttd = window.ttd || {};

/**
 * Creates a ratings widget for use with Things to do landing page.
 * @constructor
 * @param {object} opts an object containing configuration properties.
 */
ttd.Ratings = function(opts) {
  this.callback = opts.callback;
  this.lang = opts.lang || 'en';
  this.appId = opts.appId || 76;
  this.ns = opts.ns;
  this.debug = opts.debug;
  this.entities = this.getEntities_(opts.count);
  this.req = ttd.utils.getXhr();
  this.ratingsContainer = document.getElementById(opts.ratingsContainer);
  this.widget = google.annotations.create('RatingPanel', this.ratingsContainer,
                                         {'entity': {'url': this.ns},
                                         'inline': true});
  this.loadData_();
};

/**
 * @private
 * @return {string} URL of Google annotations framework.
 */
ttd.Ratings.prototype.getRatingsUrl_ = function() {
  return this.debug ? 'http://www.google.com/reviews/json/lookup' :
                      '/reviews/json/lookup';
};

/**
 * Creates xhr request to get annotations data.
 * @private
 */
ttd.Ratings.prototype.loadData_ = function() {
  this.req.onreadystatechange = ttd.utils.bind(this.dataHandler_, this);
  if (this.debug) {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
  }
  this.req.open('POST', this.getRatingsUrl_(), true);
  this.req.send('req={"entities":[' + this.entities + '], "applicationId": ' +
      this.appId + '}');
};

/**
 * @return {string} list of entities for data req.
 * @param {int} num The number of entities on the page.
 * @private
 */
ttd.Ratings.prototype.getEntities_ = function(num) {
  var str = '';
  for (var i = 1; i < num + 1; i++) {
    str += '{"url": "' + this.ns + '%23tip' + i +
           '", "includeAggregateInfo": true},';
  };
  return str;
};

/**
 * Handles the onreadystatechange event of data req.
 * @param {object} response the ajax response object.
 * @private
 */
ttd.Ratings.prototype.dataHandler_ = function(response) {
  if (this.req.readyState != 4) {return;}
  var obj = eval('(' + this.req.responseText + ')');
  var sorted = obj.annotations.sort(function(a, b) {
    return (b.aggregateInfo.averageRating || 0) -
           (a.aggregateInfo.averageRating || 0);
  });
  this.data = sorted;
  this.callback();
};

/**
 * @return {object} Object containing the annotations data.
 */
ttd.Ratings.prototype.getData = function() {
  return this.data;
};

/**
 * @return {element} The DOM node that contains the annotations widget.
 */
ttd.Ratings.prototype.getRatingsContainer = function() {
  return this.ratingsContainer;
};

/**
 * Changes the entity which the widget is rating.
 * @param {string} id The current tip id.
 */
ttd.Ratings.prototype.setEntity = function(id) {
  google.annotations.setEntity(this.widget, {'url': this.ns + id});
  google.annotations.fetch();
};
