
/*var*/ _lastRequestId = 1;

function request()
{
	
	var _id,
			_controller,
			_action,
			_onSuccessEvent,
			_onFailureEvent,
			_beforeSend,
			_complete;
	
	
	_id = _lastRequestId++;
    this.parameters = new Object();
    
    
    this.getId = function()
    {
        return _id;
    };
    
    
    this.setControllerAction = function(controller, action)
    {
        _controller = controller;
        _action = action;
    };
    
    
    this.setEvents = function(onSuccess, onFailure)
    {
        _onSuccessEvent = onSuccess;
        _onFailureEvent = onFailure;
    };

    this.setEventsSuccess = function(onSuccess)
    {
        _onSuccessEvent = onSuccess;
    };

    this.setEventsFailure = function(onFailure)
    {
        _onFailureEvent = onFailure;
    };

    this.setEventsBefore = function(onBefore)
    {
        _beforeSend = onBefore;
    };

    this.setEventsComplete = function(onComplete)
    {
        _complete = onComplete;
    };
    
    
    this.send = function()
    {
    	this.parameters.id = _id;
        this.parameters.controller = _controller;
        this.parameters.action = _action;
        /*new Ajax.Request('/ajax/' + Object.toJSON(this.parameters),
        {
            method:'get',
            onSuccess: _successSend.bind(this),
            onFailure: _failureSend.bind(this)
        });*/
        
        $.ajax({
        	   type: "get",
        	   url: "/ajax/",
        	   dataType: "json",
        	   data: 'params=' + $.toJSON(this.parameters),
        	   success: this._successSend,
        	   error: this._failureSend,
	   		   beforeSend: _beforeSend,
			   complete: _complete
        	 });

        
    };
    
    
    this._successSend = function(json, status)
    {
    	var req = new request();
    	//        json = transport.responseText.evalJSON();
    	if (!json)
        {
    		req._onFailureEvent = _onFailureEvent;
        	req._failureSend();
        	return;
        }
        if (json && json.id > 0)
        {
        	if (json.error != '' && json.error != null)
            {
    	    	req._showError(json);
            }
                else
                {
                    //alert(Object.inspect(json.value));
                    if (_onSuccessEvent != null && _onSuccessEvent != '')
                    {
                        try
                        {
                        	_onSuccessEvent(json, status);
                        }
                            catch (e)
                            {
                                if (console)
                                {
                                    console.log(e);
                                }
                            }
                    }
                }
        }
            else
            {
                if (DEBUG_MODE)
                {                
        	    	var req = new request();
                	req._showError(json, 'No object returned');
                }
            }
    };





    this._failureSend = function()
	{
	    if (DEBUG_MODE)
	    {
	    	var req = new request();
	    	req._showError(null, 'Server does not respond');
	    }
	    if (this._onFailureEvent != null && this._onFailureEvent != '')
	    {
	        try
	        {
	            this._onFailureEvent();
	        }
	            catch (e)
	            {
	                if (console)
	                {
	                    console.log(e);
	                }
	            }
	    }
	};
	
	this._showError = function(json, error)
	{
	    if (error == '' || error == null)
	    {
	        if (json.error != '')
	        {
	            error = json.error;
	        }
	            else
	            {
	                error = 'AJAX error';
	            }
	    }
	    console.log(error);
	};

};
