var SPARQL = {}

SPARQL.OUTPUT = {
	JSON:'json',
	XML:'xml'
}

SPARQL.ACCEPT = {
	'json':'application/sparql-results+json,text/javascript',
	'xml':'application/sparql-results+xml'
};

SPARQL.Service = function(endpoint) {
	
	//Private variables
	var _endpoint = endpoint;
	var _output = SPARQL.OUTPUT.XML;
	var _prefix_map = {};
	
	//Get functions
	this.endpoint = function() { return _endpoint; };
	this.output = function() {return _output};
	this.prefixes = function() { return _prefix_map; };
	
	//Set functions
	this.setPrefix = function(p, u) { this.prefixes()[p] = u; };
	this.createQuery = function() { return new SPARQL.Query(this); };
	
	return this;
	
	
	return this;
}

SPARQL.Query = function(service){
	
	var _service = service;
	var _query = '';
	
	this.service = function(){return _service};
	this.query = function(){return _query};
	
	this.setQuery = function(q){_query = q};
	
	this.execute = function(callback){
		var _url = this.service().endpoint() + '?' +	this.getQueryString();
		trace("Complete query is: " + _url);
		return ExternalXHR.GET(_url, callback, {'Accept':SPARQL.ACCEPT[this.service().output()]} );
	}
	
	this.getQueryString = function(){
		var s = '';
		s += 'output=' + this.service().output();
		s += '&';
		s += 'query=' + escape(this.query().replace('\n',' '));
		return s;
	}
	
	this.getPrefixes = function(){
		return '';
	}
}