/**
 * RubyOnRails Support Module v2.2.1
 * http://rightjs.org/plugins/rails
 *
 * Copyright (C) 2009-2011 Nikolay Nemshilov
 */
(function(a,b,c){var d=c,e=c.$,f=c.$$,g=c.$E,h=c.Xhr,i=c.Object;c.Rails={version:"2.2.1"},d([c.String.prototype,c.Array.prototype,c.Function.prototype,c.Object,c.Options,c.Observer,c.Observer.prototype,a,b]).each(function(a){for(var b in a)try{if(/[A-Z]/.test(b)&&typeof a[b]==="function"){var c=d(b).underscored();if(a[c]===null||a[c]===undefined)a[c]=a[b]}}catch(e){}}),d([c.Element,c.Event,c.Form,c.Input]).each(function(a){if(a){var b={},c=a.prototype;for(var e in c)/[A-Z]/.test(e)&&typeof c[e]==="function"&&(a.prototype[d(e).underscored()]=c[e])}}),c.$alias(c.String.prototype,{index_of:"indexOf",last_index_of:"lastIndexOf",to_f:"toFloat",to_i:"toInt",gsub:"replace",downcase:"toLowerCase",upcase:"toUpperCase",index:"indexOf",rindex:"lastIndexOf",strip:"trim"}),c.$alias(c.Array.prototype,{collect:"map",detect:"filter",index_of:"indexOf",last_index_of:"lastIndexOf",index:"indexOf",rindex:"lastIndexOf"});var j=function(a,b){var c=b.get("data-confirm");if(c&&!confirm(c)){a.stop();return!0}},k=function(a,b){return i.merge({onCreate:function(){a.fire("ajax:loading",{xhr:this})},onComplete:function(){a.fire("ajax:complete",{xhr:this})},onSuccess:function(){a.fire("ajax:success",{xhr:this})},onFailure:function(){a.fire("ajax:failure",{xhr:this})}},b)},l=function(a){var c=a.target,d=c.get("data-method"),e=c.get("data-remote"),i=c.get("href");if(!j(a,c)){(d||e)&&a.stop();if(e)h.load(i,k(c,{method:d||"get",spinner:c.get("data-spinner")}));else if(d){var l=f("meta[name=csrf-param]")[0],m=f("meta[name=csrf-token]")[0],n=g("form",{action:i,method:"post"});l&&m&&n.insert('<input type="hidden" name="'+l.get("content")+'" value="'+m.get("content")+'" />'),n.insert('<input type="hidden" name="_method" value="'+d+'"/>').insertTo(b.body).submit()}}};e(b).on({click:function(a){var b=a.target._.tagName;(b==="A"||b==="BUTTON")&&l(a)},submit:function(a){var b=a.target;b.has("data-remote")&&!j(a,b)&&(a.stop(),b.send(k(b,{spinner:b.get("data-spinner")||b.first(".spinner")})))}});var m={Options:{format:"js",flashId:"flashes",flashHideFx:"slide",flashHideDelay:3200,highlightUpdates:!0,removeFx:"fade",insertFx:"fade",insertPosition:"bottom",linkToAjaxEdit:".ajax_edit",linkToAjaxDelete:".ajax_delete",rescanWithScopes:!0},update_flash:function(a){var b=e(this.Options.flashId);b&&this.replace(b,a).hide_flash();return this},hide_flash:function(){if(this.Options.flashHideDelay>-1){var a=e(this.Options.flashId);a&&a.visible()&&a.hide.bind(a,this.Options.flashHideFx).delay(this.Options.flashHideDelay)}return this},highlight:function(a){e(a)&&this.Options.highlightUpdates&&e(a).highlight();return this},insert:function(a,b,c){var d=c||this.Options.insertPosition,f,g=e(a).insert(b,d);switch(d){case"bottom":f=g.children().last();break;case"top":f=g.first();break;case"before":f=g.prev();break;case"after":f=g.next()}f&&this.Options.insertFx?f.hide().show(this.Options.insertFx,{onFinish:this.highlight.bind(this,f)}):this.highlight(f);return this.rescan(a)},replace:function(a,b){e(a).replace(b);return this.highlight(a).rescan(a)},remove:function(a){e(a)&&e(a).remove(this.Options.removeFx)},remotize_form:function(a){var b=e(a);b&&(b.remotize().enable()._.action+="."+this.Options.format);return this},replace_form:function(a,b){var c=e(a);c&&(c.replace(b),this.remotize_form(a));return this.rescan(a)},show_form_for:function(a,b){e(a).find("form").each("remove"),e(a).insert(b);return this.remotize_form(e(a).first("form")).rescan(a)},process_click:function(a){var b;(b=a.find("a"+this.Options.linkToAjaxEdit))?(a.stop(),h.load(b.get("href")+"."+this.Options.format)):(b=a.find("a"+this.Options.linkToAjaxDelete))&&b.has("onclick")&&(a.stop(),(new Function("return "+b.onclick.toString().replace(".submit",".send")))().call(b))},rescan:function(b){$w("Draggable Droppable Tabs Slider Selectable").each(function(c){c in a&&a[c].rescan(this.Options.rescanWithScopes?b:null)},this);return this}};e(b).on({ready:function(){m.hide_flash()},click:function(a){m.process_click(a)}}),a.RR=m})(window,document,RightJS)