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