/* Vibe v3.0.0-Alpha2 | http://vibe-project.github.io/projects/vibe-javascript-client/ | (c) 2014, The Vibe Project | http://www.apache.org/licenses/LICENSE-2.0 */
(function(k,t){if("function"===typeof define&&define.amd)define([],function(){return t(k)});else if("object"===typeof exports){var y=require("jsdom").jsdom().parentWindow;y.WebSocket=require("ws");y.EventSource=require("eventsource");module.exports=t(y);module.exports.util.corsable=!0}else k.vibe=t(k)})(this,function(k){function t(c){var b,a,h,g,d,e,m=[],k=function(k,r){r=r||[];a=!c||[k,r];h=!0;e=g||0;g=0;for(d=m.length;e',n.body.appendChild(c));d=c.firstChild.contentWindow;return{init:function(){d.callbacks=[a];d.fire=function(b){var a;for(a=0;a
b._heartbeat&&(a(),d.once("close",function(){clearTimeout(c)}));f.connecting.lock();r=w=l=null}).on("close",function(){m="closed";var a,c,g=f.close.order;for(a in f)c=f[a],c.order';return encodeURI(decodeURI(b.firstChild.href))},on:function(c,b,a){c.addEventListener?c.addEventListener(b,a,!1):c.attachEvent&&c.attachEvent("on"+b,a)},off:function(c,b,a){c.removeEventListener?c.removeEventListener(b,
a,!1):c.detachEvent&&c.detachEvent("on"+b,a)},url:function(c,b){var a,h=[];b=b||{};b._=u++;for(a in b)h.push(encodeURIComponent(a)+"="+encodeURIComponent(b[a]));return c+(/\?/.test(c)?"&":"?")+h.join("&").replace(/%20/g,"+")},xhr:function(){try{return new k.XMLHttpRequest}catch(c){try{return new k.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}},parseJSON:k.JSON?k.JSON.parse:function(c){return Function("return "+c)()},stringifyJSON:k.JSON?k.JSON.stringify:function(c){function b(a){return'"'+a.replace(h,
function(a){var b=g[a];return"string"===typeof b?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"'}function a(a){return 10>a?"0"+a:a}var h=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,g={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};return function f(c,g){var h,e,k,l=g[c];k=typeof l;l&&"object"===typeof l&&"function"===typeof l.toJSON&&(l=l.toJSON(c),k=typeof l);switch(k){case "string":return b(l);
case "number":return isFinite(l)?String(l):"null";case "boolean":return String(l);case "object":if(!l)return"null";switch(F.call(l)){case "[object Date]":return isFinite(l.valueOf())?'"'+l.getUTCFullYear()+"-"+a(l.getUTCMonth()+1)+"-"+a(l.getUTCDate())+"T"+a(l.getUTCHours())+":"+a(l.getUTCMinutes())+":"+a(l.getUTCSeconds())+'Z"':"null";case "[object Array]":e=l.length;k=[];for(h=0;hc.indexOf("android")&&/version\/(.+) (safari)/.exec(c)||[];b[c[1]||""]=!0;b.version=c[2]||"0";b.vmajor=b.version.split(".")[0];b.trident&&(b.msie=!0);return b}();var q={session:function(c,b){function a(a,b){var c,d=a.length;for(c=0;c';c.firstChild.value=a;e.on(c.lastChild,"load",function(){n.body.removeChild(c)});
n.body.appendChild(c);c.submit()};return a},sse:function(c,b){var a,h=k.EventSource,g=q.httpbase(c,b);if(h&&!(b.crossOrigin&&e.browser.safari&&7>e.browser.vmajor))return g.open=function(){var b=g.uri.open();a=new h(b,{withCredentials:!0});a.onopen=function(){c.fire("open")};a.onmessage=function(a){c.receive(a.data)};a.onerror=function(){a.close();c.fire("close","done")}},g.abort=function(){a.close()},g},streambase:function(c,b){var a="",h=q.httpbase(c,b);h.parse=function(b){if(b=b.replace(/^\s+/,
"")){var d=(a+b).split("\n\n");for(b=0;be.browser.vmajor||e.browser.opera&&13>e.browser.vmajor||b.crossOrigin&&!e.corsable))return h.open=function(){var b,d,f=h.uri.open();a=e.xhr();a.onreadystatechange=function(){3===a.readyState&&200===a.status?(d=a.responseText.length,b?d>b&&h.parse(a.responseText.substring(b)):(c.fire("open"),h.parse(a.responseText)),
b=d):4===a.readyState&&c.fire("close",200===a.status?"done":"error")};a.open("GET",f);e.corsable&&(a.withCredentials=!0);a.send(null)},h.abort=function(){a.abort()},h},streamxdr:function(c,b){var a,h=k.XDomainRequest,e=q.streambase(c,b);if(h&&b.xdrURL)return e.open=function(){var d,f,k=b.xdrURL.call(c,e.uri.open());a=new h;a.onprogress=function(){f=a.responseText.length;d?e.parse(a.responseText.substring(d)):(c.fire("open"),e.parse(a.responseText));d=f};a.onerror=function(){c.fire("close","error")};
a.onload=function(){c.fire("close","done")};a.open("GET",k);a.send()},e.abort=function(){a.abort()},e},streamiframe:function(c,b){var a,e,g=k.ActiveXObject,d=q.streambase(c,b);if(g&&!b.crossOrigin){try{new g("htmlfile")}catch(f){return}d.open=function(){function b(a){var c;(function s(){c=setTimeout(function(){!1!==a()&&s()},1)})();return function(){clearTimeout(c)}}var f,k,n=d.uri.open();a=new g("htmlfile");a.open();a.close();f=a.createElement("iframe");f.src=n;a.body.appendChild(f);k=f.contentDocument||
f.contentWindow.document;e=b(function(){function a(){var c;c=f.cloneNode(!0);c.appendChild(k.createTextNode("."));c=c.innerText.replace(/\r\n/g,"\n");return c.substring(0,c.length-1)}var f;if(k.firstChild){f=k.body.lastChild;if(!f)return c.fire("close","error"),!1;c.fire("open");d.parse(a());f.innerText="";e=b(function(){var b=a();b&&(f.innerText="",d.parse(b));if("complete"===k.readyState)return c.fire("close","done"),!1});return!1}})};d.abort=function(){e();a.execCommand("Stop")};return d}},longpollbase:function(c,
b){var a=q.httpbase(c,b);a.uri.poll=function(a){return e.url(b.url,{id:b.id,when:"poll",lastEventIds:a.join(",")})};a.open=function(){a.connect(a.uri.open(),function(){function b(g){a.connect(a.uri.poll(g),function(a){if(a){var f=[];a=e.parseJSON(a);var g=e.isArray(a)?a:[a];for(a=0;a