'use strict';

angular
    .module('ngWebsocket', [])
    .provider('$websocket', function () {
        var wsp = this;

        function $websocketManager () {
            var wsm = this;

            wsm.websocketList = {};

            wsm.$new = function (cfg) {
                var ws = new $websocket(cfg);
                wsm.websocketList[cfg] = ws;

                return ws;
            };
        }

        function $websocket (cfg) {
            var me = this;

            me._eventMap = {};

            me._fireEvent = function () {
                var args = [];

                Array.prototype.push.apply(args, arguments);

                var event = args.shift(),
                    handler = me._eventMap[event];

                if (typeof handler === 'function') handler.apply(me, args);
            };

            me._init = function (cfg) {
                if (typeof cfg === 'undefined') throw new Error('An url must be specified for WebSocket');

                if (typeof cfg === 'string') {
                    me.ws = new WebSocket(cfg);

                    me.ws.onmessage = function (message) {
                        try {
                            var decoded = JSON.parse(message.data);
                            me._fireEvent(decoded.event, decoded.data);
                            me._fireEvent('$message', decoded);
                        }
                        catch (err) {
                            me._fireEvent('$message', message.data);
                        }
                    };

                    me.ws.onerror = function (error) {
                        me._fireEvent('$error', error);
                    };

                    me.ws.onopen = function () {
                        me._fireEvent('$open');
                    };

                    me.ws.onclose = function () {
                        me._fireEvent('$close');
                    };
                }

                return me;
            };

            me.$CONNECTING = 0;
            me.$OPEN = 1;
            me.$CLOSING = 2;
            me.$CLOSED = 3;

            me.$on = function (event, handler) {
                if (typeof event !== 'string' || typeof handler !== 'function') throw new Error('$on accept two parameters: a String and a Function');

                me._eventMap[event] = handler;

                return me;
            };

            me.$un = function (event) {
                if (typeof event !== 'string') throw new Error('$un accept just a String');

                delete me._eventMap[event];

                return me;
            };

            me.$emit = function (event, data) {
                if (!(typeof event === 'string' && typeof data !== 'undefined')) throw new Error('$emit needs two parameter: a String and a Object or a String');

                var message = {
                    event: event,
                    data: data
                };

                me.ws.send(JSON.stringify(message));

                return me;
            };

            me.$close = function () {
                me.ws.close();

                return me;
            };

            me.$status = function () {
                return me.ws.readyState;
            };

            me.$ready = function () {
                return me.$status() === me.$OPEN;
            };

            return me._init(cfg);
        }

        wsp.$get = function () {
            return new $websocketManager();
        };
    });
@@ -476,33 +476,34 @@
 %0A            me.
-_
+$$
 eventMap = %7B%7D;%0A%0A
@@ -509,33 +509,34 @@
 %0A            me.
-_
+$$
 fireEvent = func
@@ -710,25 +710,26 @@
 andler = me.
-_
+$$
 eventMap%5Beve
@@ -837,25 +837,26 @@
          me.
-_
+$$
 init = funct
@@ -1251,33 +1251,34 @@
              me.
-_
+$$
 fireEvent(decode
@@ -1324,33 +1324,34 @@
              me.
-_
+$$
 fireEvent('$mess
@@ -1453,33 +1453,34 @@
              me.
-_
+$$
 fireEvent('$mess
@@ -1624,33 +1624,34 @@
              me.
-_
+$$
 fireEvent('$erro
@@ -1754,33 +1754,34 @@
              me.
-_
+$$
 fireEvent('$open
@@ -1885,17 +1885,18 @@
      me.
-_
+$$
 fireEven
@@ -2325,25 +2325,26 @@
          me.
-_
+$$
 eventMap%5Beve
@@ -2561,17 +2561,18 @@
 lete me.
-_
+$$
 eventMap
@@ -3388,9 +3388,10 @@
  me.
-_
+$$
 init

replaced _ with $$ for private variables