Ajax.CI = Class.create();
Ajax.CI.prototype = Object.extend(new Ajax.Base(), {

  initialize: function(){
    this.transport = Ajax.getTransport();
  },

  request: function(func, args, options){

	// The object to hold our parameters
	var theparams = new Object();

  	// Set the paramters
  	theparams.ajax_param = args;

	// Serialize the arguments
  	this.args = this.serialize(theparams);

	// Build the URL based on the current one
  	this.url = location.href.replace(/#.*/, '');

	// Check if $_GET is already in use
	if (location.href.indexOf('?') != -1){
		var start = '&';
	}
	else {
		var start = '?';
	}

	if (!options){
		options = {};
	}

	if (typeof(options.method) != 'undefined' && options.method == 'get'){
		this.url = this.url + start + this.args + '&ajax_function=' + escape(func);
	}
	else {
		options.postBody = this.args + '&ajax_function=' + escape(func);

		options.method = 'post';
	}

	//options.parameters = {ajax_function: func, ajax_param: args};
	this.func = false;

	if (options && typeof(options.onComplete) != 'undefined'){
		this.func = options.onComplete;
	}

	options.onComplete = null;

	options.onSuccess = AJAX.prepComplete.bind(null, this.func);

	new Ajax.Request(this.url, options);
  },

  // Get ready for completion
  prepComplete: function(func, transport){

	//var func = AJAX.func;
	var data = transport.responseText;

	if (data.substring(0, 5) == '<' + '?xml' && obj.responseXML != ''){

		if (typeof(xfunc) != 'undefined' && self.xfunc){
			eval(func + '(obj.responseXML)');
		}

	}
	else if (data.substring(0, 9) == 'AJAX_true'){

		if (typeof(func) != "undefined"){
			eval('var testt = '+ data.substring(9));
			func(testt);
		}

	}
	else if (data.substring(0, 10) == 'AJAX_false'){
		alert("The AJAX Module encountered an error whilst attempting to perform the selected action. It returned the following '" + data.substring(10) + "'.");
	}
	else {
		alert("The server encountered an error whilst attempting to perform the selected action. It returned the following '" + data + "'.");
	}

  },

  serialize: function(formdata, numeric_prefix, key){
		var numeric_prefix = (typeof(numeric_prefix) == 'undefined') ? '' : numeric_prefix;
		var key = (typeof(key) == 'undefined') ? '' : key;
		var result = new Array();

		// Array or object?
		if (formdata.constructor.toString().indexOf("Array") == -1){
			var hash = $H(formdata);
		}
		else {
			var hash = $A(formdata);
		}

		// Loop through the form data
		hash.each(function(pair, index){

			if (pair && typeof(pair.key) == 'undefined'){
				var k = index;
				var v = pair;
			}
			else if (pair){
				var k = pair.key;
				var v = pair.value;
			}
			else {
				return;
			}



			// URLencode the key and add a numeric prefix
			var temp_key = !isNaN(parseFloat(k)) ? numeric_prefix + k : k;

			// Add the previous key
			if (key){
				temp_key = key + '[' + temp_key + ']';
			}

			// If it's an array, do it all again
			// Fix: typeof(null) == 'object'
			if (v !== null && (typeof(v) == 'object' || typeof(v) == 'array')){
				result.push(AJAX.serialize(v, '', temp_key));
			}
			else {

				if (v === true){
					v = '__core__b:1;';
				}
				else if (v === false){
					v = '__core__b:0;';
				}
				else if (typeof(v) == 'undefined' || v === null){
					v = '__core__N;';
				}

				v = encodeURIComponent(v);

				result.push(temp_key + '=' + v);//AJAX.utf8_string(v));
			}

		});

		return result.join('&');
	},

  utf8_string: function(thestr){
	if (thestr == '' || typeof(thestr) == 'undefined' || !thestr.toString()){
		return '';
	}

	if ((matches = thestr.toString().match(/[\\x90-\\xFF]/g))){
		var replace = (matches[0].charCodeAt(0) & 0xFF).toString(16);
		thestr = thestr.toString().replace(matches[0], '%u00' + replace);
	}

	// Use %2B, not + for whitespaces
	thestr = escape(thestr).replace(/\+/g, "%2B");
	return thestr;
  }

});

var AJAX = new Ajax.CI();
