jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })

function _ajax_request(url, data, callback, type, method) {

    if (jQuery.isFunction(data)) {
        /*
          checks if data is a function if
          assigns data to callback var to be run on success:
        */
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
}); 

/*  Another way to define methods within the element namespace is to 
    define jQuery.fn.nameOfFunction = function(){ };
    
    jQuery.fn.extend({
      check: function() {
        return this.each(function() { this.checked = true; });
      },
      uncheck: function() {
        return this.each(function() { this.checked = false; });
      }
    });

*/

jQuery.fn.submitWithAjax = function() {
  this.unbind('submit', false);
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  
  })
  return this;
};

//Send data via get if JS enabled
jQuery.fn.getWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.get($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

//Send data via Post if JS enabled
jQuery.fn.postWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.post($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};


jQuery.fn.putWithAjax = function() {
  this.unbind('click', false);
  this.click(function() {
    $.put($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};


jQuery.fn.deleteWithAjax = function() {
  this.removeAttr('onclick');
  this.unbind('click', false);
  this.click(function() {
    $.delete_($(this).attr("href"), $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

var j = jQuery.noConflict();

//This will "ajaxify" the links
function ajaxLinks(){

    j('.ajaxForm').submitWithAjax();
    
    // I need to write a hook that lets me know that form was submitted
    // clears the form
    
    j('a.get').getWithAjax();
    j('a.post').postWithAjax();
    j('a.put').putWithAjax();
    j('a.delete').deleteWithAjax();
}



j(document).ready(function() {

// All non-GET requests will add the authenticity token
  // if not already present in the data packet
 j(document).ajaxSend(function(event, request, settings) {
       if (typeof(window.AUTH_TOKEN) == "undefined") return;
       // IE6 fix for http://dev.jquery.com/ticket/3155
       if (settings.type == 'GET' || settings.type == 'get') return;

       settings.data = settings.data || "";
       settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
     });

  ajaxLinks();
});
