(
  function($)
  {
    var config = {};
    var methods =
    {
      init: function ()
      {
        eval('config = ' + $(this).attr('data-db-search'))
        var options =
        {
          source: function(request, response)
          {
            $(this).search_db('fetch', request, response);
          },

          select: function(event, ui)
          {
            if (config.input)
            {
              $('input[name=' + config.input + ']').val(ui.item.id);
            }

          }
        }
        $(this).autocomplete(options);
      },

      fetch: function(request, response)
      {
        var option = config;
        option.term = request.term;

        var success = function(result)
        {
          eval('result = ' + result);
          response(result);
        }

        var call =
        {
          url: '/resources/php/ajax/search.php',
          data: option,
          success: success
        }
        $.ajax(call);
      }
    }

    $.fn.search_db = function(method)
    {
      // method calling logic
      if (methods[method])
      {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
      }
      else if(typeof method === 'object' || !method)
      {
        return methods.init.apply(this, arguments);
      }
      else
      {
        $.error( 'Method ' +  method + ' does not exist on jQuery.search_db' );
      }
    };
  }
)
(jQuery);

/* php interface
$db_search = array
(
  'database' => DATABASE,
  'table' => 'user',
  'field' => array('username'),
  'search' => array('username'),
  'map' => array('username' => 'label'),
  'filter' => array
  (
    array
    (
      'field' => 'admintype',
      'value' => 'advertiser',
    ),
  ),
  'input' => 'user',
);

$input['user-search'] = array
(
  'type' => 'text',
  'label' => 'Select New User',
  'width' => 250,
  'property' => array('data-db-search' => json($db_search)),
);

$input['user'] = array
(
  'type' => 'hidden',
);
*/
