var deprecation_message = require("sys");
var my_http = require("http");
var url = require("url");

var Firebase = require("firebase")
var WebSocketServer = require('websocket').server;
var reqobj = require("request")

var server = my_http.createServer(function(request,response){
    var fb = new Firebase("https://radiant-torch-7198.firebaseio.com");
    //TODO serve front-end objects
}).listen(3000,'0.0.0.0');

wsServer = new WebSocketServer({
    httpServer: server
});

var history = [];
var clients = [];

wsServer.on('request', function(request) {
    var connection = request.accept(null, request.origin);
    var index = clients.push(connection) - 1;
    var username = false;
    var result = false;

    connection.on('message', function(message) {
        if(message.type === 'utf8') {
            var obj = message.utf8Data;
            var json_msg = JSON.parse(obj);
            username = json_msg.username;
            console.log(json_msg);

	    //TODO put message into firebase, see if message parts need to be picked out piece by piece`    

            for(var i = 0; i < clients.length; i++) {
                clients[i].sendUTF(obj);
            }
        }
    });

    connection.on('close', function(reasonCode, description) {
        for(var i = 0; i < clients.length; i++) {
            clients[i].sendUTF(JSON.stringify({message: "User ",username, "disconnected."}));
        }
    });
});

@@ -1,27 +1,11 @@
 var 
-deprecation_message
+sys
  = r
@@ -17,22 +17,18 @@
 e(%22sys%22)
-;%0Avar 
+,%0A
 my_http 
@@ -44,22 +44,42 @@
 (%22http%22)
-;%0Avar 
+,%0Apath = require(%22path%22),%0A
 url = re
@@ -286,106 +286,520 @@
 var 
-fb = new Firebase(%22https://radiant-torch-7198.firebaseio.com%22);%0A    //TODO serve front-end objects
+my_path = url.parse(request.url).pathname;%0A    var full_path = path.join(process.cwd(),my_path);%0A    path.exists(full_path, function(exists)%7B%0A%09      if(!exists)%7B%0A%09          response.writeHeader(404, %7B%22Content-Type%22: %22text/plain%22%7D);%0A%09          response.write(%22404 Not Found%5Cn%22);%0A%09          response.end();%0A%09      %7D%0A%09      else%7B%0A%09          // a reference here doesn't open a connection until r/w ops are invoked%0A%09          var fb = new Firebase(%22https://improvplusplus.firebaseio.com%22)%0A%09          %0A%09      %7D%0A    %7D);
 %0A%7D).
@@ -1373,111 +1373,8 @@
 );%0A%0A
-%09    //TODO put message into firebase, see if message parts need to be picked out piece by piece%60    %0A%0A
     
@@ -1661,36 +1661,43 @@
 fy(%7B
-message: %22User %22,username, %22
+username: username, message: 'User 
 disc
@@ -1705,17 +1705,17 @@
 nnected.
-%22
+'
 %7D));%0A   

add username to disconnect