//Request.JSONRPC = Request.JSON.implement({

var RequestJSONRPC = new Class({
	Implements: Request.JSON,									 
	/*
	initialize: function(options){
		this.parent(options);
		//this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'});
	}, */
	
	send: function(options){
		if (!this.check(arguments.callee, options)) return this;
		this.running = true;

		var type = $type(options);
		if (type == 'string' || type == 'element') options = {data: options};

		var old = this.options;
		options = $extend({data: old.data, url: old.url, method: old.method}, options);
		var data = options.data, url = options.url, method = options.method;

	    if (data && method == 'get'){
	        switch ($type(data)){
		    	case 'element': data = $(data).toQueryString(); break;
			    case 'object': case 'hash': data = Hash.toQueryString(data);
		    }
			url = url + (url.contains('?') ? '&' : '?') + data;
			data = null;
		} else { //(method == 'post')
		
		    if (!data) {
		        data = {  };
		    }
		    data = JSON.encode(data);
		} 
		
		this.xhr.open(method.toUpperCase(), url, this.options.async);

		this.xhr.onreadystatechange = this.onStateChange.bind(this);

		this.headers.each(function(value, key){
			if (!$try(function(){
				this.xhr.setRequestHeader(key, value);
				return true;
			}.bind(this))) this.fireEvent('exception', [key, value]);
		}, this);

		this.fireEvent('request');
		this.xhr.send(data);
		if (!this.options.async) this.onStateChange();
		return this;
	}
	
});
