XHR.Comet=new Class({tunnel:null,type:0,nextReadPos:0,options:{url:'',name:"MooComet",onPush:Class.empty},init:function(){switch(this.type){case 3:this.tunnel=new ActiveXObject("htmlfile");break;case 2:this.tunnel=document.createElement("event-source");break;default:this.tunnel=new XHR.Comet.XHR(this.options.url,this.options);this.tunnel.addCallback(this.onChange.bind(this),{readyState:false,status:false});break;};if(this.type==4){this.tunnel.transport.multipart=true;}},setUrl:function(url){if(this.type>1){this.options.url=url+(url.search("\\?")>-1?"&":"?")+"cometType="+this.type+"&cometName="+this.options.name;}
else{this.options.url=url;}},initialize:function(options){this.setOptions(options);this.type=(window.ie?3:(window.opera?2:1));if(this.type>1){this.setUrl(this.options.url);}
if(window.gecko){this.type=4;}
this.init();return this;},cancel:function(){this.nextReadPos=0;if(this.type==3){this.tunnel.body.innerHTML="<iframe src='about:blank'></iframe>";}
else if(this.type==2){document.body.removeChild(this.tunnel);}
else{if($defined(this.tunnel)){this.tunnel.cancel();this.tunnel=null;this.tunnel=new XHR.Comet.XHR(this.options.url,this.options);if(this.type==4){this.tunnel.transport.multipart=true;}}}
return this;},send:function(){if(this.type==3){this.tunnel.open();this.tunnel.write("<html><body></body></html>");this.tunnel.close();this.tunnel.parentWindow._cometObject=this;this.tunnel.body.innerHTML="<iframe src='"+this.options.url+"'></iframe>";}
else if(this.type==2){this.tunnel.setAttribute("src",this.options.url);document.body.appendChild(this.tunnel);this.tunnel.addEventListener(this.options.name,this.onChange.bind(this),false);}
else{this.tunnel.send(this.options.url,{data:{'cometType':this.type,'cometName':this.options.name}});}
return this;},pollLatestResponse:function(text){var seplen=("<end />").length;var anUpdate=false;var unprocessed=text.substring(this.nextReadPos);var messageXMLEndIndex=unprocessed.indexOf("<end />");if(messageXMLEndIndex!=-1){var endOfFirstMessageIndex=messageXMLEndIndex+seplen;anUpdate=unprocessed.substring(0,endOfFirstMessageIndex-seplen);this.nextReadPos+=endOfFirstMessageIndex;}
return anUpdate;},onChange:function(text,xml){var response=null;switch(this.type){case 2:response=arguments[0].data;break;case 4:if(text==='')return true;response=text;break;default:response=this.pollLatestResponse(text);break;};this.fireEvent('onPush',response);}});XHR.Comet.implement(new Events,new Options);XHR.Comet.XHR=XHR.extend({callbacks:[],check:function(){return true;},addCallback:function(fn,options){options=$extend({fn:Class.empty,readyState:4,status:200},options);this.callbacks.push($extend(options,{fn:fn}));return this;},onStateChange:function(){this.status=0;try{this.status=this.transport.status;}catch(e){}
this.response={'text':this.transport.responseText,'xml':this.transport.responseXML};this.callbacks.forEach(function(callback){if(callback.readyState!=false&&callback.readyState!=this.transport.readyState)
return;if(callback.status!=false&&callback.status!=this.status)
return;callback.fn(this.response.text,this.response.xml);},this);}});
