/**
 * The behavior definitions library for RightJS
 *
 * See http://rightjs.org/goods/behave for more details
 *
 * Copyright (C) 2009-2010 Nikolay V. Nemshilov
 */
var Behavior=new Class({extend:{add:function(){var a=$A(arguments);var b=new Behavior(a.shift());return b.on.apply(b,a).start()},stop:function(r){var b=Behavior.active[r];if(b)b.stop();return b},delegate:function(r){return function(a){var e=$(a.target);for(var s in r)if(e.match(s))return r[s].apply(this,arguments)}},refresh:function(){for(var k in Behavior.active)Behavior.active[k].refresh()},active:{}},rule:null,args:null,regs:null,initialize:function(){var a=$A(arguments);this.rule=a.shift();this.on.apply(this,a);this.regs=[]},start:function(){return Behavior.active[this.rule]=this.refresh()},stop:function(){var a=this.args;if(!isHash(a[0])){var h={};h[a.shift()]=a;a=h}else a=a[0];$$(this.rule).each(function(e){var u=$uid(e);if(this.regs[u])for(var k in a){if(isArray(a[k])){a[k].each(function(o){if(isArray(o))e.stopObserving.apply(e,[k].concat(options[0]))});if(!isArray(a[k][0]))e.stopObserving.apply(e,[k].concat(a[k][0]))}else e.stopObserving.apply(e,[k].concat(a[k]))}},this);this.regs=[];delete(Behavior.active[this.rule]);return this},active:function(){return Behavior.active[this.rule]===this},on:function(){this.args=$A(arguments);return this},refresh:function(){$$(this.rule).each(function(e){var u=$uid(e);if(!this.regs[u]){e.on.apply(e,this.args);this.regs[u]=1}},this);return this}});document.onReady(Behavior.refresh);Element.include((function(a){var o=a.insert;var b=a.update;return{insert:function(){o.apply(this,arguments);Behavior.refresh();return this},update:function(c){b.apply(this,arguments);if(isString(c))Behavior.refresh();return this}}})(Element.Methods));$ext(String.prototype,{behave:function(){return Behavior.add.apply(Behavior,[''+this].concat($A(arguments)))},stopBehave:function(){return Behavior.stop.apply(Behavior,[''+this].concat($A(arguments)))}});