(function(e){var b;e.jqplot=function(r,o,l){var n,k;if(o==null){throw"No data specified"}if(o.constructor==Array&&o.length==0||o[0].constructor!=Array){throw"Improper Data Array"}if(l==null){if(o instanceof Array){n=o;k=null}else{if(o.constructor==Object){n=null;k=o}}}else{n=o;k=l}var q=new h();q.init(r,n,k);q.draw();return q};e.jqplot.debug=1;e.jqplot.preInitHooks=[];e.jqplot.postInitHooks=[];e.jqplot.preParseOptionsHooks=[];e.jqplot.postParseOptionsHooks=[];e.jqplot.preDrawHooks=[];e.jqplot.postDrawHooks=[];e.jqplot.preDrawSeriesHooks=[];e.jqplot.postDrawSeriesHooks=[];e.jqplot.preDrawLegendHooks=[];e.jqplot.addLegendRowHooks=[];e.jqplot.preSeriesInitHooks=[];e.jqplot.postSeriesInitHooks=[];e.jqplot.preParseSeriesOptionsHooks=[];e.jqplot.postParseSeriesOptionsHooks=[];e.jqplot.eventListenerHooks=[];e.jqplot.preDrawSeriesShadowHooks=[];e.jqplot.postDrawSeriesShadowHooks=[];e.jqplot.ElemContainer=function(){this._elem;this._plotWidth;this._plotHeight;this._plotDimensions={height:null,width:null}};e.jqplot.ElemContainer.prototype.getWidth=function(){return this._elem.outerWidth(true)};e.jqplot.ElemContainer.prototype.getHeight=function(){return this._elem.outerHeight(true)};e.jqplot.ElemContainer.prototype.getPosition=function(){return this._elem.position()};e.jqplot.ElemContainer.prototype.getTop=function(){return this.getPosition().top};e.jqplot.ElemContainer.prototype.getLeft=function(){return this.getPosition().left};e.jqplot.ElemContainer.prototype.getBottom=function(){return this._elem.css("bottom")};e.jqplot.ElemContainer.prototype.getRight=function(){return this._elem.css("right")};function g(k){e.jqplot.ElemContainer.call(this);this.name=k;this._series=[];this.show=false;this.tickRenderer=e.jqplot.AxisTickRenderer;this.tickOptions={};this.labelRenderer=e.jqplot.AxisLabelRenderer;this.labelOptions={};this.label=null;this.showLabel=true;this.min=null;this.max=null;this.autoscale=false;this.pad=1.2;this.padMax=null;this.padMin=null;this.ticks=[];this.numberTicks;this.tickInterval;this.renderer=e.jqplot.LinearAxisRenderer;this.rendererOptions={};this.showTicks=true;this.showTickMarks=true;this.showMinorTicks=true;this.useSeriesColor=false;this.borderWidth=null;this.borderColor=null;this._dataBounds={min:null,max:null};this._offsets={min:null,max:null};this._ticks=[];this._label=null}g.prototype=new e.jqplot.ElemContainer();g.prototype.constructor=g;g.prototype.init=function(){this.renderer=new this.renderer();this.tickOptions.axis=this.name;if(this.label==null||this.label==""){this.showLabel=false}else{this.labelOptions.label=this.label}if(this.showLabel==false){this.labelOptions.show=false}if(this.pad==0){this.pad=1}if(this.padMax==0){this.padMax=1}if(this.padMin==0){this.padMin=1}if(this.padMax==null){this.padMax=(this.pad-1)/2+1}if(this.padMin==null){this.padMin=(this.pad-1)/2+1}if(this.min!=null||this.max!=null){this.autoscale=false}this.renderer.init.call(this,this.rendererOptions)};g.prototype.draw=function(k){return this.renderer.draw.call(this,k)};g.prototype.set=function(){this.renderer.set.call(this)};g.prototype.pack=function(l,k){if(this.show){this.renderer.pack.call(this,l,k)}};function a(){e.jqplot.ElemContainer.call(this);this.show=false;this.location="ne";this.xoffset=12;this.yoffset=12;this.border;this.background;this.textColor;this.fontFamily;this.fontSize;this.rowSpacing="0.5em";this.renderer=e.jqplot.TableLegendRenderer;this.rendererOptions={};this.preDraw=false;this.escapeHtml=false;this._series=[]}a.prototype=new e.jqplot.ElemContainer();a.prototype.constructor=a;a.prototype.init=function(){this.renderer=new this.renderer();this.renderer.init.call(this,this.rendererOptions)};a.prototype.draw=function(l){for(var k=0;k<e.jqplot.preDrawLegendHooks.length;k++){e.jqplot.preDrawLegendHooks[k].call(this,l)}return this.renderer.draw.call(this,l)};a.prototype.pack=function(k){this.renderer.pack.call(this,k)};e.jqplot.TableLegendRenderer=function(){};function f(k){e.jqplot.ElemContainer.call(this);this.text=k;this.show=true;this.fontFamily;this.fontSize;this.textAlign;this.textColor;this.renderer=e.jqplot.DivTitleRenderer;this.rendererOptions={}}f.prototype=new e.jqplot.ElemContainer();f.prototype.constructor=f;f.prototype.init=function(){this.renderer=new this.renderer();this.renderer.init.call(this,this.rendererOptions)};f.prototype.draw=function(k){return this.renderer.draw.call(this,k)};f.prototype.pack=function(){this.renderer.pack.call(this)};function j(){e.jqplot.ElemContainer.call(this);this.show=true;this.xaxis="xaxis";this._xaxis;this.yaxis="yaxis";this._yaxis;this.gridBorderWidth=2;this.renderer=e.jqplot.LineRenderer;this.rendererOptions={};this.data=[];this.gridData=[];this.label="";this.color;this.lineWidth=2.5;this.shadow=true;this.shadowAngle=45;this.shadowOffset=1.25;this.shadowDepth=3;this.shadowAlpha="0.1";this.breakOnNull=false;this.markerRenderer=e.jqplot.MarkerRenderer;this.markerOptions={};this.showLine=true;this.showMarker=true;this.index;this.fill=false;this.fillColor;this.fillAlpha;this.fillAndStroke=false;this._stack=false;this.neighborThreshold=4;this._stackData=[];this._plotData=[];this._prevPlotData=[];this._prevGridData=[];this._stackAxis="y";this._primaryAxis="_xaxis";this.plugins={}}j.prototype=new e.jqplot.ElemContainer();j.prototype.constructor=j;j.prototype.init=function(l,r){this.index=l;this.gridBorderWidth=r;var q=this.data;for(var n=0;n<q.length;n++){if(!this.breakOnNull){if(q[n]==null||q[n][0]==null||q[n][1]==null){q.splice(n,1);continue}}else{if(q[n]==null||q[n][0]==null||q[n][1]==null){var o}}}if(!this.fillColor){this.fillColor=this.color}if(this.fillAlpha){var k=e.jqplot.normalize2rgb(this.fillColor);var k=e.jqplot.getColorComponents(k);this.fillColor="rgba("+k[0]+","+k[1]+","+k[2]+","+this.fillAlpha+")"}this.renderer=new this.renderer();this.renderer.init.call(this,this.rendererOptions);this.markerRenderer=new this.markerRenderer();if(!this.markerOptions.color){this.markerOptions.color=this.color}if(this.markerOptions.show==null){this.markerOptions.show=this.showMarker}this.markerRenderer.init(this.markerOptions)};j.prototype.draw=function(r,o){var l=(o==b)?{}:o;for(var k=0;k<e.jqplot.preDrawSeriesHooks.length;k++){e.jqplot.preDrawSeriesHooks[k].call(this,r,l)}if(this.show){this.renderer.setGridData.call(this);if(!l.preventJqPlotSeriesDrawTrigger){e(r.canvas).trigger("jqplotSeriesDraw",[this.data,this.gridData])}var q=[];if(l.data){q=l.data}else{if(!this._stack){q=this.data}else{q=this._plotData}}var n=l.gridData||this.renderer.makeGridData.call(this,q);this.renderer.draw.call(this,r,n,l)}for(var k=0;k<e.jqplot.postDrawSeriesHooks.length;k++){e.jqplot.postDrawSeriesHooks[k].call(this,r,l)}};j.prototype.drawShadow=function(r,o){var l=(o==b)?{}:o;for(var k=0;k<e.jqplot.preDrawSeriesShadowHooks.length;k++){e.jqplot.preDrawSeriesShadowHooks[k].call(this,r,l)}if(this.shadow){this.renderer.setGridData.call(this);var q=[];if(l.data){q=l.data}else{if(!this._stack){q=this.data}else{q=this._plotData}}var n=l.gridData||this.renderer.makeGridData.call(this,q);this.renderer.drawShadow.call(this,r,n,l)}for(var k=0;k<e.jqplot.postDrawSeriesShadowHooks.length;k++){e.jqplot.postDrawSeriesShadowHooks[k].call(this,r,l)}};function d(){e.jqplot.ElemContainer.call(this);this.drawGridlines=true;this.gridLineColor="#cccccc";this.gridLineWidth=1;this.background="#fffdf6";this.borderColor="#999999";this.borderWidth=2;this.shadow=true;this.shadowAngle=45;this.shadowOffset=1.5;this.shadowWidth=3;this.shadowDepth=3;this.shadowAlpha="0.07";this._left;this._top;this._right;this._bottom;this._width;this._height;this._axes=[];this.renderer=e.jqplot.CanvasGridRenderer;this.rendererOptions={};this._offsets={top:null,bottom:null,left:null,right:null}}d.prototype=new e.jqplot.ElemContainer();d.prototype.constructor=d;d.prototype.init=function(){this.renderer=new this.renderer();this.renderer.init.call(this,this.rendererOptions)};d.prototype.createElement=function(k){this._offsets=k;return this.renderer.createElement.call(this)};d.prototype.draw=function(){this.renderer.draw.call(this)};e.jqplot.GenericCanvas=function(){e.jqplot.ElemContainer.call(this);this._ctx};e.jqplot.GenericCanvas.prototype=new e.jqplot.ElemContainer();e.jqplot.GenericCanvas.prototype.constructor=e.jqplot.GenericCanvas;e.jqplot.GenericCanvas.prototype.createElement=function(q,n,l){this._offsets=q;var k="jqplot";if(n!=b){k=n}var o=document.createElement("canvas");if(l!=b){this._plotDimensions=l}o.width=this._plotDimensions.width-this._offsets.left-this._offsets.right;o.height=this._plotDimensions.height-this._offsets.top-this._offsets.bottom;this._elem=e(o);this._elem.addClass(k);this._elem.css({position:"absolute",left:this._offsets.left,top:this._offsets.top});if(e.browser.msie){window.G_vmlCanvasManager.init_(document)}if(e.browser.msie){o=window.G_vmlCanvasManager.initElement(o)}return this._elem};e.jqplot.GenericCanvas.prototype.setContext=function(){this._ctx=this._elem.get(0).getContext("2d");return this._ctx};function h(){this.data=[];this.targetId=null;this.target=null;this.defaults={axesDefaults:{},axes:{xaxis:{},yaxis:{},x2axis:{},y2axis:{},y3axis:{},y4axis:{},y5axis:{},y6axis:{},y7axis:{},y8axis:{},y9axis:{}},seriesDefaults:{},gridPadding:{top:10,right:10,bottom:10,left:10},series:[]};this.series=[];this.axes={xaxis:new g("xaxis"),yaxis:new g("yaxis"),x2axis:new g("x2axis"),y2axis:new g("y2axis"),y3axis:new g("y3axis"),y4axis:new g("y4axis"),y5axis:new g("y5axis"),y6axis:new g("y6axis"),y7axis:new g("y7axis"),y8axis:new g("y8axis"),y9axis:new g("y9axis")};this.grid=new d();this.legend=new a();this.baseCanvas=new e.jqplot.GenericCanvas();this.seriesCanvas=new e.jqplot.GenericCanvas();this.eventCanvas=new e.jqplot.GenericCanvas();this._width=null;this._height=null;this._plotDimensions={height:null,width:null};this._gridPadding={top:10,right:10,bottom:10,left:10};this.equalXTicks=true;this.equalYTicks=true;this.seriesColors=["#4bb2c5","#EAA228","#c5b47f","#579575","#839557","#958c12","#953579","#4b5de4","#d8b83f","#ff5800","#0085cc"];this.sortData=true;var o=0;this.textColor;this.fontFamily;this.fontSize;this.title=new f();this.options={};this.stackSeries=false;this._stackData=[];this._plotData=[];this.plugins={};this.colorGenerator=ColorGenerator;this.init=function(w,v,t){for(var u=0;u<e.jqplot.preInitHooks.length;u++){e.jqplot.preInitHooks[u].call(this,w,v,t)}this.targetId=w;this.target=e("#"+w);if(!this.target.get(0)){throw"No plot target specified"}if(this.target.css("position")=="static"){this.target.css("position","relative")}if(!this.target.hasClass("jqplot-target")){this.target.addClass("jqplot-target")}if(!this.target.height()){this._height=300;this.target.css("height","300px")}else{this._height=this.target.height()}if(!this.target.width()){this._width=400;this.target.css("width","400px")}else{this._width=this.target.width()}this._plotDimensions.height=this._height;this._plotDimensions.width=this._width;this.grid._plotDimensions=this._plotDimensions;this.title._plotDimensions=this._plotDimensions;this.baseCanvas._plotDimensions=this._plotDimensions;this.seriesCanvas._plotDimensions=this._plotDimensions;this.eventCanvas._plotDimensions=this._plotDimensions;this.legend._plotDimensions=this._plotDimensions;if(this._height<=0||this._width<=0||!this._height||!this._width){throw"Canvas dimensions <=0"}this.data=v;this.parseOptions(t);if(this.textColor){this.target.css("color",this.textColor)}if(this.fontFamily){this.target.css("font-family",this.fontFamily)}if(this.fontSize){this.target.css("font-size",this.fontSize)}this.title.init();this.legend.init();for(var u=0;u<this.series.length;u++){for(var r=0;r<e.jqplot.preSeriesInitHooks.length;r++){e.jqplot.preSeriesInitHooks[r].call(this.series[u],w,v,this.options.seriesDefaults,this.options.series[u])}this.populatePlotData(this.series[u],u);this.series[u]._plotDimensions=this._plotDimensions;this.series[u].init(u,this.grid.borderWidth);for(var r=0;r<e.jqplot.postSeriesInitHooks.length;r++){e.jqplot.postSeriesInitHooks[r].call(this.series[u],w,v,this.options.seriesDefaults,this.options.series[u])}}for(var q in this.axes){this.axes[q]._plotDimensions=this._plotDimensions;this.axes[q].init()}if(this.sortData){k(this.series)}this.grid.init();this.grid._axes=this.axes;this.legend._series=this.series;for(var u=0;u<e.jqplot.postInitHooks.length;u++){e.jqplot.postInitHooks[u].call(this,w,v,t)}};function k(r){var t;for(var q=0;q<r.length;q++){t=r[q].data;if(r[q]._stackAxis=="x"){t.sort(function(v,u){var w=v[1]-u[1];if(w){return w}return 0})}else{t.sort(function(v,u){var w=v[0]-u[0];if(w){return w}return 0})}}}this.populatePlotData=function(u,v){this._plotData=[];this._stackData=[];u._stackData=[];u._plotData=[];if(this.stackSeries){u._stack=true;var w=u._stackAxis=="x"?0:1;var x=w?0:1;var y=e.extend(true,[],u.data);var z=e.extend(true,[],u.data);for(var t=0;t<v;t++){var q=this.series[t].data;for(var r=0;r<q.length;r++){y[r][0]+=q[r][0];y[r][1]+=q[r][1];z[r][w]+=q[r][w]}}this._plotData.push(z);this._stackData.push(y);u._stackData=y;u._plotData=z}else{this._stackData.push(u.data);this.series[v]._stackData=u.data;this._plotData.push(u.data);u._plotData=u.data}if(v>0){u._prevPlotData=this.series[v-1]._plotData}};this.getNextSeriesColor=(function(r){var q=0;var u=r.seriesColors;return function(){if(q<u.length){return u[q++]}else{q=0;return u[q++]}}})(this);this.parseOptions=function(u){for(var v=0;v<e.jqplot.preParseOptionsHooks.length;v++){e.jqplot.preParseOptionsHooks[v].call(this,u)}this.options=e.extend(true,{},this.defaults,u);this.stackSeries=this.options.stackSeries;if(this.options.seriesColors){this.seriesColors=this.options.seriesColors}var x=new this.colorGenerator(this.seriesColors);this._gridPadding=this.options.gridPadding;this.sortData=(this.options.sortData!=null)?this.options.sortData:this.sortData;for(var y in this.axes){var w=this.axes[y];e.extend(true,w,this.options.axesDefaults,this.options.axes[y]);w._plotWidth=this._width;w._plotHeight=this._height}if(this.data.length==0){this.data=[];for(var v=0;v<this.options.series.length;v++){this.data.push(this.options.series.data)}}var t=function(B){var z=[];var A;if(!(B[0] instanceof Array)){for(var A=0;A<B.length;A++){z.push([A+1,B[A]])}}else{e.extend(true,z,B)}return z};for(var v=0;v<this.data.length;v++){var q=new j();for(var r=0;r<e.jqplot.preParseSeriesOptionsHooks.length;r++){e.jqplot.preParseSeriesOptionsHooks[r].call(q,this.options.seriesDefaults,this.options.series[v])}e.extend(true,q,this.options.seriesDefaults,this.options.series[v]);q.data=t(this.data[v]);switch(q.xaxis){case"xaxis":q._xaxis=this.axes.xaxis;break;case"x2axis":q._xaxis=this.axes.x2axis;break;default:break}q._yaxis=this.axes[q.yaxis];q._xaxis._series.push(q);q._yaxis._series.push(q);if(q.show){q._xaxis.show=true;q._yaxis.show=true}if(!q.color&&q.show!=false){q.color=x.next()}if(!q.label){q.label="Series "+(v+1).toString()}this.series.push(q);for(var r=0;r<e.jqplot.postParseSeriesOptionsHooks.length;r++){e.jqplot.postParseSeriesOptionsHooks[r].call(this.series[v],this.options.seriesDefaults,this.options.series[v])}}e.extend(true,this.grid,this.options.grid);for(var y in this.axes){var w=this.axes[y];if(w.borderWidth==null){w.borderWidth=this.grid.borderWidth}if(w.borderColor==null){if(y!="xaxis"&&y!="x2axis"&&w.useSeriesColor===true&&w.show){w.borderColor=w._series[0].color}else{w.borderColor=this.grid.borderColor}}}if(typeof this.options.title=="string"){this.title.text=this.options.title}else{if(typeof this.options.title=="object"){e.extend(true,this.title,this.options.title)}}this.title._plotWidth=this._width;e.extend(true,this.legend,this.options.legend);for(var v=0;v<e.jqplot.postParseOptionsHooks.length;v++){e.jqplot.postParseOptionsHooks[v].call(this,u)}};this.redraw=function(){this.target.trigger("jqplotPreRedraw");this.target.empty();for(var r in this.axes){this.axes[r]._ticks=[]}for(var q=0;q<this.series.length;q++){this.populatePlotData(this.series[q],q)}this.draw();this.target.trigger("jqplotPostRedraw")};this.draw=function(){this.target.trigger("jqplotPreDraw");for(var x=0;x<e.jqplot.preDrawHooks.length;x++){e.jqplot.preDrawHooks[x].call(this)}this.target.append(this.baseCanvas.createElement({left:0,right:0,top:0,bottom:0},"jqplot-base-canvas"));var w=this.baseCanvas.setContext();this.target.append(this.title.draw());this.title.pack({top:0,left:0});for(var t in this.axes){this.target.append(this.axes[t].draw(w));this.axes[t].set()}if(this.axes.yaxis.show){this._gridPadding.left=this.axes.yaxis.getWidth()}var u=["y2axis","y3axis","y4axis","y5axis","y6axis","y7axis","y8axis","y9axis"];var r=[0,0,0,0];var z=0;for(var v=8;v>0;v--){var q=this.axes[u[v-1]];if(q.show){r[v-1]=z;z+=q.getWidth()}}if(z>this._gridPadding.right){this._gridPadding.right=z}if(this.title.show&&this.axes.x2axis.show){this._gridPadding.top=this.title.getHeight()+this.axes.x2axis.getHeight()}else{if(this.title.show){this._gridPadding.top=this.title.getHeight()}else{if(this.axes.x2axis.show){this._gridPadding.top=this.axes.x2axis.getHeight()}}}if(this.axes.xaxis.show){this._gridPadding.bottom=this.axes.xaxis.getHeight()}this.axes.xaxis.pack({position:"absolute",bottom:0,left:0,width:this._width},{min:this._gridPadding.left,max:this._width-this._gridPadding.right});this.axes.yaxis.pack({position:"absolute",top:0,left:0,height:this._height},{min:this._height-this._gridPadding.bottom,max:this._gridPadding.top});this.axes.x2axis.pack({position:"absolute",top:this.title.getHeight(),left:0,width:this._width},{min:this._gridPadding.left,max:this._width-this._gridPadding.right});for(var x=8;x>0;x--){this.axes[u[x-1]].pack({position:"absolute",top:0,right:r[x-1]},{min:this._height-this._gridPadding.bottom,max:this._gridPadding.top})}this.target.append(this.grid.createElement(this._gridPadding));this.grid.draw();this.target.append(this.seriesCanvas.createElement(this._gridPadding,"jqplot-series-canvas"));var B=this.seriesCanvas.setContext();this.target.append(this.eventCanvas.createElement(this._gridPadding,"jqplot-event-canvas"));var A=this.eventCanvas.setContext();A.fillStyle="rgba(0,0,0,0)";A.fillRect(0,0,A.canvas.width,A.canvas.height);this.bindCustomEvents();if(this.legend.preDraw){this.target.append(this.legend.draw());this.legend.pack(this._gridPadding);if(this.legend._elem){this.drawSeries(B,{legendInfo:{location:this.legend.location,width:this.legend.getWidth(),height:this.legend.getHeight(),xoffset:this.legend.xoffset,yoffset:this.legend.yoffset}})}else{this.drawSeries(B)}}else{this.drawSeries(B);this.target.append(this.legend.draw());this.legend.pack(this._gridPadding)}for(var x=0;x<e.jqplot.eventListenerHooks.length;x++){var y=e.jqplot.eventListenerHooks[x];this.eventCanvas._elem.bind(y[0],{plot:this},y[1])}for(var x=0;x<e.jqplot.postDrawHooks.length;x++){e.jqplot.postDrawHooks[x].call(this)}this.target.trigger("jqplotPostDraw",[this])};this.bindCustomEvents=function(){this.eventCanvas._elem.bind("click",{plot:this},this.onClick);this.eventCanvas._elem.bind("dblclick",{plot:this},this.onDblClick);this.eventCanvas._elem.bind("mousedown",{plot:this},this.onMouseDown);this.eventCanvas._elem.bind("mouseup",{plot:this},this.onMouseUp);this.eventCanvas._elem.bind("mousemove",{plot:this},this.onMouseMove);this.eventCanvas._elem.bind("mouseenter",{plot:this},this.onMouseEnter);this.eventCanvas._elem.bind("mouseleave",{plot:this},this.onMouseLeave)};function l(z){var y=z.data.plot;var u=y.eventCanvas._elem.offset();var x={x:z.pageX-u.left,y:z.pageY-u.top};var v={xaxis:null,yaxis:null,x2axis:null,y2axis:null,y3axis:null,y4axis:null,y5axis:null,y6axis:null,y7axis:null,y8axis:null,y9axis:null};var w=["xaxis","yaxis","x2axis","y2axis","y3axis","y4axis","y5axis","y6axis","y7axis","y8axis","y9axis"];var q=y.axes;for(var r=11;r>0;r--){var t=w[r-1];if(q[t].show){v[t]=q[t].series_p2u(x[t.charAt(0)])}}return({offsets:u,gridPos:x,dataPos:v})}function n(A,E,D){var B=null;var F,w,u,C,v,t;var z;for(var w=0;w<A.series.length;w++){F=A.series[w];t=F.renderer;if(F.show){z=Math.abs(F.markerRenderer.size/2+F.neighborThreshold);for(var v=0;v<F.gridData.length;v++){p=F.gridData[v];if(t.constructor==e.jqplot.OHLCRenderer){if(t.candleStick){var q=F._yaxis.series_u2p;if(E>=p[0]-t.bodyWidth/2&&E<=p[0]+t.bodyWidth/2&&D>=q(F.data[v][2])&&D<=q(F.data[v][3])){B={seriesIndex:w,pointIndex:v,gridData:p,data:F.data[v]}}}else{if(!t.hlc){var q=F._yaxis.series_u2p;if(E>=p[0]-t.tickLength&&E<=p[0]+t.tickLength&&D>=q(F.data[v][2])&&D<=q(F.data[v][3])){B={seriesIndex:w,pointIndex:v,gridData:p,data:F.data[v]}}}else{var q=F._yaxis.series_u2p;if(E>=p[0]-t.tickLength&&E<=p[0]+t.tickLength&&D>=q(F.data[v][1])&&D<=q(F.data[v][2])){B={seriesIndex:w,pointIndex:v,gridData:p,data:F.data[v]}}}}}else{C=Math.sqrt((E-p[0])*(E-p[0])+(D-p[1])*(D-p[1]));if(C<=z&&(C<=u||u==null)){u=C;B={seriesIndex:w,pointIndex:v,gridData:p,data:F.data[v]}}}}}}return B}this.onClick=function(r){var q=l(r);var u=r.data.plot;var t=n(u,q.gridPos.x,q.gridPos.y);r.data.plot.eventCanvas._elem.trigger("jqplotClick",[q.gridPos,q.dataPos,t,u])};this.onDblClick=function(r){var q=l(r);var u=r.data.plot;var t=n(u,q.gridPos.x,q.gridPos.y);r.data.plot.eventCanvas._elem.trigger("jqplotDblClick",[q.gridPos,q.dataPos,t,u])};this.onMouseDown=function(r){var q=l(r);var u=r.data.plot;var t=n(u,q.gridPos.x,q.gridPos.y);r.data.plot.eventCanvas._elem.trigger("jqplotMouseDown",[q.gridPos,q.dataPos,t,u])};this.onMouseUp=function(r){var q=l(r);r.data.plot.eventCanvas._elem.trigger("jqplotMouseUp",[q.gridPos,q.dataPos,null,r.data.plot])};this.onMouseMove=function(r){var q=l(r);var u=r.data.plot;var t=n(u,q.gridPos.x,q.gridPos.y);r.data.plot.eventCanvas._elem.trigger("jqplotMouseMove",[q.gridPos,q.dataPos,t,u])};this.onMouseEnter=function(r){var q=l(r);var t=r.data.plot;r.data.plot.eventCanvas._elem.trigger("jqplotMouseEnter",[q.gridPos,q.dataPos,null,t])};this.onMouseLeave=function(r){var q=l(r);var t=r.data.plot;r.data.plot.eventCanvas._elem.trigger("jqplotMouseLeave",[q.gridPos,q.dataPos,null,t])};this.drawSeries=function(t,q){t.clearRect(0,0,t.canvas.width,t.canvas.height);for(var r=0;r<this.series.length;r++){this.series[r].drawShadow(t,q)}for(var r=0;r<this.series.length;r++){this.series[r].draw(t,q)}}}ColorGenerator=function(l){var k=0;this.next=function(){if(k<l.length){return l[k++]}else{k=0;return l[k++]}};this.previous=function(){if(k>0){return l[k--]}else{k=l.length-1;return l[k]}};this.setColors=function(n){l=n};this.reset=function(){k=0}};e.jqplot.hex2rgb=function(n,k){n=n.replace("#","");if(n.length==3){n=n[0]+n[0]+n[1]+n[1]+n[2]+n[2]}var l;l="rgba("+parseInt(n.slice(0,2),16)+", "+parseInt(n.slice(2,4),16)+", "+parseInt(n.slice(4,6),16);if(k){l+=", "+k}l+=")";return l};e.jqplot.rgb2hex=function(q){var n=/rgba?\( *([0-9]{1,3}\.?[0-9]*%?) *, *([0-9]{1,3}\.?[0-9]*%?) *, *([0-9]{1,3}\.?[0-9]*%?) *(?:, *[0-9.]*)?\)/;var k=q.match(n);var o="#";for(i=1;i<4;i++){var l;if(k[i].search(/%/)!=-1){l=parseInt(255*k[i]/100,10).toString(16);if(l.length==1){l="0"+l}}else{l=parseInt(k[i],10).toString(16);if(l.length==1){l="0"+l}}o+=l}return o};e.jqplot.normalize2rgb=function(l,k){if(l.search(/^ *rgba?\(/)!=-1){return l}else{if(l.search(/^ *#?[0-9a-fA-F]?[0-9a-fA-F]/)!=-1){return e.jqplot.hex2rgb(l,k)}else{throw"invalid color spec"}}};e.jqplot.getColorComponents=function(q){var o=e.jqplot.normalize2rgb(q);var n=/rgba?\( *([0-9]{1,3}\.?[0-9]*%?) *, *([0-9]{1,3}\.?[0-9]*%?) *, *([0-9]{1,3}\.?[0-9]*%?) *,? *([0-9.]* *)?\)/;var k=o.match(n);var l=[];for(i=1;i<4;i++){if(k[i].search(/%/)!=-1){l[i-1]=parseInt(255*k[i]/100,10)}else{l[i-1]=parseInt(k[i],10)}}l[3]=parseFloat(k[4])?parseFloat(k[4]):1;return l};e.jqplot.log=function(){if(window.console&&e.jqplot.debug){if(arguments.length==1){console.log(arguments[0])}else{console.log(arguments)}}};var c=e.jqplot.log;e.jqplot.AxisLabelRenderer=function(k){e.jqplot.ElemContainer.call(this);this.axis;this.show=true;this.label="";this._elem;this.escapeHTML=false;e.extend(true,this,k)};e.jqplot.AxisLabelRenderer.prototype=new e.jqplot.ElemContainer();e.jqplot.AxisLabelRenderer.prototype.constructor=e.jqplot.AxisLabelRenderer;e.jqplot.AxisLabelRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.AxisLabelRenderer.prototype.draw=function(){this._elem=e('<div style="position:absolute;" class="jqplot-'+this.axis+'-label"></div>');if(Number(this.label)){this._elem.css("white-space","nowrap")}if(!this.escapeHTML){this._elem.html(this.label)}else{this._elem.text(this.label)}return this._elem};e.jqplot.AxisLabelRenderer.prototype.pack=function(){};e.jqplot.AxisTickRenderer=function(k){e.jqplot.ElemContainer.call(this);this.mark="outside";this.axis;this.showMark=true;this.showGridline=true;this.isMinorTick=false;this.size=4;this.markSize=6;this.show=true;this.showLabel=true;this.label="";this.value=null;this._styles={};this.formatter=e.jqplot.DefaultTickFormatter;this.formatString="";this.fontFamily;this.fontSize;this.textColor;this._elem;e.extend(true,this,k)};e.jqplot.AxisTickRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.AxisTickRenderer.prototype=new e.jqplot.ElemContainer();e.jqplot.AxisTickRenderer.prototype.constructor=e.jqplot.AxisTickRenderer;e.jqplot.AxisTickRenderer.prototype.setTick=function(k,n,l){this.value=k;this.axis=n;if(l){this.isMinorTick=true}return this};e.jqplot.AxisTickRenderer.prototype.draw=function(){if(!this.label){this.label=this.formatter(this.formatString,this.value)}style='style="position:absolute;';if(Number(this.label)){style+="white-space:nowrap;"}style+='"';this._elem=e("<div "+style+' class="jqplot-'+this.axis+'-tick">'+this.label+"</div>");for(var k in this._styles){this._elem.css(k,this._styles[k])}if(this.fontFamily){this._elem.css("font-family",this.fontFamily)}if(this.fontSize){this._elem.css("font-size",this.fontSize)}if(this.textColor){this._elem.css("color",this.textColor)}return this._elem};e.jqplot.DefaultTickFormatter=function(k,l){if(typeof l=="number"){if(!k){k="%.1f"}return e.jqplot.sprintf(k,l)}else{return String(l)}};e.jqplot.AxisTickRenderer.prototype.pack=function(){};e.jqplot.CanvasGridRenderer=function(){this.shadowRenderer=new e.jqplot.ShadowRenderer()};e.jqplot.CanvasGridRenderer.prototype.init=function(l){this._ctx;e.extend(true,this,l);var k={lineJoin:"miter",lineCap:"round",fill:false,isarc:false,angle:this.shadowAngle,offset:this.shadowOffset,alpha:this.shadowAlpha,depth:this.shadowDepth,lineWidth:this.shadowWidth,closePath:false};this.renderer.shadowRenderer.init(k)};e.jqplot.CanvasGridRenderer.prototype.createElement=function(){var n=document.createElement("canvas");var k=this._plotDimensions.width;var l=this._plotDimensions.height;n.width=k;n.height=l;this._elem=e(n);this._elem.addClass("jqplot-grid-canvas");this._elem.css({position:"absolute",left:0,top:0});if(e.browser.msie){window.G_vmlCanvasManager.init_(document)}if(e.browser.msie){n=window.G_vmlCanvasManager.initElement(n)}this._top=this._offsets.top;this._bottom=l-this._offsets.bottom;this._left=this._offsets.left;this._right=k-this._offsets.right;this._width=this._right-this._left;this._height=this._bottom-this._top;return this._elem};e.jqplot.CanvasGridRenderer.prototype.draw=function(){this._ctx=this._elem.get(0).getContext("2d");var E=this._ctx;var x=this._axes;E.save();E.fillStyle=this.background;E.fillRect(this._left,this._top,this._width,this._height);if(this.drawGridlines){E.save();E.lineJoin="miter";E.lineCap="butt";E.lineWidth=this.gridLineWidth;E.strokeStyle=this.gridLineColor;var z,w;var k=["xaxis","yaxis","x2axis","y2axis"];for(var r=4;r>0;r--){var l=k[r-1];var o=x[l];var A=o._ticks;if(o.show){for(var q=A.length;q>0;q--){var D=A[q-1];if(D.show){var y=Math.round(o.u2p(D.value))+0.5;switch(l){case"xaxis":if(D.showGridline){v(y,this._top,y,this._bottom)}if(D.showMark&&D.mark){s=D.markSize;m=D.mark;var y=Math.round(o.u2p(D.value))+0.5;switch(m){case"outside":z=this._bottom;w=this._bottom+s;break;case"inside":z=this._bottom-s;w=this._bottom;break;case"cross":z=this._bottom-s;w=this._bottom+s;break;default:z=this._bottom;w=this._bottom+s;break}if(this.shadow){this.renderer.shadowRenderer.draw(E,[[y,z],[y,w]],{lineCap:"butt",lineWidth:this.gridLineWidth,offset:this.gridLineWidth*0.75,depth:2,fill:false,closePath:false})}v(y,z,y,w)}break;case"yaxis":if(D.showGridline){v(this._right,y,this._left,y)}if(D.showMark&&D.mark){s=D.markSize;m=D.mark;var y=Math.round(o.u2p(D.value))+0.5;switch(m){case"outside":z=this._left-s;w=this._left;break;case"inside":z=this._left;w=this._left+s;break;case"cross":z=this._left-s;w=this._left+s;break;default:z=this._left-s;w=this._left;break}if(this.shadow){this.renderer.shadowRenderer.draw(E,[[z,y],[w,y]],{lineCap:"butt",lineWidth:this.gridLineWidth*1.5,offset:this.gridLineWidth*0.75,fill:false,closePath:false})}v(z,y,w,y,{strokeStyle:o.borderColor})}break;case"x2axis":if(D.showGridline){v(y,this._bottom,y,this._top)}if(D.showMark&&D.mark){s=D.markSize;m=D.mark;var y=Math.round(o.u2p(D.value))+0.5;switch(m){case"outside":z=this._top-s;w=this._top;break;case"inside":z=this._top;w=this._top+s;break;case"cross":z=this._top-s;w=this._top+s;break;default:z=this._top-s;w=this._top;break}if(this.shadow){this.renderer.shadowRenderer.draw(E,[[y,z],[y,w]],{lineCap:"butt",lineWidth:this.gridLineWidth,offset:this.gridLineWidth*0.75,depth:2,fill:false,closePath:false})}v(y,z,y,w)}break;case"y2axis":if(D.showGridline){v(this._left,y,this._right,y)}if(D.showMark&&D.mark){s=D.markSize;m=D.mark;var y=Math.round(o.u2p(D.value))+0.5;switch(m){case"outside":z=this._right;w=this._right+s;break;case"inside":z=this._right-s;w=this._right;break;case"cross":z=this._right-s;w=this._right+s;break;default:z=this._right;w=this._right+s;break}if(this.shadow){this.renderer.shadowRenderer.draw(E,[[z,y],[w,y]],{lineCap:"butt",lineWidth:this.gridLineWidth*1.5,offset:this.gridLineWidth*0.75,fill:false,closePath:false})}v(z,y,w,y,{strokeStyle:o.borderColor})}break;default:break}}}}}k=["y3axis","y4axis","y5axis","y6axis","y7axis","y8axis","y9axis"];for(var r=7;r>0;r--){var o=x[k[r-1]];var A=o._ticks;if(o.show){var C=A[o.numberTicks-1];var u=A[0];var n=o.getLeft();var B=[[n,C.getTop()+C.getHeight()/2],[n,u.getTop()+u.getHeight()/2+1]];if(this.shadow){this.renderer.shadowRenderer.draw(E,B,{lineCap:"butt",fill:false,closePath:false})}v(B[0][0],B[0][1],B[1][0],B[1][1],{lineCap:"butt",strokeStyle:o.borderColor,lineWidth:o.borderWidth});for(var q=A.length;q>0;q--){var D=A[q-1];s=D.markSize;m=D.mark;var y=Math.round(o.u2p(D.value))+0.5;if(D.show&&D.showGridline){switch(m){case"outside":z=n;w=n+s;break;case"inside":z=n-s;w=n;break;case"cross":z=n-s;w=n+s;break;default:z=n;w=n+s;break}B=[[z,y],[w,y]];if(this.shadow){this.renderer.shadowRenderer.draw(E,B,{lineCap:"butt",lineWidth:this.gridLineWidth*1.5,offset:this.gridLineWidth*0.75,fill:false,closePath:false})}v(z,y,w,y,{strokeStyle:o.borderColor})}}}}E.restore()}function v(I,H,F,t,G){E.save();G=G||{};e.extend(true,E,G);E.beginPath();E.moveTo(I,H);E.lineTo(F,t);E.stroke();E.restore()}if(this.shadow){var B=[[this._left,this._bottom],[this._right,this._bottom],[this._right,this._top]];this.renderer.shadowRenderer.draw(E,B)}v(this._left,this._top,this._right,this._top,{lineCap:"round",strokeStyle:x.x2axis.borderColor,lineWidth:x.x2axis.borderWidth});v(this._right,this._top,this._right,this._bottom,{lineCap:"round",strokeStyle:x.y2axis.borderColor,lineWidth:x.y2axis.borderWidth});v(this._right,this._bottom,this._left,this._bottom,{lineCap:"round",strokeStyle:x.xaxis.borderColor,lineWidth:x.xaxis.borderWidth});v(this._left,this._bottom,this._left,this._top,{lineCap:"round",strokeStyle:x.yaxis.borderColor,lineWidth:x.yaxis.borderWidth});E.restore()};e.jqplot.DivTitleRenderer=function(){};e.jqplot.DivTitleRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.DivTitleRenderer.prototype.draw=function(){var l=this.renderer;if(!this.text){this.show=false;this._elem=e('<div style="height:0px;width:0px;"></div>')}else{if(this.text){var k="position:absolute;top:0px;left:0px;";k+=(this._plotWidth)?"width:"+this._plotWidth+"px;":"";k+=(this.fontFamily)?"font-family:"+this.fontFamily+";":"";k+=(this.fontSize)?"font-size:"+this.fontSize+";":"";k+=(this.textAlign)?"text-align:"+this.textAlign+";":"text-align:center;";k+=(this.textColor)?"color:"+this.textColor+";":"";this._elem=e('<div class="jqplot-title" style="'+k+'">'+this.text+"</div>")}}return this._elem};e.jqplot.DivTitleRenderer.prototype.pack=function(){};e.jqplot.LineRenderer=function(){this.shapeRenderer=new e.jqplot.ShapeRenderer();this.shadowRenderer=new e.jqplot.ShadowRenderer()};e.jqplot.LineRenderer.prototype.init=function(l){e.extend(true,this.renderer,l);var o={lineJoin:"round",lineCap:"round",fill:this.fill,isarc:false,strokeStyle:this.color,fillStyle:this.fillColor,lineWidth:this.lineWidth,closePath:this.fill};this.renderer.shapeRenderer.init(o);if(this.lineWidth>2.5){var n=this.shadowOffset*(1+(Math.atan((this.lineWidth/2.5))/0.785398163-1)*0.6)}else{var n=this.shadowOffset*Math.atan((this.lineWidth/2.5))/0.785398163}var k={lineJoin:"round",lineCap:"round",fill:this.fill,isarc:false,angle:this.shadowAngle,offset:n,alpha:this.shadowAlpha,depth:this.shadowDepth,lineWidth:this.lineWidth,closePath:this.fill};this.renderer.shadowRenderer.init(k)};e.jqplot.LineRenderer.prototype.setGridData=function(){var l=this._xaxis.series_u2p;var q=this._yaxis.series_u2p;var n=this._plotData;var o=this._prevPlotData;this.gridData=[];this._prevGridData=[];for(var k=0;k<this.data.length;k++){if(n[k]!=null){this.gridData.push([l.call(this._xaxis,n[k][0]),q.call(this._yaxis,n[k][1])])}if(o[k]!=null){this._prevGridData.push([l.call(this._xaxis,o[k][0]),q.call(this._yaxis,o[k][1])])}}};e.jqplot.LineRenderer.prototype.makeGridData=function(o){var n=this._xaxis.series_u2p;var q=this._yaxis.series_u2p;var l=[];var r=[];for(var k=0;k<o.length;k++){if(o[k]!=null){l.push([n.call(this._xaxis,o[k][0]),q.call(this._yaxis,o[k][1])])}}return l};e.jqplot.LineRenderer.prototype.draw=function(y,v,z){var q;var l=(z!=b)?z:{};var w=(l.shadow!=b)?l.shadow:this.shadow;var t=(l.showLine!=b)?l.showLine:this.showLine;var x=(l.fill!=b)?l.fill:this.fill;var k=(l.fillAndStroke!=b)?l.fillAndStroke:this.fillAndStroke;y.save();if(v.length){if(t){if(x){if(k){var r=v.slice(0)}if(this.index==0||!this._stack){var u=this._yaxis.series_u2p(this._yaxis.min)-this.gridBorderWidth/2;v.unshift([v[0][0],u]);len=v.length;v.push([v[len-1][0],u])}else{var o=this._prevGridData;for(var q=o.length;q>0;q--){v.push(o[q-1])}}}if(w){this.renderer.shadowRenderer.draw(y,v,l)}this.renderer.shapeRenderer.draw(y,v,l);if(k){var n=e.extend(true,{},l,{fill:false,closePath:false});this.renderer.shapeRenderer.draw(y,r,n);if(this.markerRenderer.show){for(q=0;q<r.length;q++){this.markerRenderer.draw(r[q][0],r[q][1],y,l.markerOptions)}}}}if(this.markerRenderer.show&&!x){for(q=0;q<v.length;q++){this.markerRenderer.draw(v[q][0],v[q][1],y,l.markerOptions)}}}y.restore()};e.jqplot.LineRenderer.prototype.drawShadow=function(k,n,l){};e.jqplot.LinearAxisRenderer=function(){};e.jqplot.LinearAxisRenderer.prototype.init=function(n){e.extend(true,this,n);var k=this._dataBounds;for(var o=0;o<this._series.length;o++){var q=this._series[o];var r=q._plotData;for(var l=0;l<r.length;l++){if(this.name=="xaxis"||this.name=="x2axis"){if(r[l][0]<k.min||k.min==null){k.min=r[l][0]}if(r[l][0]>k.max||k.max==null){k.max=r[l][0]}}else{if(r[l][1]<k.min||k.min==null){k.min=r[l][1]}if(r[l][1]>k.max||k.max==null){k.max=r[l][1]}}}}};e.jqplot.LinearAxisRenderer.prototype.draw=function(k){if(this.show){this.renderer.createTicks.call(this);var u=0;var l;this._elem=e('<div class="jqplot-axis jqplot-'+this.name+'" style="position:absolute;"></div>');if(this.name=="xaxis"||this.name=="x2axis"){this._elem.width(this._plotDimensions.width)}else{this._elem.height(this._plotDimensions.height)}this.labelOptions.axis=this.name;this._label=new this.labelRenderer(this.labelOptions);if(this._label.show){var r=this._label.draw(k);r.appendTo(this._elem)}if(this.showTicks){var q=this._ticks;for(var o=0;o<q.length;o++){var n=q[o];if(n.showLabel&&(!n.isMinorTick||this.showMinorTicks)){var r=n.draw(k);r.appendTo(this._elem)}}}}return this._elem};e.jqplot.LinearAxisRenderer.prototype.set=function(){var v=0;var n;var l=0;var u=0;var k=(this._label==null)?false:this._label.show;if(this.show&&this.showTicks){var r=this._ticks;for(var q=0;q<r.length;q++){var o=r[q];if(o.showLabel&&(!o.isMinorTick||this.showMinorTicks)){if(this.name=="xaxis"||this.name=="x2axis"){n=o._elem.outerHeight(true)}else{n=o._elem.outerWidth(true)}if(n>v){v=n}}}if(k){l=this._label._elem.outerWidth(true);u=this._label._elem.outerHeight(true)}if(this.name=="xaxis"){v=v+u;this._elem.css({height:v+"px",left:"0px",bottom:"0px"})}else{if(this.name=="x2axis"){v=v+u;this._elem.css({height:v+"px",left:"0px",top:"0px"})}else{if(this.name=="yaxis"){v=v+l;this._elem.css({width:v+"px",left:"0px",top:"0px"});if(k&&this._label.constructor==e.jqplot.AxisLabelRenderer){this._label._elem.css("width",l+"px")}}else{v=v+l;this._elem.css({width:v+"px",right:"0px",top:"0px"});if(k&&this._label.constructor==e.jqplot.AxisLabelRenderer){this._label._elem.css("width",l+"px")}}}}}};e.jqplot.LinearAxisRenderer.prototype.createTicks=function(){var I=this._ticks;var F=this.ticks;var J=this.name;var H=this._dataBounds;var B,G;var z,C;var n,l;var k,D;if(F.length){for(D=0;D<F.length;D++){var q=F[D];var u=new this.tickRenderer(this.tickOptions);if(q.constructor==Array){u.value=q[0];u.label=q[1];if(!this.showTicks){u.showLabel=false;u.showMark=false}else{if(!this.showTickMarks){u.showMark=false}}u.setTick(q[0],this.name);this._ticks.push(u)}else{u.value=q;if(!this.showTicks){u.showLabel=false;u.showMark=false}else{if(!this.showTickMarks){u.showMark=false}}u.setTick(q,this.name);this._ticks.push(u)}}this.numberTicks=F.length;this.min=this._ticks[0].value;this.max=this._ticks[this.numberTicks-1].value;this.tickInterval=(this.max-this.min)/(this.numberTicks-1)}else{if(J=="xaxis"||J=="x2axis"){B=this._plotDimensions.width}else{B=this._plotDimensions.height}if(!this.autoscale&&this.min!=null&&this.max!=null&&this.numberTicks!=null){this.tickInterval=null}z=((this.min!=null)?this.min:H.min);C=((this.max!=null)?this.max:H.max);if(z==C){var r=0.05;if(z>0){r=Math.max(Math.log(z)/Math.LN10,0.05)}z-=r;C+=r}var w=C-z;var y,A;var E;if(this.autoscale&&this.min==null&&this.max==null){var v,o,x;if(this.numberTicks==null){if(B>100){this.numberTicks=parseInt(3+(B-100)/75,10)}else{this.numberTicks=2}}if(this.tickInterval==null){o=w/(this.numberTicks-1);if(o<1){E=Math.pow(10,Math.abs(Math.floor(Math.log(o)/Math.LN10)))}else{E=1}this.tickInterval=Math.ceil(o*E*this.pad)/E}v=this.tickInterval*(this.numberTicks-1);x=(v-w)/2;if(this.min==null){this.min=Math.floor(E*(z-x))/E}if(this.max==null){this.max=this.min+v}}else{y=(this.min!=null)?this.min:z-w*(this.padMin-1);A=(this.max!=null)?this.max:C+w*(this.padMax-1);this.min=y;this.max=A;w=this.max-this.min;if(this.numberTicks==null){if(this.tickInterval!=null){this.numberTicks=Math.ceil((this.max-this.min)/this.tickInterval)+1;this.max=this.min+this.tickInterval*(this.numberTicks-1)}else{if(B>100){this.numberTicks=parseInt(3+(B-100)/75,10)}else{this.numberTicks=2}}}if(this.tickInterval==null){this.tickInterval=w/(this.numberTicks-1)}}for(var D=0;D<this.numberTicks;D++){k=this.min+D*this.tickInterval;var u=new this.tickRenderer(this.tickOptions);if(!this.showTicks){u.showLabel=false;u.showMark=false}else{if(!this.showTickMarks){u.showMark=false}}u.setTick(k,this.name);this._ticks.push(u)}}};e.jqplot.LinearAxisRenderer.prototype.pack=function(y,q){var B=this._ticks;var z=this.max;var u=this.min;var o=q.max;var F=q.min;var v=(this._label==null)?false:this._label.show;for(var k in y){this._elem.css(k,y[k])}this._offsets=q;var n=o-F;var E=z-u;this.p2u=function(t){return(t-F)*E/n+u};this.u2p=function(t){return(t-u)*n/E+F};if(this.name=="xaxis"||this.name=="x2axis"){this.series_u2p=function(t){return(t-u)*n/E};this.series_p2u=function(t){return t*E/n+u}}else{this.series_u2p=function(t){return(t-z)*n/E};this.series_p2u=function(t){return t*E/n+z}}if(this.show){if(this.name=="xaxis"||this.name=="x2axis"){for(i=0;i<B.length;i++){var D=B[i];if(D.show&&D.showLabel){var r;if(D.constructor==e.jqplot.CanvasAxisTickRenderer&&D.angle){var C=(this.name=="xaxis")?1:-1;switch(D.labelPosition){case"auto":if(C*D.angle<0){r=-D.getWidth()+D._textRenderer.height*Math.sin(-D._textRenderer.angle)/2}else{r=-D._textRenderer.height*Math.sin(D._textRenderer.angle)/2}break;case"end":r=-D.getWidth()+D._textRenderer.height*Math.sin(-D._textRenderer.angle)/2;break;case"start":r=-D._textRenderer.height*Math.sin(D._textRenderer.angle)/2;break;case"middle":r=-D.getWidth()/2+D._textRenderer.height*Math.sin(-D._textRenderer.angle)/2;break;default:r=-D.getWidth()/2+D._textRenderer.height*Math.sin(-D._textRenderer.angle)/2;break}}else{r=-D.getWidth()/2}var l=this.u2p(D.value)+r+"px";D._elem.css("left",l);D.pack()}}if(v){var A=this._label._elem.outerWidth(true);this._label._elem.css("left",F+n/2-A/2+"px");if(this.name=="xaxis"){this._label._elem.css("bottom","0px")}else{this._label._elem.css("top","0px")}this._label.pack()}}else{for(i=0;i<B.length;i++){var D=B[i];if(D.show&&D.showLabel){var r;if(D.constructor==e.jqplot.CanvasAxisTickRenderer&&D.angle){var C=(this.name=="yaxis")?1:-1;switch(D.labelPosition){case"auto":case"end":if(C*D.angle<0){r=-D._textRenderer.height*Math.cos(-D._textRenderer.angle)/2}else{r=-D.getHeight()+D._textRenderer.height*Math.cos(D._textRenderer.angle)/2}break;case"start":if(D.angle>0){r=-D._textRenderer.height*Math.cos(-D._textRenderer.angle)/2}else{r=-D.getHeight()+D._textRenderer.height*Math.cos(D._textRenderer.angle)/2}break;case"middle":r=-D.getHeight()/2;break;default:r=-D.getHeight()/2;break}}else{r=-D.getHeight()/2}var l=this.u2p(D.value)+r+"px";D._elem.css("top",l);D.pack()}}if(v){var x=this._label._elem.outerHeight(true);this._label._elem.css("top",o-n/2-x/2+"px");if(this.name=="yaxis"){this._label._elem.css("left","0px")}else{this._label._elem.css("right","0px")}this._label.pack()}}}};e.jqplot.MarkerRenderer=function(k){this.show=true;this.style="filledCircle";this.lineWidth=2;this.size=9;this.color="#666666";this.shadow=true;this.shadowAngle=45;this.shadowOffset=1;this.shadowDepth=3;this.shadowAlpha="0.07";this.shadowRenderer=new e.jqplot.ShadowRenderer();this.shapeRenderer=new e.jqplot.ShapeRenderer();e.extend(true,this,k)};e.jqplot.MarkerRenderer.prototype.init=function(k){e.extend(true,this,k);var n={angle:this.shadowAngle,offset:this.shadowOffset,alpha:this.shadowAlpha,lineWidth:this.lineWidth,depth:this.shadowDepth,closePath:true};if(this.style.indexOf("filled")!=-1){n.fill=true}if(this.style.indexOf("ircle")!=-1){n.isarc=true;n.closePath=false}this.shadowRenderer.init(n);var l={fill:false,isarc:false,strokeStyle:this.color,fillStyle:this.color,lineWidth:this.lineWidth,closePath:true};if(this.style.indexOf("filled")!=-1){l.fill=true}if(this.style.indexOf("ircle")!=-1){l.isarc=true;l.closePath=false}this.shapeRenderer.init(l)};e.jqplot.MarkerRenderer.prototype.drawDiamond=function(n,l,r,q,u){var k=1.2;var v=this.size/2/k;var t=this.size/2*k;var o=[[n-v,l],[n,l+t],[n+v,l],[n,l-t]];if(this.shadow){this.shadowRenderer.draw(r,o)}this.shapeRenderer.draw(r,o,u);r.restore()};e.jqplot.MarkerRenderer.prototype.drawSquare=function(n,l,r,q,u){var k=1;var v=this.size/2/k;var t=this.size/2*k;var o=[[n-v,l-t],[n-v,l+t],[n+v,l+t],[n+v,l-t]];if(this.shadow){this.shadowRenderer.draw(r,o)}this.shapeRenderer.draw(r,o,u);r.restore()};e.jqplot.MarkerRenderer.prototype.drawCircle=function(l,u,o,t,q){var k=this.size/2;var n=2*Math.PI;var r=[l,u,k,0,n,true];if(this.shadow){this.shadowRenderer.draw(o,r)}this.shapeRenderer.draw(o,r,q);o.restore()};e.jqplot.MarkerRenderer.prototype.draw=function(k,o,l,n){n=n||{};switch(this.style){case"diamond":this.drawDiamond(k,o,l,false,n);break;case"filledDiamond":this.drawDiamond(k,o,l,true,n);break;case"circle":this.drawCircle(k,o,l,false,n);break;case"filledCircle":this.drawCircle(k,o,l,true,n);break;case"square":this.drawSquare(k,o,l,false,n);break;case"filledSquare":this.drawSquare(k,o,l,true,n);break;default:this.drawDiamond(k,o,l,false,n);break}};e.jqplot.ShadowRenderer=function(k){this.angle=45;this.offset=1;this.alpha=0.07;this.lineWidth=1.5;this.lineJoin="miter";this.lineCap="round";this.closePath=false;this.fill=false;this.depth=3;this.isarc=false;e.extend(true,this,k)};e.jqplot.ShadowRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.ShadowRenderer.prototype.draw=function(w,u,x){w.save();var k=(x!=null)?x:{};var v=(k.fill!=null)?k.fill:this.fill;var t=(k.closePath!=null)?k.closePath:this.closePath;var o=(k.offset!=null)?k.offset:this.offset;var l=(k.alpha!=null)?k.alpha:this.alpha;var r=(k.depth!=null)?k.depth:this.depth;w.lineWidth=(k.lineWidth!=null)?k.lineWidth:this.lineWidth;w.lineJoin=(k.lineJoin!=null)?k.lineJoin:this.lineJoin;w.lineCap=(k.lineCap!=null)?k.lineCap:this.lineCap;w.strokeStyle="rgba(0,0,0,"+l+")";w.fillStyle="rgba(0,0,0,"+l+")";for(var n=0;n<r;n++){w.translate(Math.cos(this.angle*Math.PI/180)*o,Math.sin(this.angle*Math.PI/180)*o);w.beginPath();if(this.isarc){w.arc(u[0],u[1],u[2],u[3],u[4],true)}else{w.moveTo(u[0][0],u[0][1]);for(var q=1;q<u.length;q++){w.lineTo(u[q][0],u[q][1])}}if(t){w.closePath()}if(v){w.fill()}else{w.stroke()}}w.restore()};e.jqplot.ShapeRenderer=function(k){this.lineWidth=1.5;this.lineJoin="miter";this.lineCap="round";this.closePath=false;this.fill=false;this.isarc=false;this.fillRect=false;this.strokeRect=false;this.clearRect=false;this.strokeStyle="#999999";this.fillStyle="#999999";e.extend(true,this,k)};e.jqplot.ShapeRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.ShapeRenderer.prototype.draw=function(v,t,x){v.save();var k=(x!=null)?x:{};var u=(k.fill!=null)?k.fill:this.fill;var q=(k.closePath!=null)?k.closePath:this.closePath;var r=(k.fillRect!=null)?k.fillRect:this.fillRect;var n=(k.strokeRect!=null)?k.strokeRect:this.strokeRect;var l=(k.clearRect!=null)?k.clearRect:this.clearRect;var w=(k.isarc!=null)?k.isarc:this.isarc;v.lineWidth=k.lineWidth||this.lineWidth;v.lineJoin=k.lineJoing||this.lineJoin;v.lineCap=k.lineCap||this.lineCap;v.strokeStyle=(k.strokeStyle||k.color)||this.strokeStyle;v.fillStyle=k.fillStyle||this.fillStyle;v.beginPath();if(w){v.arc(t[0],t[1],t[2],t[3],t[4],true);if(q){v.closePath()}if(u){v.fill()}else{v.stroke()}}else{if(r){v.fillRect(t[0],t[1],t[2],t[3])}else{if(n){v.strokeRect(t[0],t[1],t[2],t[3])}else{if(l){v.clearRect(t[0],t[1],t[2],t[3])}else{v.moveTo(t[0][0],t[0][1]);for(var o=1;o<t.length;o++){v.lineTo(t[o][0],t[o][1])}if(q){v.closePath()}if(u){v.fill()}else{v.stroke()}}}}}v.restore()};e.jqplot.TableLegendRenderer.prototype.init=function(k){e.extend(true,this,k)};e.jqplot.TableLegendRenderer.prototype.draw=function(){var t=this;if(this.show){var q=this._series;var w="position:absolute;";w+=(this.background)?"background:"+this.background+";":"";w+=(this.border)?"border:"+this.border+";":"";w+=(this.fontSize)?"font-size:"+this.fontSize+";":"";w+=(this.fontFamily)?"font-family:"+this.fontFamily+";":"";w+=(this.textColor)?"color:"+this.textColor+";":"";this._elem=e('<table class="jqplot-legend" style="'+w+'"></table>');var k=false;for(var r=0;r<q.length;r++){s=q[r];if(s.show){var o=s.label.toString();if(o){var l=s.color;if(s._stack&&!s.fill){l=""}v.call(this,o,l,k);k=true}for(var n=0;n<e.jqplot.addLegendRowHooks.length;n++){var u=e.jqplot.addLegendRowHooks[n].call(this,s);if(u){v.call(this,u.label,u.color,k);k=true}}}}}function v(z,y,C){var x=(C)?this.rowSpacing:"0";var B=e('<tr class="jqplot-legend"></tr>').appendTo(this._elem);e('<td class="jqplot-legend" style="vertical-align:middle;text-align:center;padding-top:'+x+';"><div style="border:1px solid #cccccc;padding:0.2em;"><div style="width:1.2em;height:0.7em;background-color:'+y+';"></div></div></td>').appendTo(B);var A=e('<td class="jqplot-legend" style="vertical-align:middle;padding-top:'+x+';"></td>');A.appendTo(B);if(this.escapeHtml){A.text(z)}else{A.html(z)}}return this._elem};e.jqplot.TableLegendRenderer.prototype.pack=function(o){if(this.show){var n={_top:o.top,_left:o.left,_right:o.right,_bottom:this._plotDimensions.height-o.bottom};switch(this.location){case"nw":var l=n._left+this.xoffset;var k=n._top+this.yoffset;this._elem.css("left",l);this._elem.css("top",k);break;case"n":var l=(o.left+(this._plotDimensions.width-o.right))/2-this.getWidth()/2;var k=n._top+this.yoffset;this._elem.css("left",l);this._elem.css("top",k);break;case"ne":var l=o.right+this.xoffset;var k=n._top+this.yoffset;this._elem.css({right:l,top:k});break;case"e":var l=o.right+this.xoffset;var k=(o.top+(this._plotDimensions.height-o.bottom))/2-this.getHeight()/2;this._elem.css({right:l,top:k});break;case"se":var l=o.right+this.xoffset;var k=o.bottom+this.yoffset;this._elem.css({right:l,bottom:k});break;case"s":var l=(o.left+(this._plotDimensions.width-o.right))/2-this.getWidth()/2;var k=o.bottom+this.yoffset;this._elem.css({left:l,bottom:k});break;case"sw":var l=n._left+this.xoffset;var k=o.bottom+this.yoffset;this._elem.css({left:l,bottom:k});break;case"w":var l=n._left+this.xoffset;var k=(o.top+(this._plotDimensions.height-o.bottom))/2-this.getHeight()/2;this._elem.css({left:l,top:k});break;default:var l=n._right-this.xoffset;var k=n._bottom+this.yoffset;this._elem.css({right:l,bottom:k});break}}};e.jqplot.sprintf=function(){function r(y,u,v,x){var w=(y.length>=u)?"":Array(1+u-y.length>>>0).join(v);return x?y+w:w+y}function n(x,w,z,u,v){var y=u-x.length;if(y>0){if(z||!v){x=r(x,u," ",z)}else{x=x.slice(0,w.length)+r("",y,"0",true)+x.slice(w.length)}}return x}function t(A,z,y,B,v,u,x){var w=A>>>0;y=y&&w&&{"2":"0b","8":"0","16":"0x"}[z]||"";A=y+r(w.toString(z),u||0,"0",false);return n(A,y,B,v,x)}function k(x,y,v,u,w){if(u!=null){x=x.slice(0,u)}return n(x,"",y,v,w)}var l=arguments,o=0,q=l[o++];return q.replace(e.jqplot.sprintf.regex,function(M,z,A,D,O,K,y){if(M=="%%"){return"%"}var E=false,B="",C=false,L=false;for(var J=0;A&&J<A.length;J++){switch(A.charAt(J)){case" ":B=" ";break;case"+":B="+";break;case"-":E=true;break;case"0":C=true;break;case"#":L=true;break}}if(!D){D=0}else{if(D=="*"){D=+l[o++]}else{if(D.charAt(0)=="*"){D=+l[D.slice(1,-1)]}else{D=+D}}}if(D<0){D=-D;E=true}if(!isFinite(D)){throw new Error("$.jqplot.sprintf: (minimum-)width must be finite")}if(!K){K="fFeE".indexOf(y)>-1?6:(y=="d")?0:void (0)}else{if(K=="*"){K=+l[o++]}else{if(K.charAt(0)=="*"){K=+l[K.slice(1,-1)]}else{K=+K}}}var G=z?l[z.slice(0,-1)]:l[o++];switch(y){case"s":return k(String(G),E,D,K,C);case"c":return k(String.fromCharCode(+G),E,D,K,C);case"b":return t(G,2,L,E,D,K,C);case"o":return t(G,8,L,E,D,K,C);case"x":return t(G,16,L,E,D,K,C);case"X":return t(G,16,L,E,D,K,C).toUpperCase();case"u":return t(G,10,L,E,D,K,C);case"i":case"d":var w=parseInt(+G);var I=w<0?"-":B;G=I+r(String(Math.abs(w)),K,"0",false);return n(G,I,E,D,C);case"e":case"E":case"f":case"F":case"g":case"G":var w=+G;var I=w<0?"-":B;var x=["toExponential","toFixed","toPrecision"]["efg".indexOf(y.toLowerCase())];var N=["toString","toUpperCase"]["eEfFgG".indexOf(y)%2];G=I+Math.abs(w)[x](K);return n(G,I,E,D,C)[N]();case"p":case"P":var w=+G;var I=w<0?"-":B;var F=String(Number(Math.abs(w)).toExponential()).split(/e|E/);var v=(F[0].indexOf(".")!=-1)?F[0].length-1:F[0].length;var H=(F[1]<0)?-F[1]-1:0;if(Math.abs(w)<1){if(v+H<=K){G=I+Math.abs(w).toPrecision(v)}else{if(v<=K-1){G=I+Math.abs(w).toExponential(v-1)}else{G=I+Math.abs(w).toExponential(K-1)}}}else{var u=(v<=K)?v:K;G=I+Math.abs(w).toPrecision(u)}var N=["toString","toUpperCase"]["pP".indexOf(y)%2];return n(G,I,E,D,C)[N]();default:return M}})};e.jqplot.sprintf.regex=/%%|%(\d+\$)?([-+#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegpEGP])/g})(jQuery);

//cursor
(function(g){g.jqplot.Cursor=function(m){this.style="crosshair";this.previousCursor="auto";this.show=true;this.showTooltip=true;this.followMouse=false;this.tooltipLocation="se";this.tooltipOffset=6;this.showTooltipGridPosition=false;this.showTooltipUnitPosition=true;this.tooltipFormatString="%.4P";this.useAxesFormatters=true;this.tooltipAxisGroups=[];this.zoom=false;this.clickReset=false;this.dblClickReset=true;this._zoom={start:[],end:[],started:false,zooming:false,axes:{start:{},end:{}}};this._tooltipElem;this.zoomCanvas;g.extend(true,this,m)};g.jqplot.Cursor.init=function(p,o,n){var m=n||{};this.plugins.cursor=new g.jqplot.Cursor(m.cursor);if(this.plugins.cursor.show){g.jqplot.eventListenerHooks.push(["jqplotMouseEnter",b]);g.jqplot.eventListenerHooks.push(["jqplotMouseLeave",e]);g.jqplot.eventListenerHooks.push(["jqplotMouseMove",f]);if(this.plugins.cursor.zoom){g.jqplot.eventListenerHooks.push(["jqplotMouseDown",a]);g.jqplot.eventListenerHooks.push(["jqplotMouseUp",l]);if(this.plugins.cursor.clickReset){g.jqplot.eventListenerHooks.push(["jqplotClick",h])}if(this.plugins.cursor.dblClickReset){g.jqplot.eventListenerHooks.push(["jqplotDblClick",c])}}}};g.jqplot.Cursor.postDraw=function(){var t=this.plugins.cursor;t._tooltipElem=g('<div class="jqplot-cursor-tooltip" style="position:absolute;display:none"></div>');this.target.append(t._tooltipElem);if(t.zoom){t.zoomCanvas=new g.jqplot.GenericCanvas();this.eventCanvas._elem.before(t.zoomCanvas.createElement(this._gridPadding,"jqplot-zoom-canvas",this._plotDimensions));var r=t.zoomCanvas.setContext()}if(t.showTooltipUnitPosition){if(t.tooltipAxisGroups.length===0){var o=this.series;var p;var m=[];for(var n=0;n<o.length;n++){p=o[n];var q=p.xaxis+","+p.yaxis;if(g.inArray(q,m)==-1){m.push(q)}}for(var n=0;n<m.length;n++){t.tooltipAxisGroups.push(m[n].split(","))}}}};g.jqplot.Cursor.resetZoom=function(p){var o=p.axes;var n=p.plugins.cursor._zoom.axes;for(var m in o){o[m]._ticks=[];o[m].min=n[m].min;o[m].max=n[m].max;o[m].numberTicks=n[m].numberTicks;o[m].tickInterval=n[m].tickInterval;o[m]._tickInterval=n[m]._tickInterval}p.redraw()};g.jqplot.preInitHooks.push(g.jqplot.Cursor.init);g.jqplot.postDrawHooks.push(g.jqplot.Cursor.postDraw);function d(q,n,u){var w=u.plugins.cursor;var y="";var m=false;if(w.showTooltipGridPosition){y=q.x+", "+q.y;m=true}if(w.showTooltipUnitPosition){var r;for(var o=0;o<w.tooltipAxisGroups.length;o++){r=w.tooltipAxisGroups[o];if(m){y+="<br />"}if(w.useAxesFormatters){var x=u.axes[r[0]]._ticks[0].formatter;var p=u.axes[r[1]]._ticks[0].formatter;var v=u.axes[r[0]]._ticks[0].formatString;var t=u.axes[r[1]]._ticks[0].formatString;y+=x(v,n[r[0]])+", "+p(t,n[r[1]])}else{y+=g.jqplot.sprintf(w.tooltipFormatString,n[r[0]])+", "+g.jqplot.sprintf(w.tooltipFormatString,n[r[1]])}m=true}}w._tooltipElem.html(y)}function k(n,p){var r=p.plugins.cursor;var o=r._tooltipElem;switch(r.tooltipLocation){case"nw":var m=n.x+p._gridPadding.left-o.outerWidth(true)-r.tooltipOffset;var q=n.y+p._gridPadding.top-r.tooltipOffset-o.outerHeight(true);break;case"n":var m=n.x+p._gridPadding.left-o.outerWidth(true)/2;var q=n.y+p._gridPadding.top-r.tooltipOffset-o.outerHeight(true);break;case"ne":var m=n.x+p._gridPadding.left+r.tooltipOffset;var q=n.y+p._gridPadding.top-r.tooltipOffset-o.outerHeight(true);break;case"e":var m=n.x+p._gridPadding.left+r.tooltipOffset;var q=n.y+p._gridPadding.top-o.outerHeight(true)/2;break;case"se":var m=n.x+p._gridPadding.left+r.tooltipOffset;var q=n.y+p._gridPadding.top+r.tooltipOffset;break;case"s":var m=n.x+p._gridPadding.left-o.outerWidth(true)/2;var q=n.y+p._gridPadding.top+r.tooltipOffset;break;case"sw":var m=n.x+p._gridPadding.left-o.outerWidth(true)-r.tooltipOffset;var q=n.y+p._gridPadding.top+r.tooltipOffset;break;case"w":var m=n.x+p._gridPadding.left-o.outerWidth(true)-r.tooltipOffset;var q=n.y+p._gridPadding.top-o.outerHeight(true)/2;break;default:var m=n.x+p._gridPadding.left+r.tooltipOffset;var q=n.y+p._gridPadding.top+r.tooltipOffset;break}r._tooltipElem.css("left",m);r._tooltipElem.css("top",q)}function j(q){var o=q._gridPadding;var r=q.plugins.cursor;var p=r._tooltipElem;switch(r.tooltipLocation){case"nw":var n=o.left+r.tooltipOffset;var m=o.top+r.tooltipOffset;p.css("left",n);p.css("top",m);break;case"n":var n=(o.left+(q._plotDimensions.width-o.right))/2-p.outerWidth(true)/2;var m=o.top+r.tooltipOffset;p.css("left",n);p.css("top",m);break;case"ne":var n=o.right+r.tooltipOffset;var m=o.top+r.tooltipOffset;p.css({right:n,top:m});break;case"e":var n=o.right+r.tooltipOffset;var m=(o.top+(q._plotDimensions.height-o.bottom))/2-p.outerHeight(true)/2;p.css({right:n,top:m});break;case"se":var n=o.right+r.tooltipOffset;var m=o.bottom+r.tooltipOffset;p.css({right:n,bottom:m});break;case"s":var n=(o.left+(q._plotDimensions.width-o.right))/2-p.outerWidth(true)/2;var m=o.bottom+r.tooltipOffset;p.css({left:n,bottom:m});break;case"sw":var n=o.left+r.tooltipOffset;var m=o.bottom+r.tooltipOffset;p.css({left:n,bottom:m});break;case"w":var n=o.left+r.tooltipOffset;var m=(o.top+(q._plotDimensions.height-o.bottom))/2-p.outerHeight(true)/2;p.css({left:n,top:m});break;default:var n=o.right-r.tooltipOffset;var m=o.bottom+r.tooltipOffset;p.css({right:n,bottom:m});break}}function h(n,m,q,p,o){var r=o.plugins.cursor;if(r.clickReset){g.jqplot.Cursor.resetZoom(o)}return true}function c(n,m,q,p,o){var r=o.plugins.cursor;if(r.dblClickReset){g.jqplot.Cursor.resetZoom(o)}return}function e(o,n,r,q,p){var s=p.plugins.cursor;if(s.show){g(o.target).css("cursor",s.previousCursor);if(s.showTooltip){s._tooltipElem.hide()}if(s.zoom){s._zoom.started=false;s._zoom.zooming=false;var m=s.zoomCanvas._ctx;m.clearRect(0,0,m.canvas.width,m.canvas.height)}}}function b(n,m,q,p,o){var r=o.plugins.cursor;if(r.show){r.previousCursor=n.target.style.cursor;n.target.style.cursor=r.style;if(r.showTooltip){d(m,q,o);if(r.followMouse){k(m,o)}else{j(o)}r._tooltipElem.show()}}}function f(n,m,q,p,o){var r=o.plugins.cursor;if(r.show){if(r.showTooltip){d(m,q,o);if(r.followMouse){k(m,o)}}if(r.zoom&&r._zoom.started){r._zoom.zooming=true;r._zoom.end=[m.x,m.y];i.call(r)}}}function a(n,m,s,r,q){var t=q.plugins.cursor;var p=q.axes;if(t.zoom){t._zoom.start=[m.x,m.y];t._zoom.started=true;for(var o in s){t._zoom.axes.start[o]=s[o];if(t._zoom.axes[o]==undefined){t._zoom.axes[o]={};t._zoom.axes[o].numberTicks=p[o].numberTicks;t._zoom.axes[o].tickInterval=p[o].tickInterval;t._zoom.axes[o]._tickInterval=p[o]._tickInterval;t._zoom.axes[o].min=p[o].min;t._zoom.axes[o].max=p[o].max}}}}function l(x,s,p,y,t){var v=t.plugins.cursor;if(v.zoom&&v._zoom.zooming){var u=t.axes;var n=v._zoom.axes;var o=n.start;var q=n.end;var r,w;var z=v.zoomCanvas._ctx;z.clearRect(0,0,z.canvas.width,z.canvas.height);if(!(Math.abs(s.x-v._zoom.start[0])<8||Math.abs(s.y-v._zoom.start[1])<8)){for(var m in p){dp=p[m];if(dp!=null){if(dp>o[m]){u[m].min=o[m];u[m].max=dp}else{span=o[m]-dp;u[m].max=o[m];u[m].min=dp}u[m].tickInterval=null;u[m]._tickInterval=null;u[m]._ticks=[]}}t.redraw()}}v._zoom.started=false;v._zoom.zooming=false}function i(){var s=this._zoom.start;var p=this._zoom.end;var o=this.zoomCanvas._ctx;var n,q,r,m;if(p[0]>s[0]){n=s[0];m=p[0]-s[0]}else{n=p[0];m=s[0]-p[0]}if(p[1]>s[1]){q=s[1];r=p[1]-s[1]}else{q=p[1];r=s[1]-p[1]}o.fillStyle="rgba(0,0,0,0.2)";o.strokeStyle="#999999";o.lineWidth=1;o.clearRect(0,0,o.canvas.width,o.canvas.height);o.fillRect(0,0,o.canvas.width,o.canvas.height);o.clearRect(n,q,m,r);o.strokeRect(n,q,m,r)}})(jQuery);
//dateAxisRenderer
(function(d){d.jqplot.DateAxisRenderer=function(){d.jqplot.LinearAxisRenderer.call(this)};d.jqplot.DateAxisRenderer.prototype=new d.jqplot.LinearAxisRenderer();d.jqplot.DateAxisRenderer.prototype.constructor=d.jqplot.DateAxisRenderer;d.jqplot.DateTickFormatter=function(j,k){if(!j){j="%Y/%m/%d"}return Date.create(k).strftime(j)};d.jqplot.DateAxisRenderer.prototype.init=function(n){this.tickOptions.formatter=d.jqplot.DateTickFormatter;this._tickInterval=null;d.extend(true,this,n);var l=this._dataBounds;for(var o=0;o<this._series.length;o++){var p=this._series[o];var r=p.data;var k=p._plotData;var q=p._stackData;for(var m=0;m<r.length;m++){if(this.name=="xaxis"||this.name=="x2axis"){r[m][0]=Date.create(r[m][0]).getTime();k[m][0]=Date.create(r[m][0]).getTime();q[m][0]=Date.create(r[m][0]).getTime();if(r[m][0]<l.min||l.min==null){l.min=r[m][0]}if(r[m][0]>l.max||l.max==null){l.max=r[m][0]}}else{r[m][1]=Date.create(r[m][1]).getTime();k[m][1]=Date.create(r[m][1]).getTime();q[m][1]=Date.create(r[m][1]).getTime();if(r[m][1]<l.min||l.min==null){l.min=r[m][1]}if(r[m][1]>l.max||l.max==null){l.max=r[m][1]}}}}};d.jqplot.DateAxisRenderer.prototype.createTicks=function(){var C=this._ticks;var z=this.ticks;var D=this.name;var B=this._dataBounds;var w,A;var u,x;var l,k;var j,y;if(z.length){for(y=0;y<z.length;y++){var n=z[y];var p=new this.tickRenderer(this.tickOptions);if(n.constructor==Array){p.value=Date.create(n[0]).getTime();p.label=n[1];if(!this.showTicks){p.showLabel=false;p.showMark=false}else{if(!this.showTickMarks){p.showMark=false}}p.setTick(p.value,this.name);this._ticks.push(p)}else{p.value=Date.create(n).getTime();if(!this.showTicks){p.showLabel=false;p.showMark=false}else{if(!this.showTickMarks){p.showMark=false}}p.setTick(p.value,this.name);this._ticks.push(p)}}this.numberTicks=z.length;this.min=this._ticks[0].value;this.max=this._ticks[this.numberTicks-1].value;this._tickInterval=[(this.max-this.min)/(this.numberTicks-1)/1000,"seconds"]}else{if(D=="xaxis"||D=="x2axis"){w=this._plotDimensions.width}else{w=this._plotDimensions.height}if(this.min!=null&&this.max!=null&&this.numberTicks!=null){this.tickInterval=null}if(this.tickInterval!=null){if(Number(this.tickInterval)){this._tickInterval=[Number(this.tickInterval),"seconds"]}else{if(typeof this.tickInterval=="string"){var r=this.tickInterval.split(" ");if(r.length==1){this._tickInterval=[1,r[0]]}else{if(r.length==2){this._tickInterval=[r[0],r[1]]}}}}}u=((this.min!=null)?Date.create(this.min).getTime():B.min);x=((this.max!=null)?Date.create(this.max).getTime():B.max);if(u==x){var o=24*60*60*500;u-=o;x+=o}var q=x-u;var s,v;s=(this.min!=null)?Date.create(this.min).getTime():u-q/2*(this.padMin-1);v=(this.max!=null)?Date.create(this.max).getTime():x+q/2*(this.padMax-1);this.min=s;this.max=v;q=this.max-this.min;if(this.numberTicks==null){if(this._tickInterval!=null){var m=Date.create(this.max).diff(this.min,this._tickInterval[1],true);this.numberTicks=Math.ceil(m/this._tickInterval[0])+1;this.max=Date.create(this.min).add((this.numberTicks-1)*this._tickInterval[0],this._tickInterval[1]).getTime()}else{if(w>200){this.numberTicks=parseInt(3+(w-200)/100,10)}else{this.numberTicks=2}}}if(this._tickInterval==null){this._tickInterval=[q/(this.numberTicks-1)/1000,"seconds"]}for(var y=0;y<this.numberTicks;y++){var u=Date.create(this.min);j=u.add(y*this._tickInterval[0],this._tickInterval[1]).getTime();var p=new this.tickRenderer(this.tickOptions);if(!this.showTicks){p.showLabel=false;p.showMark=false}else{if(!this.showTickMarks){p.showMark=false}}p.setTick(j,this.name);this._ticks.push(p)}}};var g=24*60*60*1000;var e=function(j,k){j=String(j);while(j.length<k){j="0"+j}return j};var c={millisecond:1,second:1000,minute:60*1000,hour:60*60*1000,day:g,week:7*g,month:{add:function(l,j){c.year.add(l,Math[j>0?"floor":"ceil"](j/12));var k=l.getMonth()+(j%12);if(k==12){k=0;l.setYear(l.getFullYear()+1)}else{if(k==-1){k=11;l.setYear(l.getFullYear()-1)}}l.setMonth(k)},diff:function(n,l){var j=n.getFullYear()-l.getFullYear();var k=n.getMonth()-l.getMonth()+(j*12);var m=n.getDate()-l.getDate();return k+(m/30)}},year:{add:function(k,j){k.setYear(k.getFullYear()+Math[j>0?"floor":"ceil"](j))},diff:function(k,j){return c.month.diff(k,j)/12}}};for(var i in c){if(i.substring(i.length-1)!="s"){c[i+"s"]=c[i]}}var h=function(m,l){if(Date.prototype.strftime.formatShortcuts[l]){return m.strftime(Date.prototype.strftime.formatShortcuts[l])}else{var j=(Date.prototype.strftime.formatCodes[l]||"").split(".");var k=m["get"+j[0]]?m["get"+j[0]]():"";if(j[1]){k=e(k,j[1])}return k}};var f={succ:function(j){return this.clone().add(1,j)},add:function(l,k){var j=c[k]||c.day;if(typeof j=="number"){this.setTime(this.getTime()+(j*l))}else{j.add(this,l)}return this},diff:function(k,n,j){k=Date.create(k);if(k===null){return null}var l=c[n]||c.day;if(typeof l=="number"){var m=(this.getTime()-k.getTime())/l}else{var m=l.diff(this,k)}return(j?m:Math[m>0?"floor":"ceil"](m))},strftime:function(k){var m=k||"%Y-%m-%d",j="",l;while(m.length>0){if(l=m.match(Date.prototype.strftime.formatCodes.matcher)){j+=m.slice(0,l.index);j+=(l[1]||"")+h(this,l[2]);m=m.slice(l.index+l[0].length)}else{j+=m;m=""}}return j},getShortYear:function(){return this.getYear()%100},getMonthNumber:function(){return this.getMonth()+1},getMonthName:function(){return Date.MONTHNAMES[this.getMonth()]},getAbbrMonthName:function(){return Date.ABBR_MONTHNAMES[this.getMonth()]},getDayName:function(){return Date.DAYNAMES[this.getDay()]},getAbbrDayName:function(){return Date.ABBR_DAYNAMES[this.getDay()]},getDayOrdinal:function(){return Date.ORDINALNAMES[this.getDate()%10]},getHours12:function(){var j=this.getHours();return j>12?j-12:(j==0?12:j)},getAmPm:function(){return this.getHours()>=12?"PM":"AM"},getUnix:function(){return Math.round(this.getTime()/1000,0)},getGmtOffset:function(){var j=this.getTimezoneOffset()/60;var k=j<0?"+":"-";j=Math.abs(j);return k+e(Math.floor(j),2)+":"+e((j%1)*60,2)},getTimezoneName:function(){var j=/(?:\((.+)\)$| ([A-Z]{3}) )/.exec(this.toString());return j[1]||j[2]||"GMT"+this.getGmtOffset()},toYmdInt:function(){return(this.getFullYear()*10000)+(this.getMonthNumber()*100)+this.getDate()},clone:function(){return new Date(this.getTime())}};for(var a in f){Date.prototype[a]=f[a]}var b={create:function(j){if(j instanceof Date){return j}if(typeof j=="number"){return new Date(j)}var o=String(j).replace(/^\s*(.+)\s*$/,"$1"),k=0,l=Date.create.patterns.length,m;var n=o;while(k<l){ms=Date.parse(n);if(!isNaN(ms)){return new Date(ms)}m=Date.create.patterns[k];if(typeof m=="function"){obj=m(n);if(obj instanceof Date){return obj}}else{n=o.replace(m[0],m[1])}k++}return NaN},MONTHNAMES:"January February March April May June July August September October November December".split(" "),ABBR_MONTHNAMES:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),DAYNAMES:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ABBR_DAYNAMES:"Sun Mon Tue Wed Thu Fri Sat".split(" "),ORDINALNAMES:"th st nd rd th th th th th th".split(" "),ISO:"%Y-%m-%dT%H:%M:%S.%N%G",SQL:"%Y-%m-%d %H:%M:%S",daysInMonth:function(j,k){if(k==2){return new Date(j,1,29).getDate()==29?29:28}return[undefined,31,undefined,31,30,31,30,31,31,30,31,30,31][k]}};for(var a in b){Date[a]=b[a]}Date.prototype.strftime.formatCodes={matcher:/()%(#?(%|[a-z]))/i,Y:"FullYear",y:"ShortYear.2",m:"MonthNumber.2","#m":"MonthNumber",B:"MonthName",b:"AbbrMonthName",d:"Date.2","#d":"Date",e:"Date",A:"DayName",a:"AbbrDayName",w:"Day",o:"DayOrdinal",H:"Hours.2","#H":"Hours",I:"Hours12.2","#I":"Hours12",p:"AmPm",M:"Minutes.2","#M":"Minutes",S:"Seconds.2","#S":"Seconds",s:"Unix",N:"Milliseconds.3","#N":"Milliseconds",O:"TimezoneOffset",Z:"TimezoneName",G:"GmtOffset"};Date.prototype.strftime.formatShortcuts={F:"%Y-%m-%d",T:"%H:%M:%S",X:"%H:%M:%S",x:"%m/%d/%y",D:"%m/%d/%y","#c":"%a %b %e %H:%M:%S %Y",v:"%e-%b-%Y",R:"%H:%M",r:"%I:%M:%S %p",t:"\t",n:"\n","%":"%"};Date.create.patterns=[[/-/g,"/"],[/st|nd|rd|th/g,""],[/(3[01]|[0-2]\d)\s*\.\s*(1[0-2]|0\d)\s*\.\s*([1-9]\d{3})/,"$2/$1/$3"],[/([1-9]\d{3})\s*-\s*(1[0-2]|0\d)\s*-\s*(3[01]|[0-2]\d)/,"$2/$3/$1"],function(m){var k=m.match(/^(?:(.+)\s+)?([012]?\d)(?:\s*\:\s*(\d\d))?(?:\s*\:\s*(\d\d(\.\d*)?))?\s*(am|pm)?\s*$/i);if(k){if(k[1]){var l=Date.create(k[1]);if(isNaN(l)){return}}else{var l=new Date();l.setMilliseconds(0)}var j=parseFloat(k[2]);if(k[6]){j=k[6].toLowerCase()=="am"?(j==12?0:j):(j==12?12:j+12)}l.setHours(j,parseInt(k[3]||0,10),parseInt(k[4]||0,10),((parseFloat(k[5]||0))||0)*1000);return l}else{return m}},function(m){var k=m.match(/^(?:(.+))[T|\s+]([012]\d)(?:\:(\d\d))(?:\:(\d\d))(?:\.\d+)([\+\-]\d\d\:\d\d)$/i);if(k){if(k[1]){var l=Date.create(k[1]);if(isNaN(l)){return}}else{var l=new Date();l.setMilliseconds(0)}var j=parseFloat(k[2]);l.setHours(j,parseInt(k[3],10),parseInt(k[4],10),parseFloat(k[5])*1000);return l}else{return m}},function(n){var l=n.match(/^([0-3]?\d)\s*[-\/.\s]{1}\s*([a-zA-Z]{3,9})\s*[-\/.\s]{1}\s*([0-3]?\d)$/);if(l){var m=new Date();var o=parseFloat(String(m.getFullYear()).slice(2,4));var p=parseInt(String(m.getFullYear())/100)*100;var r=1;var s=parseFloat(l[1]);var q=parseFloat(l[3]);var k,j,t;if(s>31){j=l[3];if(s<o+r){k=p+s}else{k=p-100+s}}else{j=l[1];if(q<o+r){k=p+q}else{k=p-100+q}}var t=d.inArray(l[2],Date.ABBR_MONTHNAMES);if(t==-1){t=d.inArray(l[2],Date.MONTHNAMES)}m.setUTCFullYear(k,t,j);m.setUTCHours(0,0,0,0);return m}else{return n}}];if(d.jqplot.debug){d.date=Date.create}})(jQuery);
//highlighter
(function(b){b.jqplot.eventListenerHooks.push(["jqplotMouseMove",c]);b.jqplot.Highlighter=function(e){this.show=true;this.markerRenderer=new b.jqplot.MarkerRenderer({shadow:false});this.showMarker=true;this.lineWidthAdjust=2.5;this.sizeAdjust=5;this.showTooltip=true;this.tooltipLocation="nw";this.fadeTooltip=true;this.tooltipFadeSpeed="fast";this.tooltipOffset=2;this.tooltipAxes="both";this.tooltipSeparator=", ";this.useAxesFormatters=true;this.tooltipFormatString="%.5P";this.formatString=null;this.yvalues=1;this._tooltipElem;this.isHighlighting=false;b.extend(true,this,e)};b.jqplot.Highlighter.init=function(h,g,f){var e=f||{};this.plugins.highlighter=new b.jqplot.Highlighter(e.highlighter)};b.jqplot.Highlighter.parseOptions=function(f,e){this.showHighlight=true};b.jqplot.Highlighter.postPlotDraw=function(){this.plugins.highlighter.highlightCanvas=new b.jqplot.GenericCanvas();this.eventCanvas._elem.before(this.plugins.highlighter.highlightCanvas.createElement(this._gridPadding,"jqplot-highlight-canvas",this._plotDimensions));var f=this.plugins.highlighter.highlightCanvas.setContext();var e=this.plugins.highlighter;e._tooltipElem=b('<div class="jqplot-highlighter-tooltip" style="position:absolute;display:none"></div>');this.target.append(e._tooltipElem)};b.jqplot.preInitHooks.push(b.jqplot.Highlighter.init);b.jqplot.preParseSeriesOptionsHooks.push(b.jqplot.Highlighter.parseOptions);b.jqplot.postDrawHooks.push(b.jqplot.Highlighter.postPlotDraw);function a(j,l){var g=j.plugins.highlighter;var m=j.series[l.seriesIndex];var e=m.markerRenderer;var f=g.markerRenderer;f.style=e.style;f.lineWidth=e.lineWidth+g.lineWidthAdjust;f.size=e.size+g.sizeAdjust;var i=b.jqplot.getColorComponents(e.color);var k=[i[0],i[1],i[2]];var h=(i[3]>=0.6)?i[3]*0.6:i[3]*(2-i[3]);f.color="rgba("+k[0]+","+k[1]+","+k[2]+","+h+")";f.init();f.draw(m.gridData[l.pointIndex][0],m.gridData[l.pointIndex][1],g.highlightCanvas._ctx)}function d(s,m,j){var g=s.plugins.highlighter;var v=g._tooltipElem;if(g.useAxesFormatters){var q=m._xaxis._ticks[0].formatter;var e=m._yaxis._ticks[0].formatter;var w=m._xaxis._ticks[0].formatString;var n=m._yaxis._ticks[0].formatString;var r;var o=q(w,j.data[0]);var h=[];for(var t=1;t<g.yvalues+1;t++){h.push(e(n,j.data[t]))}if(g.formatString){switch(g.tooltipAxes){case"both":case"xy":h.unshift(o);h.unshift(g.formatString);r=b.jqplot.sprintf.apply(b.jqplot.sprintf,h);break;case"yx":h.push(o);h.unshift(g.formatString);r=b.jqplot.sprintf.apply(b.jqplot.sprintf,h);break;case"x":r=b.jqplot.sprintf.apply(b.jqplot.sprintf,[g.formatString,o]);break;case"y":h.unshift(g.formatString);r=b.jqplot.sprintf.apply(b.jqplot.sprintf,h);break;default:h.unshift(o);h.unshift(g.formatString);r=b.jqplot.sprintf.apply(b.jqplot.sprintf,h);break}}else{switch(g.tooltipAxes){case"both":case"xy":r=o;for(var t=0;t<h.length;t++){r+=g.tooltipSeparator+h[t]}break;case"yx":r="";for(var t=0;t<h.length;t++){r+=h[t]+g.tooltipSeparator}r+=o;break;case"x":r=o;break;case"y":r="";for(var t=0;t<h.length;t++){r+=h[t]+g.tooltipSeparator}break;default:r=o;for(var t=0;t<h.length;t++){r+=g.tooltipSeparator+h[t]}break}}}else{var r;if(g.tooltipAxes=="both"||g.tooltipAxes=="xy"){r=b.jqplot.sprintf(g.tooltipFormatString,j.data[0])+g.tooltipSeparator+b.jqplot.sprintf(g.tooltipFormatString,j.data[1])}else{if(g.tooltipAxes=="yx"){r=b.jqplot.sprintf(g.tooltipFormatString,j.data[1])+g.tooltipSeparator+b.jqplot.sprintf(g.tooltipFormatString,j.data[0])}else{if(g.tooltipAxes=="x"){r=b.jqplot.sprintf(g.tooltipFormatString,j.data[0])}else{if(g.tooltipAxes=="y"){r=b.jqplot.sprintf(g.tooltipFormatString,j.data[1])}}}}}v.html(r);var u={x:j.gridData[0],y:j.gridData[1]};var p=0;var f=0.707;if(m.markerRenderer.show==true){p=(m.markerRenderer.size+g.sizeAdjust)/2}switch(g.tooltipLocation){case"nw":var l=u.x+s._gridPadding.left-v.outerWidth(true)-g.tooltipOffset-f*p;var k=u.y+s._gridPadding.top-g.tooltipOffset-v.outerHeight(true)-f*p;break;case"n":var l=u.x+s._gridPadding.left-v.outerWidth(true)/2;var k=u.y+s._gridPadding.top-g.tooltipOffset-v.outerHeight(true)-p;break;case"ne":var l=u.x+s._gridPadding.left+g.tooltipOffset+f*p;var k=u.y+s._gridPadding.top-g.tooltipOffset-v.outerHeight(true)-f*p;break;case"e":var l=u.x+s._gridPadding.left+g.tooltipOffset+p;var k=u.y+s._gridPadding.top-v.outerHeight(true)/2;break;case"se":var l=u.x+s._gridPadding.left+g.tooltipOffset+f*p;var k=u.y+s._gridPadding.top+g.tooltipOffset+f*p;break;case"s":var l=u.x+s._gridPadding.left-v.outerWidth(true)/2;var k=u.y+s._gridPadding.top+g.tooltipOffset+p;break;case"sw":var l=u.x+s._gridPadding.left-v.outerWidth(true)-g.tooltipOffset-f*p;var k=u.y+s._gridPadding.top+g.tooltipOffset+f*p;break;case"w":var l=u.x+s._gridPadding.left-v.outerWidth(true)-g.tooltipOffset-p;var k=u.y+s._gridPadding.top-v.outerHeight(true)/2;break;default:var l=u.x+s._gridPadding.left-v.outerWidth(true)-g.tooltipOffset-f*p;var k=u.y+s._gridPadding.top-g.tooltipOffset-v.outerHeight(true)-f*p;break}v.css("left",l);v.css("top",k);if(g.fadeTooltip){v.fadeIn(g.tooltipFadeSpeed)}else{v.show()}}function c(h,g,k,j,i){var e=i.plugins.highlighter;if(e.show){if(j==null&&e.isHighlighting){var f=e.highlightCanvas._ctx;f.clearRect(0,0,f.canvas.width,f.canvas.height);if(e.fadeTooltip){e._tooltipElem.fadeOut(e.tooltipFadeSpeed)}else{e._tooltipElem.hide()}e.isHighlighting=false}if(j!=null&&i.series[j.seriesIndex].showHighlight&&!e.isHighlighting){e.isHighlighting=true;if(e.showMarker){a(i,j)}if(e.showTooltip){d(i,i.series[j.seriesIndex],j)}}}}})(jQuery);


// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

/**
 * Encodes a string in base64
 * @param {String} input The string to encode in base64.
 */
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

/**
 * Decodes a base64 string.
 * @param {String} input The string to decode.
 */
function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}


/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  var olda, oldb, oldc, oldd;
  for(var i = 0; i < x.length; i += 16)
  {
    olda = a;
    oldb = b;
    oldc = c;
    oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return [a, b, c, d];
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) { bkey = core_md5(bkey, key.length * chrsz); }

  var ipad = new Array(16), opad = new Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = [];
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
  {
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  }
  return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
  {
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  var triplet, j;
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16) |
              (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) |
              ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
      else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
    }
  }
  return str;
}


/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for details.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}

/*
 * Perform a simple self-test to see if the VM is working
 */
function sha1_vm_test()
{
  return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}

/*
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 */
function core_sha1(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << (24 - len % 32);
  x[((len + 64 >> 9) << 4) + 15] = len;

  var w = new Array(80);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
  var e = -1009589776;

  var i, j, t, olda, oldb, oldc, oldd, olde;
  for (i = 0; i < x.length; i += 16)
  {
    olda = a;
    oldb = b;
    oldc = c;
    oldd = d;
    olde = e;

    for (j = 0; j < 80; j++)
    {
      if (j < 16) { w[j] = x[i + j]; }
      else { w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); }
      t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
                       safe_add(safe_add(e, w[j]), sha1_kt(j)));
      e = d;
      d = c;
      c = rol(b, 30);
      b = a;
      a = t;
    }

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
    e = safe_add(e, olde);
  }
  return [a, b, c, d, e];
}

/*
 * Perform the appropriate triplet combination function for the current
 * iteration
 */
function sha1_ft(t, b, c, d)
{
  if (t < 20) { return (b & c) | ((~b) & d); }
  if (t < 40) { return b ^ c ^ d; }
  if (t < 60) { return (b & c) | (b & d) | (c & d); }
  return b ^ c ^ d;
}

/*
 * Determine the appropriate additive constant for the current iteration
 */
function sha1_kt(t)
{
  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
         (t < 60) ? -1894007588 : -899497514;
}

/*
 * Calculate the HMAC-SHA1 of a key and some data
 */
function core_hmac_sha1(key, data)
{
  var bkey = str2binb(key);
  if (bkey.length > 16) { bkey = core_sha1(bkey, key.length * chrsz); }

  var ipad = new Array(16), opad = new Array(16);
  for (var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  return core_sha1(opad.concat(hash), 512 + 160);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert an 8-bit or 16-bit string to an array of big-endian words
 * In 8-bit function, characters >255 have their hi-byte silently ignored.
 */
function str2binb(str)
{
  var bin = [];
  var mask = (1 << chrsz) - 1;
  for (var i = 0; i < str.length * chrsz; i += chrsz)
  {
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
  }
  return bin;
}

/*
 * Convert an array of big-endian words to a string
 */
function binb2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for (var i = 0; i < bin.length * 32; i += chrsz)
  {
    str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
  }
  return str;
}

/*
 * Convert an array of big-endian words to a hex string.
 */
function binb2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for (var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of big-endian words to a base-64 string
 */
function binb2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  var triplet, j;
  for (var i = 0; i < binarray.length * 4; i += 3)
  {
    triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16) |
              (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) |
               ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
    for (j = 0; j < 4; j++)
    {
      if (i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
      else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
    }
  }
  return str;
}


/*
    This program is distributed under the terms of the MIT license.
    Please see the LICENSE file for details.

    Copyright 2006-2008, OGG, LLC
*/

/** File: strophe.js
 *  A JavaScript library for XMPP BOSH.
 *
 *  This is the JavaScript version of the Strophe library.  Since JavaScript
 *  has no facilities for persistent TCP connections, this library uses
 *  Bidirectional-streams Over Synchronous HTTP (BOSH) to emulate
 *  a persistent, stateful, two-way connection to an XMPP server.  More
 *  information on BOSH can be found in XEP 124.
 */

/** PrivateFunction: Function.prototype.bind
 *  Bind a function to an instance.
 *
 *  This Function object extension method creates a bound method similar
 *  to those in Python.  This means that the 'this' object will point
 *  to the instance you want.  See
 *  <a href='http://benjamin.smedbergs.us/blog/2007-01-03/bound-functions-and-function-imports-in-javascript/'>Bound Functions and Function Imports in JavaScript</a>
 *  for a complete explanation.
 *
 *  This extension already exists in some browsers (namely, Firefox 3), but
 *  we provide it to support those that don't.
 *
 *  Parameters:
 *    (Object) obj - The object that will become 'this' in the bound function.
 *
 *  Returns:
 *    The bound function.
 */
if (!Function.prototype.bind) {
    Function.prototype.bind = function (obj)
    {
        var func = this;
        return function () { return func.apply(obj, arguments); };
    };
}

/** PrivateFunction: Function.prototype.prependArg
 *  Prepend an argument to a function.
 *
 *  This Function object extension method returns a Function that will
 *  invoke the original function with an argument prepended.  This is useful
 *  when some object has a callback that needs to get that same object as
 *  an argument.  The following fragment illustrates a simple case of this
 *  > var obj = new Foo(this.someMethod);</code></blockquote>
 *
 *  Foo's constructor can now use func.prependArg(this) to ensure the
 *  passed in callback function gets the instance of Foo as an argument.
 *  Doing this without prependArg would mean not setting the callback
 *  from the constructor.
 *
 *  This is used inside Strophe for passing the Strophe.Request object to
 *  the onreadystatechange handler of XMLHttpRequests.
 *
 *  Parameters:
 *    arg - The argument to pass as the first parameter to the function.
 *
 *  Returns:
 *    A new Function which calls the original with the prepended argument.
 */
if (!Function.prototype.prependArg) {
    Function.prototype.prependArg = function (arg)
    {
        var func = this;

        return function () {
            var newargs = [arg];
            for (var i = 0; i < arguments.length; i++)
                newargs.push(arguments[i]);
            return func.apply(this, newargs);
        };
    };
}

/** PrivateFunction: Array.prototype.indexOf
 *  Return the index of an object in an array.
 *
 *  This function is not supplied by some JavaScript implementations, so
 *  we provide it if it is missing.  This code is from:
 *  http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
 *
 *  Parameters:
 *    (Object) elt - The object to look for.
 *    (Integer) from - The index from which to start looking. (optional).
 *
 *  Returns:
 *    The index of elt in the array or -1 if not found.
 */
if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(elt /*, from*/)
    {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this && this[from] === elt)
                return from;
        }

        return -1;
    };
}

/* All of the Strophe globals are defined in this special function below so
 * that references to the globals become closures.  This will ensure that
 * on page reload, these references will still be available to callbacks
 * that are still executing.
 */
 
(function (callback) {
var Strophe;

/** Function: $build
 *  Create a Strophe.Builder.
 *  This is an alias for 'new Strophe.Builder(name, attrs)'.
 *
 *  Parameters:
 *    (String) name - The root element name.
 *    (Object) attrs - The attributes for the root element in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $build(name, attrs) { return new Strophe.Builder(name, attrs); }
/** Function: $msg
 *  Create a Strophe.Builder with a <message/> element as the root.
 *
 *  Parmaeters:
 *    (Object) attrs - The <message/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $msg(attrs) { return new Strophe.Builder("message", attrs); }
/** Function: $iq
 *  Create a Strophe.Builder with an <iq/> element as the root.
 *
 *  Parameters:
 *    (Object) attrs - The <iq/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $iq(attrs) { return new Strophe.Builder("iq", attrs); }
/** Function: $pres
 *  Create a Strophe.Builder with a <presence/> element as the root.
 *
 *  Parameters:
 *    (Object) attrs - The <presence/> element attributes in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder object.
 */
function $pres(attrs) { return new Strophe.Builder("presence", attrs); }

/** Class: Strophe
 *  An object container for all Strophe library functions.
 *
 *  This class is just a container for all the objects and constants
 *  used in the library.  It is not meant to be instantiated, but to
 *  provide a namespace for library objects, constants, and functions.
 */
Strophe = {
    /** Constants: XMPP Namespace Constants
     *  Common namespace constants from the XMPP RFCs and XEPs.
     *
     *  NS.HTTPBIND - HTTP BIND namespace from XEP 124.
     *  NS.BOSH - BOSH namespace from XEP 206.
     *  NS.CLIENT - Main XMPP client namespace.
     *  NS.AUTH - Legacy authentication namespace.
     *  NS.ROSTER - Roster operations namespace.
     *  NS.PROFILE - Profile namespace.
     *  NS.DISCO_INFO - Service discovery info namespace from XEP 30.
     *  NS.DISCO_ITEMS - Service discovery items namespace from XEP 30.
     *  NS.MUC - Multi-User Chat namespace from XEP 45.
     *  NS.SASL - XMPP SASL namespace from RFC 3920.
     *  NS.STREAM - XMPP Streams namespace from RFC 3920.
     *  NS.BIND - XMPP Binding namespace from RFC 3920.
     *  NS.SESSION - XMPP Session namespace from RFC 3920.
     */
    NS: {
        HTTPBIND: "http://jabber.org/protocol/httpbind",
        BOSH: "urn:xmpp:xbosh",
        CLIENT: "jabber:client",
        AUTH: "jabber:iq:auth",
        ROSTER: "jabber:iq:roster",
        PROFILE: "jabber:iq:profile",
        DISCO_INFO: "http://jabber.org/protocol/disco#info",
        DISCO_ITEMS: "http://jabber.org/protocol/disco#items",
        MUC: "http://jabber.org/protocol/muc",
        SASL: "urn:ietf:params:xml:ns:xmpp-sasl",
        STREAM: "http://etherx.jabber.org/streams",
        BIND: "urn:ietf:params:xml:ns:xmpp-bind",
        SESSION: "urn:ietf:params:xml:ns:xmpp-session",
        VERSION: "jabber:iq:version",
        STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas"
    },

    /** Function: addNamespace 
     *  This function is used to extend the current namespaces in
     *	Strophe.NS.  It takes a key and a value with the key being the
     *	name of the new namespace, with its actual value.
     *	For example:
     *	Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub");
     *
     *  Parameters:
     *    (String) name - The name under which the namespace will be
     *      referenced under Strophe.NS
     *    (String) value - The actual namespace.	
     */
    addNamespace: function (name, value)
    {
	Strophe.NS[name] = value;
    },

    /** Constants: Connection Status Constants
     *  Connection status constants for use by the connection handler
     *  callback.
     *
     *  Status.ERROR - An error has occurred
     *  Status.CONNECTING - The connection is currently being made
     *  Status.CONNFAIL - The connection attempt failed
     *  Status.AUTHENTICATING - The connection is authenticating
     *  Status.AUTHFAIL - The authentication attempt failed
     *  Status.CONNECTED - The connection has succeeded
     *  Status.DISCONNECTED - The connection has been terminated
     *  Status.DISCONNECTING - The connection is currently being terminated
     */
    Status: {
        ERROR: 0,
        CONNECTING: 1,
        CONNFAIL: 2,
        AUTHENTICATING: 3,
        AUTHFAIL: 4,
        CONNECTED: 5,
        DISCONNECTED: 6,
        DISCONNECTING: 7
    },

    /** Constants: Log Level Constants
     *  Logging level indicators.
     *
     *  LogLevel.DEBUG - Debug output
     *  LogLevel.INFO - Informational output
     *  LogLevel.WARN - Warnings
     *  LogLevel.ERROR - Errors
     *  LogLevel.FATAL - Fatal errors
     */
    LogLevel: {
        DEBUG: 0,
        INFO: 1,
        WARN: 2,
        ERROR: 3,
        FATAL: 4
    },

    /** PrivateConstants: DOM Element Type Constants
     *  DOM element types.
     *
     *  ElementType.NORMAL - Normal element.
     *  ElementType.TEXT - Text data element.
     */
    ElementType: {
        NORMAL: 1,
        TEXT: 3
    },

    /** PrivateConstants: Timeout Values
     *  Timeout values for error states.  These values are in seconds.
     *  These should not be changed unless you know exactly what you are
     *  doing.
     *
     *  TIMEOUT - Time to wait for a request to return.  This defaults to
     *      70 seconds.
     *  SECONDARY_TIMEOUT - Time to wait for immediate request return. This
     *      defaults to 7 seconds.
     */
    TIMEOUT: 70,
    SECONDARY_TIMEOUT: 7,

    /** Function: forEachChild
     *  Map a function over some or all child elements of a given element.
     *
     *  This is a small convenience function for mapping a function over
     *  some or all of the children of an element.  If elemName is null, all
     *  children will be passed to the function, otherwise only children
     *  whose tag names match elemName will be passed.
     *
     *  Parameters:
     *    (XMLElement) elem - The element to operate on.
     *    (String) elemName - The child element tag name filter.
     *    (Function) func - The function to apply to each child.  This
     *      function should take a single argument, a DOM element.
     */
    forEachChild: function (elem, elemName, func)
    {
        var i, childNode;

        for (i = 0; i < elem.childNodes.length; i++) {
            childNode = elem.childNodes[i];
            if (childNode.nodeType == Strophe.ElementType.NORMAL &&
                (!elemName || this.isTagEqual(childNode, elemName))) {
                func(childNode);
            }
        }
    },

    /** Function: isTagEqual
     *  Compare an element's tag name with a string.
     *
     *  This function is case insensitive.
     *
     *  Parameters:
     *    (XMLElement) el - A DOM element.
     *    (String) name - The element name.
     *
     *  Returns:
     *    true if the element's tag name matches _el_, and false
     *    otherwise.
     */
    isTagEqual: function (el, name)
    {
        return el.tagName.toLowerCase() == name.toLowerCase();
    },

    /** PrivateVariable: _xmlGenerator
     *  _Private_ variable that caches a DOM document to
     *  generate elements.
     */
    _xmlGenerator: null,

    /** PrivateFunction: _makeGenerator
     *  _Private_ function that creates a dummy XML DOM document to serve as
     *  an element and text node generator.
     */
    _makeGenerator: function () {
        var doc;

        if (window.ActiveXObject) {
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.appendChild(doc.createElement('strophe'));
        } else {
            doc = document.implementation
                .createDocument('jabber:client', 'strophe', null);
        }

        return doc;
    },

    /** Function: xmlElement
     *  Create an XML DOM element.
     *
     *  This function creates an XML DOM element correctly across all
     *  implementations. Specifically the Microsoft implementation of
     *  document.createElement makes DOM elements with 43+ default attributes
     *  unless elements are created with the ActiveX object Microsoft.XMLDOM.
     *
     *  Most DOMs force element names to lowercase, so we use the
     *  _realname attribute on the created element to store the case
     *  sensitive name.  This is required to generate proper XML for
     *  things like vCard avatars (XEP 153).  This attribute is stripped
     *  out before being sent over the wire or serialized, but you may
     *  notice it during debugging.
     *
     *  Parameters:
     *    (String) name - The name for the element.
     *    (Array) attrs - An optional array of key/value pairs to use as
     *      element attributes in the following format [['key1', 'value1'],
     *      ['key2', 'value2']]
     *    (String) text - The text child data for the element.
     *
     *  Returns:
     *    A new XML DOM element.
     */
    xmlElement: function (name)
    {
        if (!name) { return null; }

        var node = null;
        if (!Strophe._xmlGenerator) {
            Strophe._xmlGenerator = Strophe._makeGenerator();
        }
        node = Strophe._xmlGenerator.createElement(name);

        // FIXME: this should throw errors if args are the wrong type or
        // there are more than two optional args
        var a, i, k;
        for (a = 1; a < arguments.length; a++) {
            if (!arguments[a]) { continue; }
            if (typeof(arguments[a]) == "string" ||
                typeof(arguments[a]) == "number") {
                node.appendChild(Strophe.xmlTextNode(arguments[a]));
            } else if (typeof(arguments[a]) == "object" &&
                       typeof(arguments[a]['sort']) == "function") {
                for (i = 0; i < arguments[a].length; i++) {
                    if (typeof(arguments[a][i]) == "object" &&
                        typeof(arguments[a][i]['sort']) == "function") {
                        node.setAttribute(arguments[a][i][0],
                                          arguments[a][i][1]);
                    }
                }
            } else if (typeof(arguments[a]) == "object") {
                for (k in arguments[a]) {
                    if (arguments[a].hasOwnProperty(k)) {
                        node.setAttribute(k, arguments[a][k]);
                    }
                } 
            }
        }

        return node;
    },

    /*  Function: xmlescape
     *  Excapes invalid xml characters.
     *
     *  Parameters:
     *     (String) text - text to escape.
     *
     *	Returns:
     *      Escaped text.
     */
    xmlescape: function(text) 
    {
	text = text.replace(/\&/g, "&amp;");
        text = text.replace(/</g,  "&lt;");
        text = text.replace(/>/g,  "&gt;");
        return text;    
    },

    /** Function: xmlTextNode
     *  Creates an XML DOM text node.
     *
     *  Provides a cross implementation version of document.createTextNode.
     *
     *  Parameters:
     *    (String) text - The content of the text node.
     *
     *  Returns:
     *    A new XML DOM text node.
     */
    xmlTextNode: function (text)
    {
	//ensure text is escaped
	text = Strophe.xmlescape(text);

        if (!Strophe._xmlGenerator) {
            Strophe._xmlGenerator = Strophe._makeGenerator();
        }
        return Strophe._xmlGenerator.createTextNode(text);
    },

    /** Function: getText
     *  Get the concatenation of all text children of an element.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    A String with the concatenated text of all text element children.
     */
    getText: function (elem)
    {
        if (!elem) return null;

        var str = "";
        if (elem.childNodes.length === 0 && elem.nodeType ==
            Strophe.ElementType.TEXT) {
            str += elem.nodeValue;
        }

        for (var i = 0; i < elem.childNodes.length; i++) {
            if (elem.childNodes[i].nodeType == Strophe.ElementType.TEXT) {
                str += elem.childNodes[i].nodeValue;
            }
        }

        return str;
    },

    /** Function: copyElement
     *  Copy an XML DOM element.
     *
     *  This function copies a DOM element and all its descendants and returns
     *  the new copy.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    A new, copied DOM element tree.
     */
    copyElement: function (elem)
    {
        var i, el;
        if (elem.nodeType == Strophe.ElementType.NORMAL) {
            el = Strophe.xmlElement(elem.tagName);

            for (i = 0; i < elem.attributes.length; i++) {
                el.setAttribute(elem.attributes[i].nodeName.toLowerCase(),
                                elem.attributes[i].value);
            }

            for (i = 0; i < elem.childNodes.length; i++) {
                el.appendChild(Strophe.copyElement(elem.childNodes[i]));
            }
        } else if (elem.nodeType == Strophe.ElementType.TEXT) {
            el = Strophe.xmlTextNode(elem.nodeValue);
        }

        return el;
    },

    /** Function: escapeJid
     *  Escape a JID.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    An escaped JID String.
     */
    escapeJid: function (jid)
    {
        var user = jid.split("@");
        if (user.length == 1)
            // no user so nothing to escape
            return jid;

        var host = user.splice(user.length - 1, 1)[0];
        user = user.join("@")
            .replace(/^\s+|\s+$/g, '')
            .replace(/\\/g,  "\\5c")
            .replace(/ /g,   "\\20")
            .replace(/\"/g,  "\\22")
            .replace(/\&/g,  "\\26")
            .replace(/\'/g,  "\\27")
            .replace(/\//g,  "\\2f")
            .replace(/:/g,   "\\3a")
            .replace(/</g,   "\\3c")
            .replace(/>/g,   "\\3e")
            .replace(/@/g,   "\\40");

        return [user, host].join("@");
    },

    /** Function: unescapeJid
     *  Unescape a JID.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    An unescaped JID String.
     */
    unescapeJid: function (jid)
    {
        return jid.replace(/\\20/g, " ")
            .replace(/\\22/g, '"')
            .replace(/\\26/g, "&")
            .replace(/\\27/g, "'")
            .replace(/\\2f/g, "/")
            .replace(/\\3a/g, ":")
            .replace(/\\3c/g, "<")
            .replace(/\\3e/g, ">")
            .replace(/\\40/g, "@")
            .replace(/\\5c/g, "\\");
    },

    /** Function: getNodeFromJid
     *  Get the node portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the node.
     */
    getNodeFromJid: function (jid)
    {
        if (jid.indexOf("@") < 0)
            return null;
        return Strophe.escapeJid(jid).split("@")[0];
    },

    /** Function: getDomainFromJid
     *  Get the domain portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the domain.
     */
    getDomainFromJid: function (jid)
    {
        var bare = Strophe.escapeJid(Strophe.getBareJidFromJid(jid));
        if (bare.indexOf("@") < 0)
            return bare;
        else
            return bare.split("@")[1];
    },

    /** Function: getResourceFromJid
     *  Get the resource portion of a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the resource.
     */
    getResourceFromJid: function (jid)
    {
        var s = Strophe.escapeJid(jid).split("/");
        if (s.length < 2) return null;
        return s[1];
    },

    /** Function: getBareJidFromJid
     *  Get the bare JID from a JID String.
     *
     *  Parameters:
     *    (String) jid - A JID.
     *
     *  Returns:
     *    A String containing the bare JID.
     */
    getBareJidFromJid: function (jid)
    {
        return this.escapeJid(jid).split("/")[0];
    },

    /** Function: log
     *  User overrideable logging function.
     *
     *  This function is called whenever the Strophe library calls any
     *  of the logging functions.  The default implementation of this
     *  function does nothing.  If client code wishes to handle the logging
     *  messages, it should override this with
     *  > Strophe.log = function (level, msg) {
     *  >   (user code here)
     *  > };
     *
     *  Please note that data sent and received over the wire is logged
     *  via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput().
     *
     *  The different levels and their meanings are
     *
     *    DEBUG - Messages useful for debugging purposes.
     *    INFO - Informational messages.  This is mostly information like
     *      'disconnect was called' or 'SASL auth succeeded'.
     *    WARN - Warnings about potential problems.  This is mostly used
     *      to report transient connection errors like request timeouts.
     *    ERROR - Some error occurred.
     *    FATAL - A non-recoverable fatal error occurred.
     *
     *  Parameters:
     *    (Integer) level - The log level of the log message.  This will
     *      be one of the values in Strophe.LogLevel.
     *    (String) msg - The log message.
     */
    log: function (level, msg)
    {
        return;
    },

    /** Function: debug
     *  Log a message at the Strophe.LogLevel.DEBUG level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    debug: function(msg)
    {
        this.log(this.LogLevel.DEBUG, msg);
    },

    /** Function: info
     *  Log a message at the Strophe.LogLevel.INFO level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    info: function (msg)
    {
        this.log(this.LogLevel.INFO, msg);
    },

    /** Function: warn
     *  Log a message at the Strophe.LogLevel.WARN level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    warn: function (msg)
    {
        this.log(this.LogLevel.WARN, msg);
    },

    /** Function: error
     *  Log a message at the Strophe.LogLevel.ERROR level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    error: function (msg)
    {
        this.log(this.LogLevel.ERROR, msg);
    },

    /** Function: fatal
     *  Log a message at the Strophe.LogLevel.FATAL level.
     *
     *  Parameters:
     *    (String) msg - The log message.
     */
    fatal: function (msg)
    {
        this.log(this.LogLevel.FATAL, msg);
    },

    /** Function: serialize
     *  Render a DOM element and all descendants to a String.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    The serialized element tree as a String.
     */
    serialize: function (elem)
    {
        var result;

        if (!elem) return null;

        if (typeof(elem["tree"]) === "function") {
            elem = elem.tree();
        }

        var nodeName = elem.nodeName;
        var i, child;

        if (elem.getAttribute("_realname")) {
            nodeName = elem.getAttribute("_realname");
        }

        result = "<" + nodeName;
        for (i = 0; i < elem.attributes.length; i++) {
               if(elem.attributes[i].nodeName != "_realname") {
                 result += " " + elem.attributes[i].nodeName.toLowerCase() +
                "='" + elem.attributes[i].value
                    .replace("'", "&#39;").replace("&", "&#x26;") + "'";
               }
        }

        if (elem.childNodes.length > 0) {
            result += ">";
            for (i = 0; i < elem.childNodes.length; i++) {
                child = elem.childNodes[i];
                if (child.nodeType == Strophe.ElementType.NORMAL) {
                    // normal element, so recurse
                    result += Strophe.serialize(child);
                } else if (child.nodeType == Strophe.ElementType.TEXT) {
                    // text element
                    result += child.nodeValue;
                }
            }
            result += "</" + nodeName + ">";
        } else {
            result += "/>";
        }

        return result;
    },

    /** PrivateVariable: _requestId
     *  _Private_ variable that keeps track of the request ids for
     *  connections.
     */
    _requestId: 0,

    /** PrivateVariable: Strophe.connectionPlugins
     *  _Private_ variable Used to store plugin names that need
     *  initialization on Strophe.Connection construction.
     */
    _connectionPlugins: {},

    /** Function: addConnectionPlugin
     *  Extends the Strophe.Connection object with the given plugin.
     *
     *  Paramaters:
     *    (String) name - The name of the extension.
     *    (Object) ptype - The plugin's prototype.
     */
    addConnectionPlugin: function (name, ptype)
    {
        Strophe._connectionPlugins[name] = ptype;
    }
};

/** Class: Strophe.Builder
 *  XML DOM builder.
 *
 *  This object provides an interface similar to JQuery but for building
 *  DOM element easily and rapidly.  All the functions except for toString()
 *  and tree() return the object, so calls can be chained.  Here's an
 *  example using the $iq() builder helper.
 *  > $iq({to: 'you': from: 'me': type: 'get', id: '1'})
 *  >     .c('query', {xmlns: 'strophe:example'})
 *  >     .c('example')
 *  >     .toString()
 *  The above generates this XML fragment
 *  > <iq to='you' from='me' type='get' id='1'>
 *  >   <query xmlns='strophe:example'>
 *  >     <example/>
 *  >   </query>
 *  > </iq>
 *  The corresponding DOM manipulations to get a similar fragment would be
 *  a lot more tedious and probably involve several helper variables.
 *
 *  Since adding children makes new operations operate on the child, up()
 *  is provided to traverse up the tree.  To add two children, do
 *  > builder.c('child1', ...).up().c('child2', ...)
 *  The next operation on the Builder will be relative to the second child.
 */

/** Constructor: Strophe.Builder
 *  Create a Strophe.Builder object.
 *
 *  The attributes should be passed in object notation.  For example
 *  > var b = new Builder('message', {to: 'you', from: 'me'});
 *  or
 *  > var b = new Builder('messsage', {'xml:lang': 'en'});
 *
 *  Parameters:
 *    (String) name - The name of the root element.
 *    (Object) attrs - The attributes for the root element in object notation.
 *
 *  Returns:
 *    A new Strophe.Builder.
 */
Strophe.Builder = function (name, attrs)
{
    // Set correct namespace for jabber:client elements
    if (name == "presence" || name == "message" || name == "iq") {
        if (attrs && !attrs.xmlns)
            attrs.xmlns = Strophe.NS.CLIENT;
        else if (!attrs)
            attrs = {xmlns: Strophe.NS.CLIENT};
    }

    // Holds the tree being built.
    this.nodeTree = Strophe.xmlElement(name, attrs);

    // Points to the current operation node.
    this.node = this.nodeTree;
};

Strophe.Builder.prototype = {
    /** Function: tree
     *  Return the DOM tree.
     *
     *  This function returns the current DOM tree as an element object.  This
     *  is suitable for passing to functions like Strophe.Connection.send().
     *
     *  Returns:
     *    The DOM tree as a element object.
     */
    tree: function ()
    {
        return this.nodeTree;
    },

    /** Function: toString
     *  Serialize the DOM tree to a String.
     *
     *  This function returns a string serialization of the current DOM
     *  tree.  It is often used internally to pass data to a
     *  Strophe.Request object.
     *
     *  Returns:
     *    The serialized DOM tree in a String.
     */
    toString: function ()
    {
        return Strophe.serialize(this.nodeTree);
    },

    /** Function: up
     *  Make the current parent element the new current element.
     *
     *  This function is often used after c() to traverse back up the tree.
     *  For example, to add two children to the same element
     *  > builder.c('child1', {}).up().c('child2', {});
     *
     *  Returns:
     *    The Stophe.Builder object.
     */
    up: function ()
    {
        this.node = this.node.parentNode;
        return this;
    },

    /** Function: attrs
     *  Add or modify attributes of the current element.
     *
     *  The attributes should be passed in object notation.  This function
     *  does not move the current element pointer.
     *
     *  Parameters:
     *    (Object) moreattrs - The attributes to add/modify in object notation.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    attrs: function (moreattrs)
    {
        for (var k in moreattrs)
            this.node.setAttribute(k, moreattrs[k]);
        return this;
    },

    /** Function: c
     *  Add a child to the current element and make it the new current
     *  element.
     *
     *  This function moves the current element pointer to the child.  If you
     *  need to add another child, it is necessary to use up() to go back
     *  to the parent in the tree.
     *
     *  Parameters:
     *    (String) name - The name of the child.
     *    (Object) attrs - The attributes of the child in object notation.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    c: function (name, attrs)
    {
        var child = Strophe.xmlElement(name, attrs);
        this.node.appendChild(child);
        this.node = child;
        return this;
    },

    /** Function: cnode
     *  Add a child to the current element and make it the new current
     *  element.
     *
     *  This function is the same as c() except that instead of using a
     *  name and an attributes object to create the child it uses an
     *  existing DOM element object.
     *
     *  Parameters:
     *    (XMLElement) elem - A DOM element.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    cnode: function (elem)
    {
        this.node.appendChild(elem);
        this.node = elem;
        return this;
    },

    /** Function: t
     *  Add a child text element.
     *
     *  This *does not* make the child the new current element since there
     *  are no children of text elements.
     *
     *  Parameters:
     *    (String) text - The text data to append to the current element.
     *
     *  Returns:
     *    The Strophe.Builder object.
     */
    t: function (text)
    {
        var child = Strophe.xmlTextNode(text);
        this.node.appendChild(child);
        return this;
    }
};


/** PrivateClass: Strophe.Handler
 *  _Private_ helper class for managing stanza handlers.
 *
 *  A Strophe.Handler encapsulates a user provided callback function to be
 *  executed when matching stanzas are received by the connection.
 *  Handlers can be either one-off or persistant depending on their
 *  return value. Returning true will cause a Handler to remain active, and
 *  returning false will remove the Handler.
 *
 *  Users will not use Strophe.Handler objects directly, but instead they
 *  will use Strophe.Connection.addHandler() and
 *  Strophe.Connection.deleteHandler().
 */

/** PrivateConstructor: Strophe.Handler
 *  Create and initialize a new Strophe.Handler.
 *
 *  Parameters:
 *    (Function) handler - A function to be executed when the handler is run.
 *    (String) ns - The namespace to match.
 *    (String) name - The element name to match.
 *    (String) type - The element type to match.
 *    (String) id - The element id attribute to match.
 *    (String) from - The element from attribute to match.
 *
 *  Returns:
 *    A new Strophe.Handler object.
 */
Strophe.Handler = function (handler, ns, name, type, id, from)
{
    this.handler = handler;
    this.ns = ns;
    this.name = name;
    this.type = type;
    this.id = id;
    this.from = from;

    // whether the handler is a user handler or a system handler
    this.user = true;
};

Strophe.Handler.prototype = {
    /** PrivateFunction: isMatch
     *  Tests if a stanza matches the Strophe.Handler.
     *
     *  Parameters:
     *    (XMLElement) elem - The XML element to test.
     *
     *  Returns:
     *    true if the stanza matches and false otherwise.
     */
    isMatch: function (elem)
    {
        var nsMatch, i;

        nsMatch = false;
        if (!this.ns) {
            nsMatch = true;
        } else {
            var self = this;
            Strophe.forEachChild(elem, null, function (elem) {
                if (elem.getAttribute("xmlns") == self.ns)
                    nsMatch = true;
            });

            nsMatch = nsMatch || elem.getAttribute("xmlns") == this.ns;
        }

        if (nsMatch &&
            (!this.name || Strophe.isTagEqual(elem, this.name)) &&
            (!this.type || elem.getAttribute("type") == this.type) &&
            (!this.id || elem.getAttribute("id") == this.id) &&
            (!this.from || elem.getAttribute("from") == this.from)) {
                return true;
        }

        return false;
    },

    /** PrivateFunction: run
     *  Run the callback on a matching stanza.
     *
     *  Parameters:
     *    (XMLElement) elem - The DOM element that triggered the
     *      Strophe.Handler.
     *
     *  Returns:
     *    A boolean indicating if the handler should remain active.
     */
    run: function (elem)
    {
        var result = null;
        try {
            result = this.handler(elem);
        } catch (e) {
            if (e.sourceURL) {
                Strophe.fatal("error: " + this.handler +
                              " " + e.sourceURL + ":" +
                              e.line + " - " + e.name + ": " + e.message);
            } else if (e.fileName) {
                if (typeof(console) != "undefined") {
                    console.trace();
                    console.error(this.handler, " - error - ", e, e.message);
                }
                Strophe.fatal("error: " + this.handler + " " +
                              e.fileName + ":" + e.lineNumber + " - " +
                              e.name + ": " + e.message);
            } else {
                Strophe.fatal("error: " + this.handler);
            }

            throw e;
        }

        return result;
    },

    /** PrivateFunction: toString
     *  Get a String representation of the Strophe.Handler object.
     *
     *  Returns:
     *    A String.
     */
    toString: function ()
    {
        return "{Handler: " + this.handler + "(" + this.name + "," +
            this.id + "," + this.ns + ")}";
    }
};

/** PrivateClass: Strophe.TimedHandler
 *  _Private_ helper class for managing timed handlers.
 *
 *  A Strophe.TimedHandler encapsulates a user provided callback that
 *  should be called after a certain period of time or at regular
 *  intervals.  The return value of the callback determines whether the
 *  Strophe.TimedHandler will continue to fire.
 *
 *  Users will not use Strophe.TimedHandler objects directly, but instead
 *  they will use Strophe.Connection.addTimedHandler() and
 *  Strophe.Connection.deleteTimedHandler().
 */

/** PrivateConstructor: Strophe.TimedHandler
 *  Create and initialize a new Strophe.TimedHandler object.
 *
 *  Parameters:
 *    (Integer) period - The number of milliseconds to wait before the
 *      handler is called.
 *    (Function) handler - The callback to run when the handler fires.  This
 *      function should take no arguments.
 *
 *  Returns:
 *    A new Strophe.TimedHandler object.
 */
Strophe.TimedHandler = function (period, handler)
{
    this.period = period;
    this.handler = handler;

    this.lastCalled = new Date().getTime();
    this.user = true;
};

Strophe.TimedHandler.prototype = {
    /** PrivateFunction: run
     *  Run the callback for the Strophe.TimedHandler.
     *
     *  Returns:
     *    true if the Strophe.TimedHandler should be called again, and false
     *      otherwise.
     */
    run: function ()
    {
        this.lastCalled = new Date().getTime();
        return this.handler();
    },

    /** PrivateFunction: reset
     *  Reset the last called time for the Strophe.TimedHandler.
     */
    reset: function ()
    {
        this.lastCalled = new Date().getTime();
    },

    /** PrivateFunction: toString
     *  Get a string representation of the Strophe.TimedHandler object.
     *
     *  Returns:
     *    The string representation.
     */
    toString: function ()
    {
        return "{TimedHandler: " + this.handler + "(" + this.period +")}";
    }
};

/** PrivateClass: Strophe.Request
 *  _Private_ helper class that provides a cross implementation abstraction
 *  for a BOSH related XMLHttpRequest.
 *
 *  The Strophe.Request class is used internally to encapsulate BOSH request
 *  information.  It is not meant to be used from user's code.
 */

/** PrivateConstructor: Strophe.Request
 *  Create and initialize a new Strophe.Request object.
 *
 *  Parameters:
 *    (XMLElement) elem - The XML data to be sent in the request.
 *    (Function) func - The function that will be called when the
 *      XMLHttpRequest readyState changes.
 *    (Integer) rid - The BOSH rid attribute associated with this request.
 *    (Integer) sends - The number of times this same request has been
 *      sent.
 */
Strophe.Request = function (elem, func, rid, sends)
{
    this.id = ++Strophe._requestId;
    this.xmlData = elem;
    this.data = Strophe.serialize(elem);
    // save original function in case we need to make a new request
    // from this one.
    this.origFunc = func;
    this.func = func;
    this.rid = rid;
    this.date = NaN;
    this.sends = sends || 0;
    this.abort = false;
    this.dead = null;
    this.age = function () {
        if (!this.date) return 0;
        var now = new Date();
        return (now - this.date) / 1000;
    };
    this.timeDead = function () {
        if (!this.dead) return 0;
        var now = new Date();
        return (now - this.dead) / 1000;
    };
    this.xhr = this._newXHR();
};

Strophe.Request.prototype = {
    /** PrivateFunction: getResponse
     *  Get a response from the underlying XMLHttpRequest.
     *
     *  This function attempts to get a response from the request and checks
     *  for errors.
     *
     *  Throws:
     *    "parsererror" - A parser error occured.
     *
     *  Returns:
     *    The DOM element tree of the response.
     */
    getResponse: function ()
    {
        var node = null;
        if (this.xhr.responseXML && this.xhr.responseXML.documentElement) {
            node = this.xhr.responseXML.documentElement;
            if (node.tagName == "parsererror") {
                Strophe.error("invalid response received");
                Strophe.error("responseText: " + this.xhr.responseText);
                Strophe.error("responseXML: " +
                              Strophe.serialize(this.xhr.responseXML));
                throw "parsererror";
            }
        } else if (this.xhr.responseText) {
            Strophe.error("invalid response received");
            Strophe.error("responseText: " + this.xhr.responseText);
            Strophe.error("responseXML: " +
                          Strophe.serialize(this.xhr.responseXML));
        }

        return node;
    },

    /** PrivateFunction: _newXHR
     *  _Private_ helper function to create XMLHttpRequests.
     *
     *  This function creates XMLHttpRequests across all implementations.
     *
     *  Returns:
     *    A new XMLHttpRequest.
     */
    _newXHR: function ()
    {
        var xhr = null;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
            if (xhr.overrideMimeType) {
                xhr.overrideMimeType("text/xml");
            }
        } else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhr.onreadystatechange = this.func.prependArg(this);

        return xhr;
    }
};

/** Class: Strophe.Connection
 *  XMPP Connection manager.
 *
 *  Thie class is the main part of Strophe.  It manages a BOSH connection
 *  to an XMPP server and dispatches events to the user callbacks as
 *  data arrives.  It supports SASL PLAIN, SASL DIGEST-MD5, and legacy
 *  authentication.
 *
 *  After creating a Strophe.Connection object, the user will typically
 *  call connect() with a user supplied callback to handle connection level
 *  events like authentication failure, disconnection, or connection
 *  complete.
 *
 *  The user will also have several event handlers defined by using
 *  addHandler() and addTimedHandler().  These will allow the user code to
 *  respond to interesting stanzas or do something periodically with the
 *  connection.  These handlers will be active once authentication is
 *  finished.
 *
 *  To send data to the connection, use send().
 */

/** Constructor: Strophe.Connection
 *  Create and initialize a Strophe.Connection object.
 *
 *  Parameters:
 *    (String) service - The BOSH service URL.
 *
 *  Returns:
 *    A new Strophe.Connection object.
 */
Strophe.Connection = function (service)
{
    /* The path to the httpbind service. */
    this.service = service;
    /* The connected JID. */
    this.jid = "";
    /* request id for body tags */
    this.rid = Math.floor(Math.random() * 4294967295);
    /* The current session ID. */
    this.sid = null;
    this.streamId = null;

    // SASL
    this.do_session = false;
    this.do_bind = false;

    // handler lists
    this.timedHandlers = [];
    this.handlers = [];
    this.removeTimeds = [];
    this.removeHandlers = [];
    this.addTimeds = [];
    this.addHandlers = [];

    this._idleTimeout = null;
    this._disconnectTimeout = null;

    this.authenticated = false;
    this.disconnecting = false;
    this.connected = false;

    this.errors = 0;

    this.paused = false;

    // default BOSH window
    this.window = 5;

    this._data = [];
    this._requests = [];
    this._uniqueId = Math.round(Math.random() * 10000);

    this._sasl_success_handler = null;
    this._sasl_failure_handler = null;
    this._sasl_challenge_handler = null;

    // setup onIdle callback every 1/10th of a second
    this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);

    // initialize plugins
    for (var k in Strophe._connectionPlugins) {
	ptype = Strophe._connectionPlugins[k];
        var F = function () {};
        F.prototype = ptype;
        this[k] = new F();
	this[k].init(this);
    }
};

Strophe.Connection.prototype = {
    /** Function: reset
     *  Reset the connection.
     *
     *  This function should be called after a connection is disconnected
     *  before that connection is reused.
     */
    reset: function ()
    {
        this.rid = Math.floor(Math.random() * 4294967295);

        this.sid = null;
        this.streamId = null;

        // SASL
        this.do_session = false;
        this.do_bind = false;

        // handler lists
        this.timedHandlers = [];
        this.handlers = [];
        this.removeTimeds = [];
        this.removeHandlers = [];
        this.addTimeds = [];
        this.addHandlers = [];

        this.authenticated = false;
        this.disconnecting = false;
        this.connected = false;

        this.errors = 0;

        this._requests = [];
        this._uniqueId = Math.round(Math.random()*10000);
    },

    /** Function: pause
     *  Pause the request manager.
     *
     *  This will prevent Strophe from sending any more requests to the
     *  server.  This is very useful for temporarily pausing while a lot
     *  of send() calls are happening quickly.  This causes Strophe to
     *  send the data in a single request, saving many request trips.
     */
    pause: function ()
    {
        this.paused = true;
    },

    /** Function: resume
     *  Resume the request manager.
     *
     *  This resumes after pause() has been called.
     */
    resume: function ()
    {
        this.paused = false;
    },

    /** Function: getUniqueId
     *  Generate a unique ID for use in <iq/> elements.
     *
     *  All <iq/> stanzas are required to have unique id attributes.  This
     *  function makes creating these easy.  Each connection instance has
     *  a counter which starts from zero, and the value of this counter
     *  plus a colon followed by the suffix becomes the unique id. If no
     *  suffix is supplied, the counter is used as the unique id.
     *
     *  Suffixes are used to make debugging easier when reading the stream
     *  data, and their use is recommended.  The counter resets to 0 for
     *  every new connection for the same reason.  For connections to the
     *  same server that authenticate the same way, all the ids should be
     *  the same, which makes it easy to see changes.  This is useful for
     *  automated testing as well.
     *
     *  Parameters:
     *    (String) suffix - A optional suffix to append to the id.
     *
     *  Returns:
     *    A unique string to be used for the id attribute.
     */
    getUniqueId: function (suffix)
    {
        if (typeof(suffix) == "string" || typeof(suffix) == "number") {
            return ++this._uniqueId + ":" + suffix;
        } else {
            return ++this._uniqueId + "";
        }
    },

    /** Function: connect
     *  Starts the connection process.
     *
     *  As the connection process proceeds, the user supplied callback will
     *  be triggered multiple times with status updates.  The callback
     *  should take two arguments - the status code and the error condition.
     *
     *  The status code will be one of the values in the Strophe.Status
     *  constants.  The error condition will be one of the conditions
     *  defined in RFC 3920 or the condition 'strophe-parsererror'.
     *
     *  Please see XEP 124 for a more detailed explanation of the optional
     *  parameters below.
     *
     *  Parameters:
     *    (String) jid - The user's JID.  This may be a bare JID,
     *      or a full JID.  If a node is not supplied, SASL ANONYMOUS
     *      authentication will be attempted.
     *    (String) pass - The user's password.
     *    (Function) callback The connect callback function.
     *    (Integer) wait - The optional HTTPBIND wait value.  This is the
     *      time the server will wait before returning an empty result for
     *      a request.  The default setting of 60 seconds is recommended.
     *      Other settings will require tweaks to the Strophe.TIMEOUT value.
     *    (Integer) hold - The optional HTTPBIND hold value.  This is the
     *      number of connections the server will hold at one time.  This
     *      should almost always be set to 1 (the default).
     *    (Integer) wind - The optional HTTBIND window value.  This is the
     *      allowed range of request ids that are valid.  The default is 5.
     */
    connect: function (jid, pass, callback, wait, hold, wind)
    {
        this.jid = jid;
        this.pass = pass;
        this.connect_callback = callback;
        this.disconnecting = false;
        this.connected = false;
        this.authenticated = false;
        this.errors = 0;

        if (!wait) wait = 60;
        if (!hold) hold = 1;
        if (wind) this.window = wind;

        // parse jid for domain and resource
        this.domain = Strophe.getDomainFromJid(this.jid);

        // build the body tag
        var body = this._buildBody().attrs({
            to: this.domain,
            "xml:lang": "en",
            wait: wait,
            hold: hold,
            window: this.window,
            content: "text/xml; charset=utf-8",
            ver: "1.6",
            "xmpp:version": "1.0",
            "xmlns:xmpp": Strophe.NS.BOSH
        });

        this._changeConnectStatus(Strophe.Status.CONNECTING, null);

        this._requests.push(
            new Strophe.Request(body.tree(),
                                this._onRequestStateChange.bind(this)
                                    .prependArg(this._connect_cb.bind(this)),
                                body.tree().getAttribute("rid")));
        this._throttledRequestHandler();
    },

    /** Function: attach
     *  Attach to an already created and authenticated BOSH session.
     *
     *  This function is provided to allow Strophe to attach to BOSH
     *  sessions which have been created externally, perhaps by a Web
     *  application.  This is often used to support auto-login type features
     *  without putting user credentials into the page.
     *
     *  Parameters:
     *    (String) jid - The full JID that is bound by the session.
     *    (String) sid - The SID of the BOSH session.
     *    (String) rid - The current RID of the BOSH session.  This RID
     *      will be used by the next request.
     *    (Function) callback The connect callback function.
     */
    attach: function (jid, sid, rid, callback)
    {
        this.jid = jid;
        this.sid = sid;
        this.rid = rid;
        this.connect_callback = callback;

        this.domain = Strophe.getDomainFromJid(this.jid);

        this.authenticated = true;
        this.connected = true;
    },

    /** Function: xmlInput
     *  User overrideable function that receives XML data coming into the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.xmlInput = function (elem) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (XMLElement) elem - The XML data received by the connection.
     */
    xmlInput: function (elem)
    {
        return;
    },

    /** Function: xmlOutput
     *  User overrideable function that receives XML data sent to the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.xmlOutput = function (elem) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (XMLElement) elem - The XMLdata sent by the connection.
     */
    xmlOutput: function (elem)
    {
        return;
    },

    /** Function: rawInput
     *  User overrideable function that receives raw data coming into the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.rawInput = function (data) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (String) data - The data received by the connection.
     */
    rawInput: function (data)
    {
        return;
    },

    /** Function: rawOutput
     *  User overrideable function that receives raw data sent to the
     *  connection.
     *
     *  The default function does nothing.  User code can override this with
     *  > Strophe.Connection.rawOutput = function (data) {
     *  >   (user code)
     *  > };
     *
     *  Parameters:
     *    (String) data - The data sent by the connection.
     */
    rawOutput: function (data)
    {
        return;
    },

    /** Function: send
     *  Send a stanza.
     *
     *  This function is called to push data onto the send queue to
     *  go out over the wire.  Whenever a request is sent to the BOSH
     *  server, all pending data is sent and the queue is flushed.
     *
     *  Parameters:
     *    (XMLElement |
     *     [XMLElement] |
     *     Strophe.Builder) elem - The stanza to send.
     */
    send: function (elem)
    {
        if (elem === null) { return ; }
        if (typeof(elem["sort"]) === "function") {
            for (var i = 0; i < elem.length; i++) {
                this._queueData(elem[i]);
            }
        } else if (typeof(elem["tree"]) === "function") {
            this._queueData(elem.tree());
        } else {
            this._queueData(elem);
        }

        this._throttledRequestHandler();
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    },

    /** Function: sendIQ
     *  Helper function to send IQ stanzas.
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza to send.
     *    (Function) callback - The callback function for a successful request.
     *    (Function) errback - The callback function for a failed or timed 
     *      out request.  On timeout, the stanza will be null.
     *    (Integer) timeout - The time specified in milliseconds for a 
     *      timeout to occur.
     *
     *  Returns:
     *    The id used to send the IQ.
    */
    sendIQ: function(elem, callback, errback, timeout) {
        var timeoutHandler = null, handler = null;
        var that = this;

        if (typeof(elem["tree"]) === "function") {
            elem = elem.tree();
        }
	var id = elem.getAttribute('id');

	// inject id if not found
	if (!id) {
	    id = this.getUniqueId("sendIQ");
	    elem.setAttribute("id", id);
	}

	var handler = this.addHandler(function (stanza) {
	    // remove timeout handler if there is one
            if (timeoutHandler) {
                that.deleteTimedHandler(timeoutHandler);
            }

            var iqtype = stanza.getAttribute('type');
	    if (iqtype === 'result') {
		if (callback) {
                    callback(stanza);
                }
	    } else if (iqtype === 'error') {
		if (errback) {
                    errback(stanza);
                }
	    } else {
                throw {
                    name: "StropheError",
                    message: "Got bad IQ type of " + iqtype
                };
            }
	}, null, 'iq', null, id);

	// if timeout specified, setup timeout handler.
	if (timeout) {
	    timeoutHandler = this.addTimedHandler(timeout, function () {
                // get rid of normal handler
                that.deleteHandler(handler);

	        // call errback on timeout with null stanza
                if (errback) {
		    errback(null);
                }
		return false;
	    });
	}

	this.send(elem);

	return id;
    },

    /** PrivateFunction: _queueData
     *  Queue outgoing data for later sending.  Also ensures that the data
     *  is a DOMElement.
     */
    _queueData: function (element) {
        if (element === null ||
            !element["tagName"] ||
            !element["childNodes"]) {
            throw {
                name: "StropheError",
                message: "Cannot queue non-DOMElement."
            };
        }
        
        this._data.push(element);
    },

    /** PrivateFunction: _sendRestart
     *  Send an xmpp:restart stanza.
     */
    _sendRestart: function ()
    {
        this._data.push("restart");

        this._throttledRequestHandler();
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    },

    /** Function: addTimedHandler
     *  Add a timed handler to the connection.
     *
     *  This function adds a timed handler.  The provided handler will
     *  be called every period milliseconds until it returns false,
     *  the connection is terminated, or the handler is removed.  Handlers
     *  that wish to continue being invoked should return true.
     *
     *  Because of method binding it is necessary to save the result of
     *  this function if you wish to remove a handler with
     *  deleteTimedHandler().
     *
     *  Note that user handlers are not active until authentication is
     *  successful.
     *
     *  Parameters:
     *    (Integer) period - The period of the handler.
     *    (Function) handler - The callback function.
     *
     *  Returns:
     *    A reference to the handler that can be used to remove it.
     */
    addTimedHandler: function (period, handler)
    {
        var thand = new Strophe.TimedHandler(period, handler);
        this.addTimeds.push(thand);
        return thand;
    },

    /** Function: deleteTimedHandler
     *  Delete a timed handler for a connection.
     *
     *  This function removes a timed handler from the connection.  The
     *  handRef parameter is *not* the function passed to addTimedHandler(),
     *  but is the reference returned from addTimedHandler().
     *
     *  Parameters:
     *    (Strophe.TimedHandler) handRef - The handler reference.
     */
    deleteTimedHandler: function (handRef)
    {
        // this must be done in the Idle loop so that we don't change
        // the handlers during iteration
        this.removeTimeds.push(handRef);
    },

    /** Function: addHandler
     *  Add a stanza handler for the connection.
     *
     *  This function adds a stanza handler to the connection.  The
     *  handler callback will be called for any stanza that matches
     *  the parameters.  Note that if multiple parameters are supplied,
     *  they must all match for the handler to be invoked.
     *
     *  The handler will receive the stanza that triggered it as its argument.
     *  The handler should return true if it is to be invoked again;
     *  returning false will remove the handler after it returns.
     *
     *  As a convenience, the ns parameters applies to the top level element
     *  and also any of its immediate children.  This is primarily to make
     *  matching /iq/query elements easy.
     *
     *  The return value should be saved if you wish to remove the handler
     *  with deleteHandler().
     *
     *  Parameters:
     *    (Function) handler - The user callback.
     *    (String) ns - The namespace to match.
     *    (String) name - The stanza name to match.
     *    (String) type - The stanza type attribute to match.
     *    (String) id - The stanza id attribute to match.
     *    (String) from - The stanza from attribute to match.
     *
     *  Returns:
     *    A reference to the handler that can be used to remove it.
     */
    addHandler: function (handler, ns, name, type, id, from)
    {
        var hand = new Strophe.Handler(handler, ns, name, type, id, from);
        this.addHandlers.push(hand);
        return hand;
    },

    /** Function: deleteHandler
     *  Delete a stanza handler for a connection.
     *
     *  This function removes a stanza handler from the connection.  The
     *  handRef parameter is *not* the function passed to addHandler(),
     *  but is the reference returned from addHandler().
     *
     *  Parameters:
     *    (Strophe.Handler) handRef - The handler reference.
     */
    deleteHandler: function (handRef)
    {
        // this must be done in the Idle loop so that we don't change
        // the handlers during iteration
        this.removeHandlers.push(handRef);
    },

    /** Function: disconnect
     *  Start the graceful disconnection process.
     *
     *  This function starts the disconnection process.  This process starts
     *  by sending unavailable presence and sending BOSH body of type
     *  terminate.  A timeout handler makes sure that disconnection happens
     *  even if the BOSH server does not respond.
     *
     *  The user supplied connection callback will be notified of the
     *  progress as this process happens.
     *
     *  Parameters:
     *    (String) reason - The reason the disconnect is occuring.
     */
    disconnect: function (reason)
    {
        this._changeConnectStatus(Strophe.Status.DISCONNECTING, reason);

        Strophe.info("Disconnect was called because: " + reason);
        if (this.connected) {
            // setup timeout handler
            this._disconnectTimeout = this._addSysTimedHandler(
                3000, this._onDisconnectTimeout.bind(this));
            this._sendTerminate();
        }
    },

    /** PrivateFunction: _changeConnectStatus
     *  _Private_ helper function that makes sure plugins and the user's
     *  callback are notified of connection status changes.
     *
     *  Parameters:
     *    (Integer) status - the new connection status, one of the values
     *      in Strophe.Status
     *    (String) condition - the error condition or null
     */
    _changeConnectStatus: function (status, condition)
    {
        // notify all plugins listening for status changes
        for (var k in Strophe._connectionPlugins) {
            var plugin = this[k];
            if (plugin.statusChanged) {
                try {
                    plugin.statusChanged(status, condition);
                } catch (err) {
                    Strophe.error("" + k + " plugin caused an exception " +
                                  "changing status: " + err);
                }
            }
        }

        // notify the user's callback
        if (this.connect_callback) {
            try {
                this.connect_callback(status, condition);
            } catch (err) {
                Strophe.error("User connection callback caused an " +
                              "exception: " + err);
            }
        }
    },

    /** PrivateFunction: _buildBody
     *  _Private_ helper function to generate the <body/> wrapper for BOSH.
     *
     *  Returns:
     *    A Strophe.Builder with a <body/> element.
     */
    _buildBody: function ()
    {
        var bodyWrap = $build('body', {
            rid: this.rid++,
            xmlns: Strophe.NS.HTTPBIND
        });

        if (this.sid !== null) {
            bodyWrap.attrs({sid: this.sid});
        }

        return bodyWrap;
    },

    /** PrivateFunction: _removeRequest
     *  _Private_ function to remove a request from the queue.
     *
     *  Parameters:
     *    (Strophe.Request) req - The request to remove.
     */
    _removeRequest: function (req)
    {
        Strophe.debug("removing request");

        var i;
        for (i = this._requests.length - 1; i >= 0; i--) {
            if (req == this._requests[i]) {
                this._requests.splice(i, 1);
            }
        }

        // set the onreadystatechange handler to a null function so
        // that we don't get any misfires
        req.xhr.onreadystatechange = function () {};

        this._throttledRequestHandler();
    },

    /** PrivateFunction: _restartRequest
     *  _Private_ function to restart a request that is presumed dead.
     *
     *  Parameters:
     *    (Integer) i - The index of the request in the queue.
     */
    _restartRequest: function (i)
    {
        var req = this._requests[i];
        if (req.dead === null) {
            req.dead = new Date();
        }

        this._processRequest(i);
    },

    /** PrivateFunction: _processRequest
     *  _Private_ function to process a request in the queue.
     *
     *  This function takes requests off the queue and sends them and
     *  restarts dead requests.
     *
     *  Parameters:
     *    (Integer) i - The index of the request in the queue.
     */
    _processRequest: function (i)
    {
        var req = this._requests[i];
        var reqStatus = -1;

        try {
            if (req.xhr.readyState == 4) {
                reqStatus = req.xhr.status;
            }
        } catch (e) {
            Strophe.error("caught an error in _requests[" + i +
                          "], reqStatus: " + reqStatus);
        }

        if (typeof(reqStatus) == "undefined") {
            reqStatus = -1;
        }

        var now = new Date();
        var time_elapsed = req.age();
        var primaryTimeout = (!isNaN(time_elapsed) &&
                              time_elapsed > Strophe.TIMEOUT);
        var secondaryTimeout = (req.dead !== null &&
                                req.timeDead() > Strophe.SECONDARY_TIMEOUT);
        var requestCompletedWithServerError = (req.xhr.readyState == 4 &&
                                               (reqStatus < 1 ||
                                                reqStatus >= 500));
        var oldreq;

        if (primaryTimeout || secondaryTimeout ||
            requestCompletedWithServerError) {
            if (secondaryTimeout) {
                Strophe.error("Request " +
                              this._requests[i].id +
                              " timed out (secondary), restarting");
            }
            req.abort = true;
            req.xhr.abort();
            oldreq = req;
            this._requests[i] = new Strophe.Request(req.xmlData,
                                                    req.origFunc,
                                                    req.rid,
                                                    req.sends);
            req = this._requests[i];
        }

        if (req.xhr.readyState === 0) {
            Strophe.debug("request id " + req.id +
                          "." + req.sends + " posting");

            req.date = new Date();
            try {
                req.xhr.open("POST", this.service, true);
            } catch (e) {
                Strophe.error("XHR open failed.");
                if (!this.connected) {
                    this._changeConnectStatus(Strophe.Status.CONNFAIL,
                                              "bad-service");
                }
                this.disconnect();
                return;
            }

            // Fires the XHR request -- may be invoked immediately
            // or on a gradually expanding retry window for reconnects
            var sendFunc = function () {
                req.xhr.send(req.data);
            };

            // Implement progressive backoff for reconnects --
            // First retry (send == 1) should also be instantaneous
            if (req.sends > 1) {
                // Using a cube of the retry number creats a nicely
                // expanding retry window
                var backoff = Math.pow(req.sends, 3) * 1000;
                setTimeout(sendFunc, backoff);
            } else {
                sendFunc();
            }

            req.sends++;

            this.xmlOutput(req.xmlData);
            this.rawOutput(req.data);
        } else {
            Strophe.debug("_throttledRequestHandler: " +
                          (i === 0 ? "first" : "second") +
                          " request has readyState of " +
                          req.xhr.readyState);
        }
    },

    /** PrivateFunction: _throttledRequestHandler
     *  _Private_ function to throttle requests to the connection window.
     *
     *  This function makes sure we don't send requests so fast that the
     *  request ids overflow the connection window in the case that one
     *  request died.
     */
    _throttledRequestHandler: function ()
    {
        if (!this._requests) {
            Strophe.debug("_throttledRequestHandler called with " +
                          "undefined requests");
        } else {
            Strophe.debug("_throttledRequestHandler called with " +
                          this._requests.length + " requests");
        }

        if (!this._requests || this._requests.length === 0) {
            return;
        }

        if (this._requests.length > 0) {
            this._processRequest(0);
        }

        if (this._requests.length > 1 &&
            Math.abs(this._requests[0].rid -
                     this._requests[1].rid) < this.window - 1) {
            this._processRequest(1);
        }
    },

    /** PrivateFunction: _onRequestStateChange
     *  _Private_ handler for Strophe.Request state changes.
     *
     *  This function is called when the XMLHttpRequest readyState changes.
     *  It contains a lot of error handling logic for the many ways that
     *  requests can fail, and calls the request callback when requests
     *  succeed.
     *
     *  Parameters:
     *    (Function) func - The handler for the request.
     *    (Strophe.Request) req - The request that is changing readyState.
     */
    _onRequestStateChange: function (func, req)
    {
        Strophe.debug("request id " + req.id +
                      "." + req.sends + " state changed to " +
                      req.xhr.readyState);

        if (req.abort) {
            req.abort = false;
            return;
        }

        // request complete
        var reqStatus;
        if (req.xhr.readyState == 4) {
            reqStatus = 0;
            try {
                reqStatus = req.xhr.status;
            } catch (e) {
                // ignore errors from undefined status attribute.  works
                // around a browser bug
            }

            if (typeof(reqStatus) == "undefined") {
                reqStatus = 0;
            }

            if (this.disconnecting) {
                if (reqStatus >= 400) {
                    this._hitError(reqStatus);
                    return;
                }
            }

            var reqIs0 = (this._requests[0] == req);
            var reqIs1 = (this._requests[1] == req);

            if ((reqStatus > 0 && reqStatus < 500) || req.sends > 5) {
                // remove from internal queue
                this._removeRequest(req);
                Strophe.debug("request id " +
                              req.id +
                              " should now be removed");
            }

            // request succeeded
            if (reqStatus == 200) {
                // if request 1 finished, or request 0 finished and request
                // 1 is over Strophe.SECONDARY_TIMEOUT seconds old, we need to
                // restart the other - both will be in the first spot, as the
                // completed request has been removed from the queue already
                if (reqIs1 ||
                    (reqIs0 && this._requests.length > 0 &&
                     this._requests[0].age() > Strophe.SECONDARY_TIMEOUT)) {
                    this._restartRequest(0);
                }
                // call handler
                Strophe.debug("request id " +
                              req.id + "." +
                              req.sends + " got 200");
                func(req);
                this.errors = 0;
            } else {
                Strophe.error("request id " +
                              req.id + "." +
                              req.sends + " error " + reqStatus +
                              " happened");
                if (reqStatus === 0 ||
                    (reqStatus >= 400 && reqStatus < 600) ||
                    reqStatus >= 12000) {
                    this._hitError(reqStatus);
                    if (reqStatus >= 400 && reqStatus < 500) {
                        this._changeConnectStatus(Strophe.Status.DISCONNECTING,
                                                  null);
                        this._doDisconnect();
                    }
                }
            }

            if (!((reqStatus > 0 && reqStatus < 10000) ||
                  req.sends > 5)) {
                this._throttledRequestHandler();
            }
        }
    },

    /** PrivateFunction: _hitError
     *  _Private_ function to handle the error count.
     *
     *  Requests are resent automatically until their error count reaches
     *  5.  Each time an error is encountered, this function is called to
     *  increment the count and disconnect if the count is too high.
     *
     *  Parameters:
     *    (Integer) reqStatus - The request status.
     */
    _hitError: function (reqStatus)
    {
        this.errors++;
        Strophe.warn("request errored, status: " + reqStatus +
                     ", number of errors: " + this.errors);
        if (this.errors > 4) {
            this._onDisconnectTimeout();
        }
    },

    /** PrivateFunction: _doDisconnect
     *  _Private_ function to disconnect.
     *
     *  This is the last piece of the disconnection logic.  This resets the
     *  connection and alerts the user's connection callback.
     */
    _doDisconnect: function ()
    {
        Strophe.info("_doDisconnect was called");
        this.authenticated = false;
        this.disconnecting = false;
        this.sid = null;
        this.streamId = null;
        this.rid = Math.floor(Math.random() * 4294967295);

        // tell the parent we disconnected
        if (this.connected) {
            this._changeConnectStatus(Strophe.Status.DISCONNECTED, null);
            this.connected = false;
        }

        // delete handlers
        this.handlers = [];
        this.timedHandlers = [];
        this.removeTimeds = [];
        this.removeHandlers = [];
        this.addTimeds = [];
        this.addHandlers = [];
    },

    /** PrivateFunction: _dataRecv
     *  _Private_ handler to processes incoming data from the the connection.
     *
     *  Except for _connect_cb handling the initial connection request,
     *  this function handles the incoming data for all requests.  This
     *  function also fires stanza handlers that match each incoming
     *  stanza.
     *
     *  Parameters:
     *    (Strophe.Request) req - The request that has data ready.
     */
    _dataRecv: function (req)
    {
        try {
            var elem = req.getResponse();
        } catch (e) {
            if (e != "parsererror") throw e;
            this.disconnect("strophe-parsererror");
        }
        if (elem === null) return;

        this.xmlInput(elem);
        this.rawInput(Strophe.serialize(elem));

        // remove handlers scheduled for deletion
        var i, hand;
        while (this.removeHandlers.length > 0) {
            hand = this.removeHandlers.pop();
            i = this.handlers.indexOf(hand);
            if (i >= 0)
                this.handlers.splice(i, 1);
        }

        // add handlers scheduled for addition
        while (this.addHandlers.length > 0) {
            this.handlers.push(this.addHandlers.pop());
        }

        // handle graceful disconnect
        if (this.disconnecting && this._requests.length == 0) {
            this.deleteTimedHandler(this._disconnectTimeout);
            this._disconnectTimeout = null;
            this._doDisconnect();
            return;
        }

        var typ = elem.getAttribute("type");
        var cond, conflict;
        if (typ !== null && typ == "terminate") {
            // an error occurred
            cond = elem.getAttribute("condition");
            conflict = elem.getElementsByTagName("conflict");
            if (cond !== null) {
                if (cond == "remote-stream-error" && conflict.length > 0) {
                    cond = "conflict";
                }
                this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
            } else {
                this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
            }
            this.disconnect();
            return;
        }

        // send each incoming stanza through the handler chain
        var self = this;
        Strophe.forEachChild(elem, null, function (child) {
            var i, newList;
            // process handlers
            newList = self.handlers;
            self.handlers = [];
            for (i = 0; i < newList.length; i++) {
                var hand = newList[i];
                if (hand.isMatch(child) &&
                    (self.authenticated || !hand.user)) {
                    if (hand.run(child)) {
                        self.handlers.push(hand);
                    }
                } else {
                    self.handlers.push(hand);
                }
            }
        });
    },

    /** PrivateFunction: _sendTerminate
     *  _Private_ function to send initial disconnect sequence.
     *
     *  This is the first step in a graceful disconnect.  It sends
     *  the BOSH server a terminate body and includes an unavailable
     *  presence if authentication has completed.
     */
    _sendTerminate: function ()
    {
        Strophe.info("_sendTerminate was called");
        var body = this._buildBody().attrs({type: "terminate"});

        var presence, i;
        if (this.authenticated) {
            body.c('presence', {
                xmlns: Strophe.NS.CLIENT,
                type: 'unavailable'
            });
        }

        this.disconnecting = true;

        var req = new Strophe.Request(body.tree(),
                                      this._onRequestStateChange.bind(this)
                                          .prependArg(this._dataRecv.bind(this)),
                                      body.tree().getAttribute("rid"));

        // abort and clear all waiting requests
        var r;
        while (this._requests.length > 0) {
            r = this._requests.pop();
            r.xhr.abort();
            r.abort = true;
        }

        this._requests.push(req);
        this._throttledRequestHandler();
    },

    /** PrivateFunction: _connect_cb
     *  _Private_ handler for initial connection request.
     *
     *  This handler is used to process the initial connection request
     *  response from the BOSH server. It is used to set up authentication
     *  handlers and start the authentication process.
     *
     *  SASL authentication will be attempted if available, otherwise
     *  the code will fall back to legacy authentication.
     *
     *  Parameters:
     *    (Strophe.Request) req - The current request.
     */
    _connect_cb: function (req)
    {
        Strophe.info("_connect_cb was called");

        this.connected = true;
        var bodyWrap = req.getResponse();
        if (!bodyWrap) return;

        this.xmlInput(bodyWrap);
        this.rawInput(Strophe.serialize(bodyWrap));

        var typ = bodyWrap.getAttribute("type");
        var cond, conflict;
        if (typ !== null && typ == "terminate") {
            // an error occurred
            cond = bodyWrap.getAttribute("condition");
            conflict = bodyWrap.getElementsByTagName("conflict");
            if (cond !== null) {
                if (cond == "remote-stream-error" && conflict.length > 0) {
                    cond = "conflict";
                }
                this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
            } else {
                this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
            }
            return;
        }

        // check to make sure we don't overwrite these if _connect_cb is
        // called multiple times in the case of missing stream:features
        if (!this.sid) {
            this.sid = bodyWrap.getAttribute("sid");
        }
        if (!this.stream_id) {
            this.stream_id = bodyWrap.getAttribute("authid");
        }

        // TODO - add SASL anonymous for guest accounts
        var do_sasl_plain = false;
        var do_sasl_digest_md5 = false;
        var do_sasl_anonymous = false;

        var mechanisms = bodyWrap.getElementsByTagName("mechanism");
        var i, mech, auth_str, hashed_auth_str;
        if (mechanisms.length > 0) {
            for (i = 0; i < mechanisms.length; i++) {
                mech = Strophe.getText(mechanisms[i]);
                if (mech == 'DIGEST-MD5') {
                    do_sasl_digest_md5 = true;
                } else if (mech == 'PLAIN') {
                    do_sasl_plain = true;
                } else if (mech == 'ANONYMOUS') {
                    do_sasl_anonymous = true;
                }
            }
        } else {
            // we didn't get stream:features yet, so we need wait for it
            // by sending a blank poll request
            var body = this._buildBody();
            this._requests.push(
                new Strophe.Request(body.tree(),
                                    this._onRequestStateChange.bind(this)
                                      .prependArg(this._connect_cb.bind(this)),
                                    body.tree().getAttribute("rid")));
            this._throttledRequestHandler();
            return;
        }

        if (Strophe.getNodeFromJid(this.jid) === null &&
            do_sasl_anonymous) {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_success_handler = this._addSysHandler(
                this._sasl_success_cb.bind(this), null,
                "success", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "ANONYMOUS"
            }).tree());
        } else if (Strophe.getNodeFromJid(this.jid) === null) {
            // we don't have a node, which is required for non-anonymous
            // client connections
            this._changeConnectStatus(Strophe.Status.CONNFAIL,
                                      'x-strophe-bad-non-anon-jid');
            this.disconnect();
        } else if (do_sasl_digest_md5) {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_challenge_handler = this._addSysHandler(
                this._sasl_challenge1_cb.bind(this), null,
                "challenge", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "DIGEST-MD5"
            }).tree());
        } else if (do_sasl_plain) {
            // Build the plain auth string (barejid null
            // username null password) and base 64 encoded.
            auth_str = Strophe.escapeJid(
                Strophe.getBareJidFromJid(this.jid));
            auth_str = auth_str + "\u0000";
            auth_str = auth_str + Strophe.getNodeFromJid(this.jid);
            auth_str = auth_str + "\u0000";
            auth_str = auth_str + this.pass;

            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._sasl_success_handler = this._addSysHandler(
                this._sasl_success_cb.bind(this), null,
                "success", null, null);
            this._sasl_failure_handler = this._addSysHandler(
                this._sasl_failure_cb.bind(this), null,
                "failure", null, null);

            hashed_auth_str = encode64(auth_str);
            this.send($build("auth", {
                xmlns: Strophe.NS.SASL,
                mechanism: "PLAIN"
            }).t(hashed_auth_str).tree());
        } else {
            this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
            this._addSysHandler(this._auth1_cb.bind(this), null, null,
                                null, "_auth_1");

            this.send($iq({
                type: "get",
                to: this.domain,
                id: "_auth_1"
            }).c("query", {
                xmlns: Strophe.NS.AUTH
            }).c("username", {}).t(Strophe.getNodeFromJid(this.jid)).tree());
        }
    },

    /** PrivateFunction: _sasl_challenge1_cb
     *  _Private_ handler for DIGEST-MD5 SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The challenge stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_challenge1_cb: function (elem)
    {
        var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;

        var challenge = decode64(Strophe.getText(elem));
        var cnonce = hex_md5(Math.random() * 1234567890);
        var realm = "";
        var host = null;
        var nonce = "";
        var qop = "";
        var matches;

        // remove unneeded handlers
        this.deleteHandler(this._sasl_failure_handler);

        while (challenge.match(attribMatch)) {
            matches = challenge.match(attribMatch);
            challenge = challenge.replace(matches[0], "");
            matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
            switch (matches[1]) {
            case "realm":
                realm = matches[2];
                break;
            case "nonce":
                nonce = matches[2];
                break;
            case "qop":
                qop = matches[2];
                break;
            case "host":
                host = matches[2];
                break;
            }
        }

        var digest_uri = "xmpp/" + this.domain;
        if (host !== null) {
            digest_uri = digest_uri + "/" + host;
        }

        var A1 = str_md5(Strophe.getNodeFromJid(this.jid) +
                         ":" + realm + ":" + this.pass) +
            ":" + nonce + ":" + cnonce;
        var A2 = 'AUTHENTICATE:' + digest_uri;

        var responseText = "";
        responseText += 'username=' +
            this._quote(Strophe.getNodeFromJid(this.jid)) + ',';
        responseText += 'realm=' + this._quote(realm) + ',';
        responseText += 'nonce=' + this._quote(nonce) + ',';
        responseText += 'cnonce=' + this._quote(cnonce) + ',';
        responseText += 'nc="00000001",';
        responseText += 'qop="auth",';
        responseText += 'digest-uri=' + this._quote(digest_uri) + ',';
        responseText += 'response=' + this._quote(hex_md5(hex_md5(A1) + ":" +
                                               nonce + ":00000001:" +
                                               cnonce + ":auth:" +
                                               hex_md5(A2))) + ',';
        responseText += 'charset="utf-8"';

        this._sasl_challenge_handler = this._addSysHandler(
            this._sasl_challenge2_cb.bind(this), null,
            "challenge", null, null);
        this._sasl_success_handler = this._addSysHandler(
            this._sasl_success_cb.bind(this), null,
            "success", null, null);
        this._sasl_failure_handler = this._addSysHandler(
            this._sasl_failure_cb.bind(this), null,
            "failure", null, null);

        this.send($build('response', {
            xmlns: Strophe.NS.SASL
        }).t(encode64(responseText)).tree());

        return false;
    },

    /** PrivateFunction: _quote
     *  _Private_ utility function to backslash escape and quote strings.
     *
     *  Parameters:
     *    (String) str - The string to be quoted.
     *
     *  Returns:
     *    quoted string
     */
    _quote: function (str)
    {
        return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
    },


    /** PrivateFunction: _sasl_challenge2_cb
     *  _Private_ handler for second step of DIGEST-MD5 SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The challenge stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_challenge2_cb: function (elem)
    {
        // remove unneeded handlers
        this.deleteHandler(this._sasl_success_handler);
        this.deleteHandler(this._sasl_failure_handler);

        this._sasl_success_handler = this._addSysHandler(
            this._sasl_success_cb.bind(this), null,
            "success", null, null);
        this._sasl_failure_handler = this._addSysHandler(
            this._sasl_failure_cb.bind(this), null,
            "failure", null, null);
        this.send($build('response', {xmlns: Strophe.NS.SASL}).tree());
        return false;
    },

    /** PrivateFunction: _auth1_cb
     *  _Private_ handler for legacy authentication.
     *
     *  This handler is called in response to the initial <iq type='get'/>
     *  for legacy authentication.  It builds an authentication <iq/> and
     *  sends it, creating a handler (calling back to _auth2_cb()) to
     *  handle the result
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza that triggered the callback.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _auth1_cb: function (elem)
    {
        var use_digest = false;
        var check_query, check_digest;

        if (elem.getAttribute("type") == "result") {
            // Find digest
            check_query = elem.childNodes[0];
            if (check_query) {
                check_digest = check_query.getElementsByTagName("digest")[0];
                if (check_digest) {
                    use_digest = true;
                }
            }
        }

        // Use digest or plaintext depending on the server features
        var iq = $iq({type: "set", id: "_auth_2"})
            .c('query', {xmlns: Strophe.NS.AUTH})
            .c('username', {}).t(Strophe.getNodeFromJid(this.jid));
        if (use_digest) {
            iq.up().c("digest", {})
                .t(hex_sha1(this.stream_id + this.pass));
        } else {
            iq.up().c('password', {}).t(this.pass);
        }
        if (!Strophe.getResourceFromJid(this.jid)) {
            // since the user has not supplied a resource, we pick
            // a default one here.  unlike other auth methods, the server
            // cannot do this for us.
            this.jid = Strophe.getBareJidFromJid(this.jid) + '/strophe';
        }
        iq.up().c('resource', {}).t(Strophe.getResourceFromJid(this.jid));

        this._addSysHandler(this._auth2_cb.bind(this), null,
                            null, null, "_auth_2");

        this.send(iq.tree());

        return false;
    },

    /** PrivateFunction: _sasl_success_cb
     *  _Private_ handler for succesful SASL authentication.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_success_cb: function (elem)
    {
        Strophe.info("SASL authentication succeeded.");

        // remove old handlers
        this.deleteHandler(this._sasl_failure_handler);
        this._sasl_failure_handler = null;
        if (this._sasl_challenge_handler) {
            this.deleteHandler(this._sasl_challenge_handler);
            this._sasl_challenge_handler = null;
        }

        this._addSysHandler(this._sasl_auth1_cb.bind(this), null,
                            "stream:features", null, null);

        // we must send an xmpp:restart now
        this._sendRestart();

        return false;
    },

    /** PrivateFunction: _sasl_auth1_cb
     *  _Private_ handler to start stream binding.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_auth1_cb: function (elem)
    {
        var i, child;

        for (i = 0; i < elem.childNodes.length; i++) {
            child = elem.childNodes[i];
            if (child.nodeName == 'bind') {
                this.do_bind = true;
            }

            if (child.nodeName == 'session') {
                this.do_session = true;
            }
        }

        if (!this.do_bind) {
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        } else {
            this._addSysHandler(this._sasl_bind_cb.bind(this), null, null,
                                null, "_bind_auth_2");

            var resource = Strophe.getResourceFromJid(this.jid);
            if (resource)
                this.send($iq({type: "set", id: "_bind_auth_2"})
                          .c('bind', {xmlns: Strophe.NS.BIND})
                          .c('resource', {}).t(resource).tree());
            else
                this.send($iq({type: "set", id: "_bind_auth_2"})
                          .c('bind', {xmlns: Strophe.NS.BIND})
                          .tree());
        }

        return false;
    },

    /** PrivateFunction: _sasl_bind_cb
     *  _Private_ handler for binding result and session start.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_bind_cb: function (elem)
    {
        if (elem.getAttribute("type") == "error") {
            Strophe.info("SASL binding failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }

        // TODO - need to grab errors
        var bind = elem.getElementsByTagName("bind");
        var jidNode;
        if (bind.length > 0) {
            // Grab jid
            jidNode = bind[0].getElementsByTagName("jid");
            if (jidNode.length > 0) {
                this.jid = Strophe.getText(jidNode[0]);

                if (this.do_session) {
                    this._addSysHandler(this._sasl_session_cb.bind(this),
                                        null, null, null, "_session_auth_2");

                    this.send($iq({type: "set", id: "_session_auth_2"})
                                  .c('session', {xmlns: Strophe.NS.SESSION})
                                  .tree());
                } else {
                    this.authenticated = true;
                    this._changeConnectStatus(Strophe.Status.CONNECTED, null);
                }
            }
        } else {
            Strophe.info("SASL binding failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }
    },

    /** PrivateFunction: _sasl_session_cb
     *  _Private_ handler to finish successful SASL connection.
     *
     *  This sets Connection.authenticated to true on success, which
     *  starts the processing of user handlers.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_session_cb: function (elem)
    {
        if (elem.getAttribute("type") == "result") {
            this.authenticated = true;
            this._changeConnectStatus(Strophe.Status.CONNECTED, null);
        } else if (elem.getAttribute("type") == "error") {
            Strophe.info("Session creation failed.");
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            return false;
        }

        return false;
    },

    /** PrivateFunction: _sasl_failure_cb
     *  _Private_ handler for SASL authentication failure.
     *
     *  Parameters:
     *    (XMLElement) elem - The matching stanza.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _sasl_failure_cb: function (elem)
    {
        // delete unneeded handlers
        if (this._sasl_success_handler) {
            this.deleteHandler(this._sasl_success_handler);
            this._sasl_success_handler = null;
        }
        if (this._sasl_challenge_handler) {
            this.deleteHandler(this._sasl_challenge_handler);
            this._sasl_challenge_handler = null;
        }

        this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
        return false;
    },

    /** PrivateFunction: _auth2_cb
     *  _Private_ handler to finish legacy authentication.
     *
     *  This handler is called when the result from the jabber:iq:auth
     *  <iq/> stanza is returned.
     *
     *  Parameters:
     *    (XMLElement) elem - The stanza that triggered the callback.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _auth2_cb: function (elem)
    {
        if (elem.getAttribute("type") == "result") {
            this.authenticated = true;
            this._changeConnectStatus(Strophe.Status.CONNECTED, null);
        } else if (elem.getAttribute("type") == "error") {
            this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
            this.disconnect();
        }

        return false;
    },

    /** PrivateFunction: _addSysTimedHandler
     *  _Private_ function to add a system level timed handler.
     *
     *  This function is used to add a Strophe.TimedHandler for the
     *  library code.  System timed handlers are allowed to run before
     *  authentication is complete.
     *
     *  Parameters:
     *    (Integer) period - The period of the handler.
     *    (Function) handler - The callback function.
     */
    _addSysTimedHandler: function (period, handler)
    {
        var thand = new Strophe.TimedHandler(period, handler);
        thand.user = false;
        this.addTimeds.push(thand);
        return thand;
    },

    /** PrivateFunction: _addSysHandler
     *  _Private_ function to add a system level stanza handler.
     *
     *  This function is used to add a Strophe.Handler for the
     *  library code.  System stanza handlers are allowed to run before
     *  authentication is complete.
     *
     *  Parameters:
     *    (Function) handler - The callback function.
     *    (String) ns - The namespace to match.
     *    (String) name - The stanza name to match.
     *    (String) type - The stanza type attribute to match.
     *    (String) id - The stanza id attribute to match.
     */
    _addSysHandler: function (handler, ns, name, type, id)
    {
        var hand = new Strophe.Handler(handler, ns, name, type, id);
        hand.user = false;
        this.addHandlers.push(hand);
        return hand;
    },

    /** PrivateFunction: _onDisconnectTimeout
     *  _Private_ timeout handler for handling non-graceful disconnection.
     *
     *  If the graceful disconnect process does not complete within the
     *  time allotted, this handler finishes the disconnect anyway.
     *
     *  Returns:
     *    false to remove the handler.
     */
    _onDisconnectTimeout: function ()
    {
        Strophe.info("_onDisconnectTimeout was called");

        // cancel all remaining requests and clear the queue
        var req;
        while (this._requests.length > 0) {
            req = this._requests.pop();
            req.xhr.abort();
            req.abort = true;
        }

        // actually disconnect
        this._doDisconnect();

        return false;
    },

    /** PrivateFunction: _onIdle
     *  _Private_ handler to process events during idle cycle.
     *
     *  This handler is called every 100ms to fire timed handlers that
     *  are ready and keep poll requests going.
     */
    _onIdle: function ()
    {
        var i, thand, since, newList;

        // remove timed handlers that have been scheduled for deletion
        while (this.removeTimeds.length > 0) {
            thand = this.removeTimeds.pop();
            i = this.timedHandlers.indexOf(thand);
            if (i >= 0)
                this.timedHandlers.splice(i, 1);
        }

        // add timed handlers scheduled for addition
        while (this.addTimeds.length > 0) {
            this.timedHandlers.push(this.addTimeds.pop());
        }

        // call ready timed handlers
        var now = new Date().getTime();
        newList = [];
        for (i = 0; i < this.timedHandlers.length; i++) {
            thand = this.timedHandlers[i];
            if (this.authenticated || !thand.user) {
                since = thand.lastCalled + thand.period;
                if (since - now <= 0) {
                    if (thand.run()) {
                        newList.push(thand);
                    }
                } else {
                    newList.push(thand);
                }
            }
        }
        this.timedHandlers = newList;

        var body, time_elapsed;

        // if no requests are in progress, poll
        if (this.authenticated && this._requests.length === 0 &&
            this._data.length === 0 && !this.disconnecting) {
            Strophe.info("no requests during idle cycle, sending " +
                         "blank request");
            this._data.push(null);
        }

        if (this._requests.length < 2 && this._data.length > 0 &&
            !this.paused) {
            body = this._buildBody();
            for (i = 0; i < this._data.length; i++) {
                if (this._data[i] !== null) {
                    if (this._data[i] === "restart") {
                        body.attrs({
                            to: this.domain,
                            "xml:lang": "en",
                            "xmpp:restart": "true",
                            "xmlns:xmpp": Strophe.NS.BOSH
                        });
                    } else {
                        body.cnode(this._data[i]).up();
                    }
                }
            }
            delete this._data;
            this._data = [];
            this._requests.push(
                new Strophe.Request(body.tree(),
                                    this._onRequestStateChange.bind(this)
                                    .prependArg(this._dataRecv.bind(this)),
                                    body.tree().getAttribute("rid")));
            this._processRequest(this._requests.length - 1);
        }

        if (this._requests.length > 0) {
            time_elapsed = this._requests[0].age();
            if (this._requests[0].dead !== null) {
                if (this._requests[0].timeDead() >
                    Strophe.SECONDARY_TIMEOUT) {
                    this._throttledRequestHandler();
                }
            }

            if (time_elapsed > Strophe.TIMEOUT) {
                Strophe.warn("Request " +
                             this._requests[0].id +
                             " timed out, over " + Strophe.TIMEOUT +
                             " seconds since last activity");
                this._throttledRequestHandler();
            }
        }

        // reactivate the timer
        clearTimeout(this._idleTimeout);
        this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
    }
};

if (callback) {
    callback(Strophe, $build, $msg, $iq, $pres);
}

})(function () {
    Strophe = arguments[0];
    $build = arguments[1];
    $msg = arguments[2];
    $iq = arguments[3];
    $pres = arguments[4];
});