/**
 * AJAX helpers
 */
 
 YAHOO.namespace("cedars.ajax");
 
 YAHOO.cedars.ajax.section = 'ajax';
 
 /**
  * Converts a JSON response to a JavaScript object
  *
  * @param o the AJAX response object that was passed to the callback
  */
 YAHOO.cedars.ajax.toObject = function(o) {
	var object = null;
	if (o.responseText != null) {
		object = eval ('(' + o.responseText + ')');
	}
	return object
 }
 
 /**
  * Converts a JavaScript object to a URL. Please note that the object
  * should be an associative array.
  *
  * @param parameters the "parameters" object (e.g. {foo: 'bar', my_id=123})
  */
 YAHOO.cedars.ajax.toURL = function(parameters) {
	var url = '';
	var name;
	for (name in parameters) {
		url += name + '=' + encodeURI(parameters[name]) + '&';
	}
	return url;
 }
 
 /**
  * Performs an AJAX request within the 'ajax' section of the Cedars site
  *
  * @param method the method ('get' or 'post')
  * @param section the subsection of the 'ajax' section that will process the request
  * @param callback the callback (for YAHOO.util.Connect.asyncRequest)
  */
 YAHOO.cedars.ajax.asyncRequest = function(method, section, callback, parameters) {
	var requestUrl = YAHOO.cedars.site.root + '/' + YAHOO.cedars.ajax.section + '/' +section;
	var queryString = this.toURL(parameters);
	switch (method) {
		case 'get':
			YAHOO.util.Connect.asyncRequest(method, requestUrl + '?' + queryString, callback);	
			break;
		case 'post':
			YAHOO.util.Connect.asyncRequest(method, requestUrl, callback, queryString);
			break;			
	}
 }
 
 /**
  * Synchronously retrieves an IgnitionWeb text
  * 
  * @param identifier the identifier (global or scope-specific)
  *
  * @param scope the scope of the text (for automatic prefixing).
  *				 Valid values: 
  *				 'global': no prefix
  *              'site': prefixed with 'cedars_'
  *              'section' if the section name is 'foo/bar', the prefix will be 'cedars_foo_bar'  
  */
  
YAHOO.cedars.ajax.getText = function(identifier, scope) {
	if (scope == undefined) {
		scope = 'global';
	}
	parameters = {};	
	parameters['scope'] = scope;
	parameters['identifier'] = identifier;
	if (scope == 'section') {
		parameters['section'] = YAHOO.cedars.site.section;
	}
	var requestUrl = YAHOO.cedars.site.root + '/' + YAHOO.cedars.ajax.section + '/gettext?' + this.toURL(parameters);
	var o = YAHOO.util.Connect.getConnectionObject();              
	o.conn.open('GET', requestUrl, false);
	o.conn.send(null);
	var text = o.conn.responseText;
	return text;	
}
  
  