/**
 * Ruby On Rails common Ajax operations conventional wrapper
 * and underscored aliases for core methods
 *
 *    http://github.com/MadRabbit/right-rails
 *
 * Copyright (C) 2009-2010 Nikolay V. Nemshilov
 */
var RR={Options:{format:'js',flashId:'flashes',flashHideFx:'slide',flashHideDelay:3200,highlightUpdates:true,removeFx:'fade',linkToAjaxEdit:'.ajax_edit',linkToAjaxDelete:'.ajax_delete',rescanWithScopes:true},update_flash:function(c){var e=$(this.Options.flashId);if(e)this.replace(e,c).hide_flash();return this},hide_flash:function(){if(this.Options.flashHideDelay>-1){var e=$(this.Options.flashId);if(e&&e.visible())e.hide.bind(e,this.Options.flashHideFx).delay(this.Options.flashHideDelay)}return this},highlight:function(i){if(this.Options.highlightUpdates)$(i).highlight();return this},insert:function(a,w){return this.highlight($(a).insert(w).lastChild).rescan(a)},replace:function(i,s){$(i).replace(s);return this.highlight(i).rescan(i)},remove:function(i){var e=$(i);if(e){var r=e.remove.bind(e);if(this.Options.removeFx)e.hide(this.Options.removeFx,{onFinish:r});else r()}},remotize_form:function(i){var f=$(i);if(f)f.remotize().enable().action+='.'+this.Options.format;return this},replace_form:function(i,s){var f=$(i);if(f){f.replace(s);this.remotize_form(i)}return this.rescan(i)},show_form_for:function(i,s){$(i).select('form').each('remove');$(i).insert(s);return this.remotize_form($(i).first('form')).rescan(i)},process_click:function(e){var t=e.target,l=[t].concat(t.parents()).first('match','a');if(l){if(l.match(this.Options.linkToAjaxEdit)){e.stop();Xhr.load(l.href+'.'+this.Options.format)}else if(l.match(this.Options.linkToAjaxDelete)&&l.has('onclick')){e.stop();eval('({f:'+l.onclick.toString().replace('.submit','.send')+'})').f.call(l)}}},rescan:function(s){$w('Draggable Droppable Tabs Slider Selectable').each(function(n){if(self[n])self[n].rescan(this.Options.rescanWithScopes?s:null)},this);return this}};(function(){var u=function(a,e){var m=e.get('data-confirm');if(m&&!confirm(m)){a.stop();return true}};var c=function(e,o){return Object.merge({onCreate:function(){e.fire('ajax:loading',this)},onComplete:function(){e.fire('ajax:complete',this)},onSuccess:function(){e.fire('ajax:success',this)},onFailure:function(){e.fire('ajax:failure',this)}},o)};var g=function(e,l){var m=l.get('data-method'),r=l.get('data-remote');if(u(e,l))return;if(m||r)e.stop();if(r)Xhr.load(l.href,c(l,{method:m||'get',spinner:l.get('data-spinner')}));else if(m){var p=$$('meta[name=csrf-param]')[0],t=$$('meta[name=csrf-token]')[0],f=$E('form',{action:l.href,method:'post'});if(p&&t)f.insert('<input type="hidden" name="'+p.get('content')+'" value="'+t.get('content')+'" />');f.insert('<input type="hidden" name="_method" value="'+m+'"/>').insertTo(document.body).submit()}};var d=function(e,b){if(!u(e,b)&&$(b.form).has('data-remote')){e.stop();b.form.send(c(b.form))}};document.on({click:function(e){var t=e.target,f=t.form,l=[t].concat(t.parents()).first('match','a');if(f&&['submit','image'].include(t.type))d(e,t);else if(l)g(e,l)},keydown:function(e){var t=e.target,f=t.form;if(f&&t.tagName==='INPUT'&&e.keyCode==13)d(e,t)}})})();document.on({ready:function(){RR.hide_flash()},click:function(e){RR.process_click(e)}});[String.prototype,Array.prototype,Function.prototype,Object,Options,Observer,Observer.prototype,window,document].each(function(o){for(var k in o)try{if(/[A-Z]/.test(k)&&typeof(o[k])==='function'){var u=k.underscored();if(o[u]===null||o[u]===undefined)o[u]=o[k]}}catch(e){}});[Element,Event,Form,Form.Element].each(function(o){var a={},m=o.Methods;for(var k in m)if(/[A-Z]/.test(k)&&typeof(m[k])==='function')a[k.underscored()]=m[k];o.include(a)});$alias(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'});$alias(Array.prototype,{collect:'map',detect:'filter',index_of:'indexOf',last_index_of:'lastIndexOf',index:'indexOf',rindex:'lastIndexOf'});