//
// Feedbag: AJAX interface for retrieving a JSON representation of a feedbag by ID.
//
function Feedbag(api_key, id, success_callback, error_callback){
  //
  // PRIVATE INSTANCE VARIABLES.
  // criteria passed to the object constructor are implicit private instance variables.
  //
  var that = this;  // Make this object available to private methods.
  // api_key - Your assigned Feedbags API key.
  // id - The ID of the feedbag you would like to request.
  // success_callback - A reference to the JavaScript function to call on success.
  // error_callback - A reference to the JavaScript function to call on failure.
  
  //
  // PRIVATE METHODS.
  // Callable only by privileged methods and other private methods.
  //
  function hasApiKey(){ return (api_key != null); }
  
  //
  // PRIVILEGED METHODS.
  // Public methods with access to private instance variables and methods.
  //
  this.setApiKey = function(value){ api_key = value; }
  this.setId = function(value){ id = value; }
  this.getId = function(){ return id; }
  this.setSuccessCallback = function(func){ success_callback = func; }
  this.setErrorCallback = function(func){ error_callback = func; }
  
  // Perform the retrieval.
  this.exec = function(){
    // Construct and execute JSONP query.
    var query_url = 'http://beta.consumersunion.org/feedbags/feedbags/' + id;
    var query = 'api_key=' + api_key;

    var options = { 
      type: 'GET',
      url: query_url,
      dataType: 'jsonp',
      data: query,
      processData: false,
      contentType: 'application/json',
      success: that.processResponse,
      error: that.processError
    };
    jQuery.ajax(options);
  }
  
  this.processResponse = function(responseBody, status){
    if( success_callback != null ){ success_callback.call(null, responseBody); }
  }

  this.processError = function(xml_http_request, status, exception){
    if( error_callback != null ){ error_callback.call(null, xml_http_request, status, exception); }
  }
}