/**
 * Messages controller.
 *
 * @desc This controller is the messages controller.
 * @param {!angular.Scope} $scope
 * @constructor
 */
angular.module('app')
    .controller('Messages', ['$scope', '$interval', '$routeParams', '$location', 'Connection',
        function($scope, $interval, $routeParams, $location, Connection) {

            $scope.myOrders = []
            $scope.ordersPanel = true;
            $scope.path = $location.path();
            $scope.$emit('sidebar', false);

            /**
             * Establish message handlers
             * @msg - message from websocket to pass on to handler
             */
            Connection.$on('load_page', function(e, msg){ $scope.load_page(msg) });
            Connection.$on('messages', function(e, msg){ $scope.parse_messages(msg) });

            $scope.load_page = function(msg) {
                $scope.messagesPanel = true;
                $scope.queryMessages();
            }

            if (Connection.websocket.readyState == 1) {
                $scope.load_page({});
            }

            $scope.queryMessages = function() {
                // Query for messages
                var query = {
                    'type': 'query_messages'
                }
                console.log('querying messages')
                Connection.send('query_messages', query)
                console.log($scope.myself.messages)

            }

            $scope.message = {};
            $scope.parse_messages = function(msg) {
                console.log('parsing messages',msg);
                if (msg != null &&
                    msg.messages != null &&
                    msg.messages.messages != null &&
                    msg.messages.messages.inboxMessages != null) {

                    $scope.messages = msg.messages.messages.inboxMessages;

                    $scope.message = {};
                    if (!$scope.$$phase) {
                        $scope.$apply();
                    }
                }
            }


        }
    ]);

@@ -356,16 +356,17 @@
 ers = %5B%5D
+;
 %0A       
@@ -702,24 +702,25 @@
 ad_page(msg)
+;
  %7D);%0A       
@@ -795,16 +795,17 @@
 ges(msg)
+;
  %7D);%0A%0A  
@@ -939,32 +939,33 @@
 );%0A            %7D
+;
 %0A%0A            if
@@ -1229,32 +1229,33 @@
                %7D
+;
 %0A               
@@ -1287,16 +1287,17 @@
 ssages')
+;
 %0A       
@@ -1345,16 +1345,17 @@
 , query)
+;
 %0A       
@@ -1398,16 +1398,17 @@
 essages)
+;
 %0A%0A      
@@ -1406,32 +1406,33 @@
 ;%0A%0A            %7D
+;
 %0A%0A            $s
@@ -1581,16 +1581,17 @@
  (msg !=
+=
  null &&
@@ -1618,32 +1618,33 @@
  msg.messages !=
+=
  null &&%0A       
@@ -1676,24 +1676,25 @@
 .messages !=
+=
  null &&%0A   
@@ -1748,16 +1748,17 @@
 sages !=
+=
  null) %7B
@@ -2013,16 +2013,17 @@
        %7D
+;
 %0A%0A%0A     

Fix JS style in messages controller