/*
 * Copyright 2006-2008 Savarese Software Research Corporation.
 * All rights reserved.
 */
var Wispers={Config:{setMSIEVersion:function(version){Wispers.Config.MSIE={Version:version};}},Array:{appendTo:function(to,from){for(var i=0;i<from.length;++i)
to.push(from[i]);},find:function(list,value){for(var i=0;i<list.length;++i){if(list[i]==value)
return i;}
return-1;}},mixin:function(src,dest){for(var key in src){dest[key]=src[key];}},subclass:function(child,parent,definition){child.prototype=new parent();for(var key in definition){child.prototype[key]=definition[key];}
child.prototype.superClass=parent.prototype;},svc_url:function(url){return"/svc"+url;},EmptyFunction:function(){},EmptyHTML:function(){return"<html></html>";},SIDPattern:new RegExp("\\bWSPRSID=([^;]*)"),getSID:function(doc){if(doc==null)
doc=document;var match=Wispers.SIDPattern.exec(doc.cookie);if(match){return match[1];}
return null;},newHTTPRequest:function(){var obj=false;try{obj=new XMLHttpRequest();}catch(e){}
if(!obj){var sigs=["MSXML2.XMLHTTP.8.0","MSXML2.XMLHTTP.7.0","MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(var i=0;i<sigs.length;++i){try{obj=new ActiveXObject(sigs[i]);break;}catch(e){}}}
return obj;},Caller:function(url){this.sender=Wispers.newHTTPRequest();this.fetcher=Wispers.newHTTPRequest();this.protocol_handler={};this.event_url=url;this.channel=0;this.stop_fetch=false;var thisObj=this;this.process_events=function(data){thisObj.processEvents(data);}
this.process_fetch=function(data){thisObj.processFetch(data);}},getCaller:function(){if(!Wispers._caller){Wispers._caller=new Wispers.Caller("/svc/event");Wispers.registerProtocol("wspr.Event",Wispers._caller);}
return Wispers._caller;},registerProtocol:function(protocol,handler){Wispers.getCaller().addProtocol(protocol,handler);},unregisterProtocol:function(protocol,handler){Wispers.getCaller().removeProtocol(protocol,handler);},Application:function(protocol,url){this.url=url;this.svc_url=Wispers.svc_url(url);this.caller=Wispers.getCaller();this.protocol=protocol;Wispers.registerProtocol(protocol,this);},onUnload:function(){if(Wispers._caller){Wispers._caller.stopFetching();Wispers._caller.sender=Wispers.EmptyFunction;Wispers._caller.fetcher=Wispers.EmptyFunction;}}}
Wispers.Caller.encodeArgs=function(args){var result="";for(var key in args){if(args[key].push&&args[key].shift){var values=args[key];for(var i=0;i<values.length;++i){if(result.length>0)
result+="&";result+=(encodeURIComponent(key)+"="+encodeURIComponent(values[i]));}}else{if(result.length>0)
result+="&";result+=(encodeURIComponent(key)+"="+encodeURIComponent(args[key]));}}
return result;}
Wispers.Caller.prototype={POST:function(request,url,args,handler,synchronous,login_xsrf){var asynchronous=true;if(request.readyState>0&&request.readyState<4){if(request.readyState!=1||navigator.userAgent.indexOf("Safari")==-1){return;}}
if(synchronous)
asynchronous=false;var encodedArgs=Wispers.Caller.encodeArgs(args);request.open("POST",url,asynchronous);var token=Wispers.getSID();var stateChangeHandler;if(handler){var thisObj=this;stateChangeHandler=function(doOver){try{if(!thisObj.stop_fetch&&(synchronous||request.readyState==4)){var enter_switch=1;try{if(request.status){enter_switch=1;}}catch(e){enter_switch=0;}
if(doOver!=true&&asynchronous){request.onreadystatechange=Wispers.EmptyFunction;}
if(enter_switch){switch(request.status){case 200:handler(request.responseText);break;case 400:case 401:case 403:if(request.getResponseHeader("Wispers-Event")=="ClearSession"){Wispers._caller.processEvents('/*([{protocol:"wspr.Event",call:"ClearSession" ,channel:"'+Wispers._caller.channel+'",_no_confirmation:"1" }])*/');}
break;case 12029:case 12030:case 12031:case 12032:case 12152:case 12159:if(doOver!=true){request.open("POST",url,asynchronous);if(asynchronous){request.onreadystatechange=function(){stateChangeHandler(true);}}
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");if(token){request.setRequestHeader("Wispers-Verify-Session",token);}
if(login_xsrf){request.setRequestHeader("Wispers-XSRF-Login","1");}
request.send(encodedArgs);doOver=true;if(synchronous){stateChangeHandler(true);}}
break;};}}}catch(e){request.onreadystatechange=Wispers.EmptyFunction;}}
if(asynchronous){request.onreadystatechange=stateChangeHandler;}}else if(asynchronous){request.onreadystatechange=Wispers.EmptyFunction;}
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");if(token){request.setRequestHeader("Wispers-Verify-Session",token);}
if(login_xsrf){request.setRequestHeader("Wispers-XSRF-Login","1");}
request.send(encodedArgs);if(synchronous&&handler){stateChangeHandler(false);}},processEvents:function(data){var events=eval(data.substring(2,data.length-2));var protocol;var proto_h;var proto_a;var event_h;var no_confirmation=0;var event;for(var i=0;i<events.length;++i){event=events[i];if(event._no_confirmation)
++no_confirmation;protocol=event.protocol;if(protocol){proto_a=this.protocol_handler[protocol];if(proto_a){for(var j=0;j<proto_a.length;++j){proto_h=proto_a[j];if(proto_h){event_h=proto_h[event.call]
if(event_h){try{event_h.call(proto_h,event);}catch(e){}}}}}else{}}}
return(events.length-no_confirmation);},processFetch:function(data){var confirmation=this.processEvents(data);if(this.stop_fetch){if(this.stopped_on_channel_change)
alert("More than one Web browser window is using the application.  Accessing the application via multiple Web browser windows is not supported at this time.  Only the most recently opened window will continue to function properly.");}else{if(confirmation>0)
this.fetchEvents({confirm:confirmation});else
this.fetchEvents();}},ChangeChannel:function(event){if(this.channel!=0){this.stop_fetch=true;this.stopped_on_channel_change=event.stopped_on_channel_change=true;}else
this.channel=event.channel;},ExpireSession:function(event){document.cookie="WSPRSID=; path=/; expires=Thursday, 01-Jan-1970 00:00:00 GMT";this.stopFetching();},addProtocol:function(protocol,handler){var proto_a=this.protocol_handler[protocol];if(proto_a)
proto_a.push(handler);else
this.protocol_handler[protocol]=[handler];},removeProtocol:function(protocol,handler){var proto_a=this.protocol_handler[protocol];if(proto_a){for(var i=0;i<proto_a.length;++i){if(proto_a[i]==handler){proto_a.splice(i,1);if(proto_a.length==0)
this.protocol_handler[protocol]=null;break;}}}},post:function(url,args,handler,synchronous,login_xsrf){try{this.POST(this.sender,url,args,handler,synchronous,login_xsrf);}catch(e){this.sender=Wispers.newHTTPRequest();this.POST(this.sender,url,args,handler,synchronous,login_xsrf);}},fetchEvents:function(args){this.stop_fetch=false;if(!args)
args={action:"fetch_events"};else
args.action="fetch_events"
args.channel=this.channel;this.POST(this.fetcher,this.event_url,args,this.process_fetch);},send:function(url,args,synchronous){this.post(url,args,null,synchronous);},invoke:function(url,args,handler,synchronous,login_xsrf){if(handler){this.post(url,args,function(data){var events=eval(data.substring(2,data.length-2));handler(events[0]);},synchronous,login_xsrf);}else{this.post(url,args,this.process_events,synchronous,login_xsrf);}},stopFetching:function(){if(!this.stop_fetch){this.stop_fetch=true;this.sender.abort();this.fetcher.abort();}},isStopped:function(){return this.stop_fetch;}}
Wispers.Application.prototype={post:function(args,handler){this.caller.post(this.svc_url,args,handler);},fetchEvents:function(){this.caller.fetchEvents();},stopFetching:function(){this.caller.stopFetching();},send:function(args){switch(arguments.length){case 1:this.caller.send(this.svc_url,arguments[0]);break;case 2:this.caller.send(arguments[0],arguments[1]);break;case 3:this.caller.send(arguments[0],arguments[1],arguments[2]);break;default:return;}},invoke:function(args,handler,synchronous,login_xsrf){this.caller.invoke(this.svc_url,args,handler,synchronous,login_xsrf);},isStopped:function(){return this.caller.isStopped();},unregisterProtocol:function(){if(this.protocol)
Wispers.unregisterProtocol(this.protocol,this);},unload:function(){this.unregisterProtocol();}}