// PricklyBase.js
var PricklyBase=Class.create({'guid':'','base_url':'','host':'localhost','port':80,'attempts':0,'MAXIMUM_ATTEMPTS':4,'DELAY':0.5,'_reload':function(){window.location.reload();},'_get_base_url':function(){var url='';if(this.base_url){url=this.base_url;}
else{var parts=window.location.href.toUrlParts();url=parts['protocol']+'://'+parts['authority']+parts['path'];}
if(url.endsWith('/')){url+='@@index.html';}
return url;}});

// Broadcaster.js
var Broadcaster=Class.create(PricklyBase,{'handle':function(event){if(hasattr(event,'memo')){var memo=event.memo;var params='';if(getattr(memo,'params',false)){params=Object.toJSON(memo.params);}
this.dispatch(Object.extend(event.memo,{'guid':this.guid,'params':params}));}},'dispatch':function(event){var url=this.base_url+"/@@input/"+this.guid;var params=$H(event).toQueryString();new Ajax.Request(url,{'parameters':params,'onSuccess':function(){this.attempts=0;},'onFailure':function(){this.attempts+=1;if(this.attempts>this.MAXIMUM_ATTEMPTS){this._reload();}
this.send.delay(this.DELAY,event);}.bind(this)});return true;},'initialize':function(){this.base_url=this._get_base_url();observe('broadcast:requested',this.handle.bindAsEventListener(this));}});

// Receiver.js
var Receiver=Class.create(PricklyBase,{'request':null,'SUPPORTED_EVENTS':['idle','error','userevent','slotevent','slotsupdate','slotuserevent'],'_clear':function(){delete this.request;this.request=null;},'_stop':function(){var request_in_progress=false;if(this.request){var state=this.request.transport.readyState;request_in_progress=(state>0&&state<4);}
if(request_in_progress){this.request.transport.abort();this._clear();}
fire('broadcast:requested',{'name':'close','guid':this.guid});},'_evaluate':function(response){var event=response.evalJSON(true);var keys=Object.keys(event);if(keys.include('name')&&this.SUPPORTED_EVENTS.include(event.name)){if(keys.include('params')&&typeof(event.params)==typeof('')&&event.params){event.params=event.params.evalJSON(true);}
return event}
return false;},'notify':function(slot,event){var name=slot+':requested';fire(name,event);},'handle':function(event){var name=getattr(event,'name',false);if(name){switch(name){case'error':warn('Received error: '+event);break;case'slotevent':if(hasattr(event,'slots')){event.slots.each(function(slot){this.notify(slot,event);}.bind(this));}
else{warn('Invalid slotevent (no slots): '+event);}
break;case'slotsupdate':break;case'slotuserevent':if(hasattr(event,'slots')){event.slots.each(function(slot){this.notify(slot,event);}.bind(this));}
else{warn('Invalid slotevent (no slots): '+event);}
break;case'userevent':if(hasattr(event,'action')&&['login','logout'].include(event.action)){var name='user:'+event.action.sub('log','logged');fire(name,event);}
break;}}
else{warn('Invalid event (no name): '+event);}},'_poll':function(){if(this.request){this.poll.delay(this.DELAY);}
else{this.request=new Ajax.Request(this.base_url+"/@@output/"+this.guid,{'method':'get','onSuccess':function(transport){var response=transport.responseText;var event=this._evaluate(response);if(event){this.handle(event);}
else{warn('failed to evaluate response :(');}}.bind(this),'onFailure':function(){this.attempts+=1;if(this.attempts>this.MAXIMUM_ATTEMPTS){this._reload();}}.bind(this),'onComplete':function(){this.poll.delay(this.DELAY);this._clear();}.bind(this)});}},'initialize':function(){this.base_url=this._get_base_url();this.poll=this._poll.bind(this);this.poll();Event.observe(window,'unload',this._stop.bind(this));}});

// ProxyParser.js
var ProxyParser={'cache':new Hash(),'_unpack':function(n){if(typeof(n)=='function'){return n.bind(this)();}
else{return n;}},'notify':function(n,d){fire('broadcast:requested',{'name':n,'slots':d['slots'],'method':d['method'],'params':d['params']});},'handle':function(event){event.stop();var unpack=ProxyParser._unpack.bind(event);var we_want_to_go_ahead=unpack(this['filter']);if(we_want_to_go_ahead){var unpacked={};['slots','method','params'].each(function(atr){unpacked[atr]=unpack(this[atr]);}.bind(this));ProxyParser.notify(this['name'],unpacked);};}};

// Event.broadcast.js
Object.extend(Event,{'broadcast':function(target,trigger,name,filter,slots,method,params){var context={'name':name,'filter':filter,'slots':slots,'method':method,'params':params};var url=SHA1.hash(Object.toJSON(context));var handler=ProxyParser.handle.bindAsEventListener(context);ProxyParser.cache.set(url,handler);Event.observe(target,trigger,handler);},'stopBroadcasting':function(target,trigger,name,filter,slots,method,params){var context={'name':name,'filter':filter,'slots':slots,'method':method,'params':params};var url=SHA1.hash(Object.toJSON(context));var handler=ProxyParser.cache.get(url);Event.stopObserving(target,trigger,handler);}});


