/**
 * Rails plugin 1.0
 *
 * Copyright (c) 2008 Blit, Inc (http://blit.com/)
 * MIT license (blit.com/licenses/mit.html)
 */

jQuery.fn.rest = function(method,data,options) {
  var ajax_args = {dataType: 'script'}
  if (!options) options = {};
  if (options.url) ajax_args.url = options.url
  else if (this.is('a')) ajax_args.url = '/'+this[0].href.replace(/http:\/\/.+?\//,'')
  else if (this.is('form')) {
    ajax_args.url = this[0].action;
    data = this.serialize();
  }
  if (!data) data = {};
  if (method=='get' || method=='post') ajax_args.type = method;
  else {
    ajax_args.type = 'POST';
    if (typeof( data ) == 'string') data = '_method=' + method + '&' + data;
    else data['_method'] = method;
  }
  ajax_args.data = data;
  jQuery.ajax(ajax_args);
  return this;
}
