/*
 * Services.js
 * Simple wrapper object for making web service requests.
 * By Ryan Sandor Richards
 * Copyright 2009 FreeCause Inc.
 */

var Services = {
  url: Site.baseurl,

  /**
   * Sets the controller used to reference web services.
   * @param controller Controller for which to call web services.
   */
  setController: function(controller) {
    this.url = Site.baseurl + controller + '/';
  },

  /**
   * Performs a single web service request using POST. Automatically sends the request
   * to the initialized controller and passes the id of the site along as a parameter.
   *
   * @param name Name of the service
   * @param params Parameters to be sent along with the request. 
   * @param callback Callback function to execute upon completion of the request.
   */
  post: function(name, params, callback) {
    // Add the site id to the reqest if present
    var $sid_input = $('input[name=site_id]');
    
    if (typeof(params['site_id']) == 'undefined')
      params['site_id'] = Site.id;
    
    // Perform the ajax request
    jQuery.ajax({
      cache: false,
      dataType: 'json',
      data: params,
      success: callback,
      url: Services.url + name,
      type: 'POST'
    });
  }
};
