// sanitycheck
if (! window.jQuery) throw "RPC2 lib needs jQuery library";

// setup RPC system
;(function($) {
  $.RPC = function(options) {
    if (! window.jQuery.toJSON)
      throw "RPC2 lib needs jquery.json library";

    var defaults = {
      namespace     : 'global',
      listener      : $(document),
      on_rpc_error  : function() {
        this.listener.trigger(options.namespace + '.rpc-error', arguments);
      },
      on_ajax_error : function() {
        this.listener.trigger(options.namespace + '.ajax-error', arguments);
      }
    };
    var options = $.extend({}, defaults, options);
    this.__call__ = function(type, endpoint, args) {
      var encoder   = type == 'POST' ? $.toJSON : $.param;
      var params    = {};
      var callback  = null;

      if      (args.length == 2)  params = args[0], callback = args[1];
      else if (args.length == 1)  callback = args[0];

			if (typeof(params) == 'undefined')
				params = {};

      if (typeof callback != 'function')
        return false;

      return $.ajax({
        type        : type,
        url         : 'http://www.ilse.nl/RPC2/' + endpoint,
        contentType : type == 'POST' ? 'application/json' : 'application/x-www-form-urlencoded',
        success     : function(response, code, req) {
          if (response.error)
            options.on_rpc_error(response.error, code, req);
          else
            callback(response.result);
        },
        error       : $.proxy(options.on_ajax_error, options),
        data        : encoder(params)
      });
    };
    
      this['nuwerk'] = {
        
          'geocode' : $.proxy(function() { return this.__call__('POST', 'nuwerk/geocode', arguments) }, this )
          ,
        
          'search' : $.proxy(function() { return this.__call__('POST', 'nuwerk/search', arguments) }, this )
          
        
      };
    
      this['users'] = {
        
          'get_status' : $.proxy(function() { return this.__call__('POST', 'users/get_status', arguments) }, this )
          ,
        
          'login' : $.proxy(function() { return this.__call__('POST', 'users/login', arguments) }, this )
          ,
        
          'logout' : $.proxy(function() { return this.__call__('POST', 'users/logout', arguments) }, this )
          
        
      };
    
      this['geocoder'] = {
        
          'geocode' : $.proxy(function() { return this.__call__('POST', 'geocoder/geocode', arguments) }, this )
          
        
      };
    
      this['goudengids'] = {
        
          'geocode' : $.proxy(function() { return this.__call__('POST', 'goudengids/geocode', arguments) }, this )
          ,
        
          'search' : $.proxy(function() { return this.__call__('POST', 'goudengids/search', arguments) }, this )
          
        
      };
    
      this['koenstest'] = {
        
          'get_top' : $.proxy(function() { return this.__call__('POST', 'koenstest/get_top', arguments) }, this )
          
        
      };
    
      this['favorites'] = {
        
          'get_all_links' : $.proxy(function() { return this.__call__('POST', 'favorites/get_all_links', arguments) }, this )
          ,
        
          'get_links' : $.proxy(function() { return this.__call__('POST', 'favorites/get_links', arguments) }, this )
          ,
        
          'set_link' : $.proxy(function() { return this.__call__('POST', 'favorites/set_link', arguments) }, this )
          
        
      };
    
      this['lite'] = {
        
          'categories' : $.proxy(function() { return this.__call__('POST', 'lite/categories', arguments) }, this )
          ,
        
          'geocode' : $.proxy(function() { return this.__call__('POST', 'lite/geocode', arguments) }, this )
          ,
        
          'geocodeview' : $.proxy(function() { return this.__call__('POST', 'lite/geocodeview', arguments) }, this )
          ,
        
          'homepage' : $.proxy(function() { return this.__call__('POST', 'lite/homepage', arguments) }, this )
          ,
        
          'search' : $.proxy(function() { return this.__call__('POST', 'lite/search', arguments) }, this )
          
        
      };
    
      this['marktplaats'] = {
        
          'search' : $.proxy(function() { return this.__call__('POST', 'marktplaats/search', arguments) }, this )
          
        
      };
    
      this['dossiers'] = {
        
          'add_document' : $.proxy(function() { return this.__call__('POST', 'dossiers/add_document', arguments) }, this )
          ,
        
          'add_dossier' : $.proxy(function() { return this.__call__('POST', 'dossiers/add_dossier', arguments) }, this )
          ,
        
          'delete_document' : $.proxy(function() { return this.__call__('POST', 'dossiers/delete_document', arguments) }, this )
          ,
        
          'delete_dossier' : $.proxy(function() { return this.__call__('POST', 'dossiers/delete_dossier', arguments) }, this )
          ,
        
          'get_document_list' : $.proxy(function() { return this.__call__('POST', 'dossiers/get_document_list', arguments) }, this )
          ,
        
          'get_dossier_list' : $.proxy(function() { return this.__call__('POST', 'dossiers/get_dossier_list', arguments) }, this )
          ,
        
          'move_document' : $.proxy(function() { return this.__call__('POST', 'dossiers/move_document', arguments) }, this )
          ,
        
          'move_dossier' : $.proxy(function() { return this.__call__('POST', 'dossiers/move_dossier', arguments) }, this )
          
        
      };
    
  };
})(jQuery);

