/**
 * Unobtrusive scripting adapter for jQuery
 *
 * Requires jQuery 1.4.3 or later.
 * https://github.com/rails/jquery-ujs

 * Uploading file using rails.js
 *
 * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
 * in the remote form, this adapter aborts the AJAX submission and submits the form through standard means.
 *
 * When AJAX submission is aborted then event `ajax:aborted:file` is fired and this allows you to bind your own
 * handler to process the form submission the way you want.
 *
 * For example if you want remote form submission to be cancelled and you do not want to submit the form through
 * standard means then you can write following handler.
 *
 * Ex:
 *     $('form').live('ajax:aborted:file', function(){
 *       // Implement own remote file-transfer handler here.
 *       return false;
 *     });
 *
 * The `ajax:aborted:file` event is fired when a form is submitted and both conditions are met:
 *   a) file-type input field is detected, and
 *   b) the value of the input:file field is not blank.
 *
 * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
 * techniques like the iframe method to upload the file instead.
 *
 * Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected,
 * providing the `ajax:aborted:required` hook.
 * Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
 *
 * The default behavior of aborting the remote form submission when required inputs are missing, may be
 * changed (thereby submitting the form via AJAX anyway) by binding a handler function that returns
 * false to the `ajax:aborted:required` hook.
 *
 * Ex:
 *     $('form').live('ajax:aborted:required', function(){
 *       return ! confirm("Would you like to submit the form with missing info?");
 *     });
 */
(function(a){var b;a.rails=b={CSRFProtection:function(b){var c=a('meta[name="csrf-token"]').attr("content");c&&b.setRequestHeader("X-CSRF-Token",c)},fire:function(b,c,d){var e=a.Event(c);return b.trigger(e,d),e.result!==!1},handleRemote:function(c){var d,e,f,g=c.data("type")||a.ajaxSettings&&a.ajaxSettings.dataType;if(b.fire(c,"ajax:before")){if(c.is("form")){d=c.attr("method"),e=c.attr("action"),f=c.serializeArray();var h=c.data("ujs:submit-button");h&&(f.push(h),c.data("ujs:submit-button",null))}else d=c.data("method"),e=c.attr("href"),f=null;a.ajax({url:e,type:d||"GET",data:f,dataType:g,beforeSend:function(a,d){return d.dataType===undefined&&a.setRequestHeader("accept","*/*;q=0.5, "+d.accepts.script),b.fire(c,"ajax:beforeSend",[a,d])},success:function(a,b,d){c.trigger("ajax:success",[a,b,d])},complete:function(a,b){c.trigger("ajax:complete",[a,b])},error:function(a,b,d){c.trigger("ajax:error",[a,b,d])}})}},handleMethod:function(b){var c=b.attr("href"),d=b.data("method"),e=a("meta[name=csrf-token]").attr("content"),f=a("meta[name=csrf-param]").attr("content"),g=a('<form method="post" action="'+c+'"></form>'),h='<input name="_method" value="'+d+'" type="hidden" />';f!==undefined&&e!==undefined&&(h+='<input name="'+f+'" value="'+e+'" type="hidden" />'),g.hide().append(h).appendTo("body"),g.submit()},disableFormElements:function(b){b.find("input[data-disable-with], button[data-disable-with]").each(function(){var b=a(this),c=b.is("button")?"html":"val";b.data("ujs:enable-with",b[c]()),b[c](b.data("disable-with")),b.attr("disabled","disabled")})},enableFormElements:function(b){b.find("input[data-disable-with]:disabled, button[data-disable-with]:disabled").each(function(){var b=a(this),c=b.is("button")?"html":"val";b.data("ujs:enable-with")&&b[c](b.data("ujs:enable-with")),b.removeAttr("disabled")})},allowAction:function(a){var c=a.data("confirm");return!c||b.fire(a,"confirm")&&confirm(c)},blankInputs:function(b,c){var d=!1,e=c||"input";return b.find(e).each(function(){if(!a(this).val())return d=!0,!1}),d},nonBlankInputs:function(b,c){var d=!1,e=c||"input";return b.find(e).each(function(){if(a(this).val())return d=!0,!1}),d},stopEverything:function(a){return a.stopImmediatePropagation(),!1},callFormSubmitBindings:function(b){var c=b.data("events"),d=!0;return c!==undefined&&c.submit!==undefined&&a.each(c.submit,function(a,b){if(typeof b.handler=="function")return d=b.handler(b.data)}),d}},"ajaxPrefilter"in a?a.ajaxPrefilter(function(a,c,d){b.CSRFProtection(d)}):a(document).ajaxSend(function(a,c){b.CSRFProtection(c)}),a("a[data-confirm], a[data-method], a[data-remote]").live("click.rails",function(c){var d=a(this);if(!b.allowAction(d))return b.stopEverything(c);if(d.data("remote")!==undefined)return b.handleRemote(d),!1;if(d.data("method"))return b.handleMethod(d),!1}),a("form").live("submit.rails",function(c){var d=a(this),e=d.data("remote")!==undefined;if(!b.allowAction(d))return b.stopEverything(c);if(b.blankInputs(d,"input[name][required]")&&b.fire(d,"ajax:aborted:required"))return!e;if(b.nonBlankInputs(d,"input:file"))return b.fire(d,"ajax:aborted:file");if(!a.support.submitBubbles&&b.callFormSubmitBindings(d)===!1)return b.stopEverything(c);if(e)return b.handleRemote(d),!1;setTimeout(function(){b.disableFormElements(d)},13)}),a("form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])").live("click.rails",function(){var c=a(this);if(!b.allowAction(c))return b.stopEverything(e);var d=c.attr("name"),f=d?{name:d,value:c.val()}:null;c.closest("form").data("ujs:submit-button",f)}),a("form").live("ajax:beforeSend.rails",function(c){this==c.target&&b.disableFormElements(a(this))}),a("form").live("ajax:complete.rails",function(c){this==c.target&&b.enableFormElements(a(this))})})(jQuery)
