// Generated by CoffeeScript 1.6.3
/*
   Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0

   Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/)
   Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com)
*/
! function() {
    var t, n, e, i, o = {}.hasOwnProperty,
        r = [].slice;
    t = { LF: "\n", NULL: "\0" };
    e = function() {
        var n;

        function e(t, n, e) {
            this.command = t;
            this.headers = n != null ? n : {};
            this.body = e != null ? e : ""
        }
        e.prototype.toString = function() {
            var n, e, i, r;
            n = [this.command];
            r = this.headers;
            for (e in r) {
                if (!o.call(r, e)) continue;
                i = r[e];
                n.push("" + e + ":" + i)
            }
            if (this.body) { n.push("content-length:" + ("" + this.body).length) }
            n.push(t.LF + this.body);
            return n.join(t.LF)
        };
        n = function(n) {
            var i, o, r, s, u, a, c, f, d, h, l, p, g, b, m, y, v;
            s = n.search(RegExp("" + t.LF + t.LF));
            u = n.substring(0, s).split(t.LF);
            r = u.shift();
            a = {};
            p = function(t) { return t.replace(/^\s+|\s+$/g, "") };
            y = u.reverse();
            for (g = 0, m = y.length; g < m; g++) {
                h = y[g];
                f = h.indexOf(":");
                a[p(h.substring(0, f))] = p(h.substring(f + 1))
            }
            i = "";
            l = s + 2;
            if (a["content-length"]) {
                d = parseInt(a["content-length"]);
                i = ("" + n).substring(l, l + d)
            } else {
                o = null;
                for (c = b = l, v = n.length; l <= v ? b < v : b > v; c = l <= v ? ++b : --b) {
                    o = n.charAt(c);
                    if (o === t.NULL) { break }
                    i += o
                }
            }
            return new e(r, a, i)
        };
        e.unmarshall = function(e) {
            var i;
            return function() {
                var o, r, s, u;
                s = e.split(RegExp("" + t.NULL + t.LF + "*"));
                u = [];
                for (o = 0, r = s.length; o < r; o++) { i = s[o]; if ((i != null ? i.length : void 0) > 0) { u.push(n(i)) } }
                return u
            }()
        };
        e.marshall = function(n, i, o) {
            var r;
            r = new e(n, i, o);
            return r.toString() + t.NULL
        };
        return e
    }();
    n = function() {
        var n;

        function o(t) {
            this.ws = t;
            this.ws.binaryType = "arraybuffer";
            this.counter = 0;
            this.connected = false;
            this.heartbeat = { outgoing: 1e4, incoming: 1e4 };
            this.maxWebSocketFrameSize = 16 * 1024;
            this.subscriptions = {}
        }
        o.prototype.debug = function(t) { var n; return typeof window !== "undefined" && window !== null ? (n = window.console) != null ? n.log(t) : void 0 : void 0 };
        n = function() { return Date.now || (new Date).valueOf };
        o.prototype._transmit = function(t, n, i) {
            var o;
            o = e.marshall(t, n, i);
            if (typeof this.debug === "function") { this.debug(">>> " + o) }
            while (true) {
                if (o.length > this.maxWebSocketFrameSize) {
                    this.ws.send(o.substring(0, this.maxWebSocketFrameSize));
                    o = o.substring(this.maxWebSocketFrameSize);
                    if (typeof this.debug === "function") { this.debug("remaining = " + o.length) }
                } else { return this.ws.send(o) }
            }
        };
        o.prototype._setupHeartbeat = function(e) {
            var o, r, s, u, a, c, f = this;
            if ((a = e.version) !== i.VERSIONS.V1_1 && a !== i.VERSIONS.V1_2) { return }
            c = function() {
                var t, n, i, o;
                i = e["heart-beat"].split(",");
                o = [];
                for (t = 0, n = i.length; t < n; t++) {
                    u = i[t];
                    o.push(parseInt(u))
                }
                return o
            }(), r = c[0], o = c[1];
            if (!(this.heartbeat.outgoing === 0 || o === 0)) {
                s = Math.max(this.heartbeat.outgoing, o);
                if (typeof this.debug === "function") { this.debug("send PING every " + s + "ms") }
                this.pinger = typeof window !== "undefined" && window !== null ? window.setInterval(function() { f.ws.send(t.LF); return typeof f.debug === "function" ? f.debug(">>> PING") : void 0 }, s) : void 0
            }
            if (!(this.heartbeat.incoming === 0 || r === 0)) {
                s = Math.max(this.heartbeat.incoming, r);
                if (typeof this.debug === "function") { this.debug("check PONG every " + s + "ms") }
                return this.ponger = typeof window !== "undefined" && window !== null ? window.setInterval(function() {
                    var t;
                    t = n() - f.serverActivity;
                    if (t > s * 2) { if (typeof f.debug === "function") { f.debug("did not receive server activity for the last " + t + "ms") } return f.ws.close() }
                }, s) : void 0
            }
        };
        o.prototype._parseConnect = function() {
            var t, n, e, i;
            t = 1 <= arguments.length ? r.call(arguments, 0) : [];
            i = {};
            switch (t.length) {
                case 2:
                    i = t[0], n = t[1];
                    break;
                case 3:
                    if (t[1] instanceof Function) { i = t[0], n = t[1], e = t[2] } else { i.login = t[0], i.passcode = t[1], n = t[2] }
                    break;
                case 4:
                    i.login = t[0], i.passcode = t[1], n = t[2], e = t[3];
                    break;
                default:
                    i.login = t[0], i.passcode = t[1], n = t[2], e = t[3], i.host = t[4]
            }
            return [i, n, e]
        };
        o.prototype.connect = function() {
            var o, s, u, a, c = this;
            o = 1 <= arguments.length ? r.call(arguments, 0) : [];
            a = this._parseConnect.apply(this, o);
            u = a[0], this.connectCallback = a[1], s = a[2];
            if (typeof this.debug === "function") { this.debug("Opening Web Socket...") }
            this.ws.onmessage = function(i) {
                var o, r, u, a, f, d, h, l, p, g, b, m;
                a = typeof ArrayBuffer !== "undefined" && i.data instanceof ArrayBuffer ? (o = new Uint8Array(i.data), typeof c.debug === "function" ? c.debug("--- got data length: " + o.length) : void 0, function() {
                    var t, n, e;
                    e = [];
                    for (t = 0, n = o.length; t < n; t++) {
                        r = o[t];
                        e.push(String.fromCharCode(r))
                    }
                    return e
                }().join("")) : i.data;
                c.serverActivity = n();
                if (a === t.LF) { if (typeof c.debug === "function") { c.debug("<<< PONG") } return }
                if (typeof c.debug === "function") { c.debug("<<< " + a) }
                b = e.unmarshall(a);
                m = [];
                for (p = 0, g = b.length; p < g; p++) {
                    f = b[p];
                    switch (f.command) {
                        case "CONNECTED":
                            if (typeof c.debug === "function") { c.debug("connected to server " + f.headers.server) }
                            c.connected = true;
                            c._setupHeartbeat(f.headers);
                            m.push(typeof c.connectCallback === "function" ? c.connectCallback(f) : void 0);
                            break;
                        case "MESSAGE":
                            l = f.headers.subscription;
                            h = c.subscriptions[l] || c.onreceive;
                            if (h) {
                                u = c;
                                d = f.headers["message-id"];
                                f.ack = function(t) { if (t == null) { t = {} } return u.ack(d, l, t) };
                                f.nack = function(t) { if (t == null) { t = {} } return u.nack(d, l, t) };
                                m.push(h(f))
                            } else { m.push(typeof c.debug === "function" ? c.debug("Unhandled received MESSAGE: " + f) : void 0) }
                            break;
                        case "RECEIPT":
                            m.push(typeof c.onreceipt === "function" ? c.onreceipt(f) : void 0);
                            break;
                        case "ERROR":
                            m.push(typeof s === "function" ? s(f) : void 0);
                            break;
                        default:
                            m.push(typeof c.debug === "function" ? c.debug("Unhandled frame: " + f) : void 0)
                    }
                }
                return m
            };
            this.ws.onclose = function() {
                var t;
                t = "Whoops! Lost connection to " + c.ws.url;
                if (typeof c.debug === "function") { c.debug(t) }
                c._cleanUp();
                return typeof s === "function" ? s(t) : void 0
            };
            return this.ws.onopen = function() {
                if (typeof c.debug === "function") { c.debug("Web Socket Opened...") }
                u["accept-version"] = i.VERSIONS.supportedVersions();
                u["heart-beat"] = [c.heartbeat.outgoing, c.heartbeat.incoming].join(",");
                return c._transmit("CONNECT", u)
            }
        };
        o.prototype.disconnect = function(t) {
            this._transmit("DISCONNECT");
            this.ws.onclose = null;
            this.ws.close();
            this._cleanUp();
            return typeof t === "function" ? t() : void 0
        };
        o.prototype._cleanUp = function() { this.connected = false; if (this.pinger) { if (typeof window !== "undefined" && window !== null) { window.clearInterval(this.pinger) } } if (this.ponger) { return typeof window !== "undefined" && window !== null ? window.clearInterval(this.ponger) : void 0 } };
        o.prototype.send = function(t, n, e) {
            if (n == null) { n = {} }
            if (e == null) { e = "" }
            n.destination = t;
            return this._transmit("SEND", n, e)
        };
        o.prototype.subscribe = function(t, n, e) {
            var i;
            if (e == null) { e = {} }
            if (!e.id) { e.id = "sub-" + this.counter++ }
            e.destination = t;
            this.subscriptions[e.id] = n;
            this._transmit("SUBSCRIBE", e);
            i = this;
            return { id: e.id, unsubscribe: function() { return i.unsubscribe(e.id) } }
        };
        o.prototype.unsubscribe = function(t) { delete this.subscriptions[t]; return this._transmit("UNSUBSCRIBE", { id: t }) };
        o.prototype.begin = function(t) {
            var n, e;
            e = t || "tx-" + this.counter++;
            this._transmit("BEGIN", { transaction: e });
            n = this;
            return { id: e, commit: function() { return n.commit(e) }, abort: function() { return n.abort(e) } }
        };
        o.prototype.commit = function(t) { return this._transmit("COMMIT", { transaction: t }) };
        o.prototype.abort = function(t) { return this._transmit("ABORT", { transaction: t }) };
        o.prototype.ack = function(t, n, e) {
            if (e == null) { e = {} }
            e["message-id"] = t;
            e.subscription = n;
            return this._transmit("ACK", e)
        };
        o.prototype.nack = function(t, n, e) {
            if (e == null) { e = {} }
            e["message-id"] = t;
            e.subscription = n;
            return this._transmit("NACK", e)
        };
        return o
    }();
    i = {
        libVersion: "2.0.0-next",
        VERSIONS: { V1_0: "1.0", V1_1: "1.1", V1_2: "1.2", supportedVersions: function() { return "1.1,1.0" } },
        client: function(t, e) {
            var o, r;
            if (e == null) { e = ["v10.stomp", "v11.stomp"] }
            o = i.WebSocketClass || WebSocket;
            r = new o(t, e);
            return new n(r)
        },
        over: function(t) { return new n(t) },
        Frame: e
    };
    if (typeof window !== "undefined" && window !== null) { window.Stomp = i } else if (typeof exports !== "undefined" && exports !== null) { exports.Stomp = i } else { self.Stomp = i }
}.call(this);