// --------------------
// ---- REQUESTS ----
// --------------------

jQuery.reqs = {

// properties

	root: 		 		HOMEPAGE + '/index.php?',
	responseDiv: 		"#response",
	defaultController:  "ajax",
	action: 	 		false,
	data: 		 		false,
	loading:	 		true,
	_controller: 		"c=",
	_action: 	 		"a=",
	_return:	 		false,
	_type:		 		"GET",
	_async:		 		true,
	_loading:			true,

	
/**
* Fire AJAX request
*/	
	fire: function () {
		var resp = $(this.responseDiv);
		var $return = this;
		this.setController();
		this.setUrl();
		this.setAsync();
		if(this._loading == true) 	{ this.loading(resp, true); }
		$.ajax({
			async: this._async,
			type:  this._type, 
			data:  this.data, 
			url:   this.url, 
			success: function(ret)	{ resp.html(ret); }	
		});  		
		return this;
	},	
	
	getResponse: function () {
		var response = false;
		this.setController();
		this.setUrl();
		this.setAsync();
		$.ajax({
			async: this._async,
			type:  this._type, 
			data:  this.data, 
			url:   this.url, 
			success: function(ret)	{ response = ret; }	
		});  		
		return response;
	},	
	
	
	setAsync: function(async)
	{
		if(async != undefined) { this._async = async; }
		return this;
	},
	
	// set request type (post, get - default)
	setType: function(type) {
		if(type != 'POST')	{ type = 'GET'; }
		this._type = type;
		return this;
	},
	
	setLoading: function(val) {
		if(val == false) { this._loading = false; }
		return this;		
	},
	
	/**
	 * Set action 
	 * @example: this.setAction('test');
	 */
	setAction: function (action) {
		this.resetAction();
		if(action != undefined) { this.action = escape(action); } 
		this.action = '&' + this._action + this.action;
		return this;
	},
	
	/**
	* Reset action
	* @example: this.setAction('test');
	*/	
	resetAction: function() {
		this.action = this._action;
	},

	/**
	* Set data
	* @example: this.setData('&john=Doe&sisa=sisa');
	*/	
	setData: function (data) {
		if(data != undefined) { this.data = data; }
		return this;
	},
	
	/**
	* Set controller: if none is set, default prefix will be used
	* @example: this.setController('sisa');
	*/	
	setController: function (controller) {
		if(controller != undefined) { 
			this.controller = this._controller + escape(controller); 
		} else {
			this.resetController();
		}
		return this;
	},
	
	resetController: function() {
		this.controller = this._controller + this.defaultController;
	},
	
	/**
	* Set request url: it woll set url as concatenation of root + controller + url passed as parameter
	* @example: this.setUrl('test/');
	*/	
	setUrl: function () {
		this.url = this.root + this.controller + this.action;
		return this;
	},
	
	/**
	* Set response div: if none is set, default response Div (this.resp) will be used
	* @example: this.setResponseDiv('responseDiv');
	*/	
	setResponseDiv: function (responseDiv) {
		if(responseDiv != undefined) { this.responseDiv = responseDiv; }
		return this;
	},
	
	/**
	* Show loading message or loading img, if clear is set, clear contents of response container
	*/	
	loading: function (div, clear) {
		if(clear != undefined) { div.html(''); }
		div.prepend('<div class="loading">&nbsp;</div>');
		return this;
	}
	
};



// fire these when DOM is ready 
$(document).ready( function() {
	
	
});

