Array.prototype.inArray=function(b){var a;for(a=0;a<this.length;a++){if(this[a]===b){return true}}return false};Array.prototype.removeItem=function(b){var a;for(a=0;a<this.length;a++){if(this[a]===b){this.splice(a,1)}}};Hydrometeor={callbacks:{process:""},channels:{},channelcount:0,backtrackqueue:new Array(),path:null,status:0,xhr:null,xhrdata:{},init:function(){if(!Hydrometeor.path){throw"Hydrometeor path not specified"}if(Hydrometeor.path.charAt(Hydrometeor.path.length)=="/"){Hydrometeor.path=Hydrometeor.path.slice(0,Hydrometeor.path.length-1)}Hydrometeor.updateXhrData()},joinChannel:function(a,b){if(typeof(Hydrometeor.channels[a])!="undefined"){throw"Cannot join channel "+a+": already subscribed"}Hydrometeor.addChannel(a,b);Hydrometeor.updateXhrData();if(Hydrometeor.status!=0&&Hydrometeor.channelcount!=0){if(typeof(b)=="number"){Hydrometeor.doBacktrack(a,Hydrometeor.connect)}}else{if(Hydrometeor.status==0&&Hydrometeor.channelcount!=0){if(typeof(b)=="number"){Hydrometeor.backtrackqueue.push(a)}}}},leaveChannel:function(a){if(typeof(Hydrometeor.channels[a])=="undefined"){throw"Cannot leave channel "+a+": not subscribed"}Hydrometeor.deleteChannel(a);Hydrometeor.updateXhrData();if(Hydrometeor.status!=0&&Hydrometeor.channelcount!=0){Hydrometeor.connect()}else{Hydrometeor.disconnect()}},addChannel:function(a,b){Hydrometeor.channels[a]={};if(b){Hydrometeor.channels[a].backtrack=b}Hydrometeor.channelcount++},deleteChannel:function(a){delete (Hydrometeor.channels[a]);Hydrometeor.channelcount--},doBacktrack:function(channelName,callback){$.get(Hydrometeor.path+"/backlog",{channel:channelName,count:Hydrometeor.channels[channelName].backtrack,callback:Hydrometeor.callbacks.process},function(msg){m=msg.split("\n");for(var i=0;i<m.length;i++){if(m[i]!=""){var id=m[i].indexOf(",");if(id==-1){throw"Message received from Hydrometeor had no id!"}Hydrometeor.updateSince(m[i].substr(0,id));eval(m[i].substr(id+1))}}delete (Hydrometeor.channels[channelName].backtrack);Hydrometeor.backtrackqueue.removeItem(channelName);if(Hydrometeor.backtrackqueue.length==0&&callback){callback()}})},connect:function(){if(!Hydrometeor.path){throw"Hydrometeor path not specified"}if(Hydrometeor.channelcount==0){throw"No channels specified"}if(!Hydrometeor.callbacks.process){throw"No process callback specified"}if(!typeof(Hydrometeor.callbacks.process)=="string"){throw"Specify process callback as a string = name of function"}if(Hydrometeor.status!=0){Hydrometeor.disconnect()}Hydrometeor.setstatus(1);if(Hydrometeor.backtrackqueue.length>0){for(var i=0;i<Hydrometeor.backtrackqueue.length;i++){Hydrometeor.doBacktrack(Hydrometeor.backtrackqueue[i],Hydrometeor.connect)}}else{Hydrometeor.xhr=$.ajax({type:"GET",url:Hydrometeor.path+"/subscribe",data:Hydrometeor.xhrdata,cache:false,success:function(msg){m=msg.split("\n");for(var i=0;i<m.length;i++){if(m[i]!=""){var id=m[i].indexOf(",");if(id==-1){throw"Message received from Hydrometeor had no id!"}Hydrometeor.updateSince(m[i].substr(0,id));eval(m[i].substr(id+1))}}Hydrometeor.connect()},error:function(){throw"Some sort of AJAX error?"}})}},disconnect:function(){if(Hydrometeor.status!=0){Hydrometeor.setstatus(0);if(Hydrometeor.xhr){Hydrometeor.xhr.abort()}}},updateXhrData:function(){if(Hydrometeor.channelcount!=0){Hydrometeor.xhrdata.channel=[];for(var a in Hydrometeor.channels){Hydrometeor.xhrdata.channel.push(a)}}else{delete (Hydrometeor.xhrdata.channel)}Hydrometeor.xhrdata.callback=Hydrometeor.callbacks.process},updateSince:function(a){var a=parseInt(a);Hydrometeor.xhrdata.since=(a>Hydrometeor.xhrdata.since||!Hydrometeor.xhrdata.since)?a:Hydrometeor.xhrdata.since},setstatus:function(a){if(Hydrometeor.status!=a){Hydrometeor.status=a}},registerEventCallback:function(a,b){Hydrometeor.callbacks[a]=b;Hydrometeor.updateXhrData()}};if(typeof jQuery=="undefined"){throw"jQuery has not been loaded!"};