var express = require('express')
    , app = module.exports = express.createServer()
    , io = require('socket.io').listen(app)
    , _ = require('underscore');
// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'your secret here' }));
  app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

io.configure(function () {
  io.set("transports", ["xhr-polling"]);
  io.set("polling duration", 10);
  io.set('log level', 2)
});

// Routes

app.get('/', function(req, res){
  res.render('index');
});

app.post('/signup', function(req, res){
  var username = req.param('username');
  var id = 1 + Math.random() * 100000000000000000;
  var user = {id: id, name: username};
  users[id] = user;
  io.sockets.in('app').emit('server:new-user', user);
  req.session.user = user;
  res.redirect('/board');
});

app.get('/board', function(req, res){
  if (!req.session.user)  {
    res.redirect('/');
    return
  }
  res.render('board', { user: req.session.user});
});

var cards = {};
var users = {};

io.sockets.on('connection', function (socket) {

  socket.join('app'); // our app has one channel!

  var state = {
    cards: _.values(cards),
    users: _.values(users)
  }

  socket.emit('server:initial-state', state);

  socket.on('client:card-moving', function (card) {
    cards[card.id] = card;
    socket.broadcast.emit('server:card-moving', card);
  });

  socket.on('client:card-letgo', function(card) {
    cards[card.id] = card;
    socket.broadcast.emit('server:card-letgo', card);
  });

  socket.on('client:card-destroyed', function(card) {
    delete cards[card.id];
    socket.broadcast.emit('server:card-destroyed', card);
  });

  socket.on('client:cursor-movement', function (user) {
    users[user.id] = user;
    socket.broadcast.emit('server:cursor-movement', user);
  });

  socket.on('client:card-created', function(data) {
    cards[data.id] = data;
    socket.broadcast.emit('server:card-created', data);
  });

  socket.on('client:clear-board', function(data) {
    cards = {};
    socket.broadcast.emit('server:clear-board', data);
  });
});


var stitch  = require('stitch');

options = {
  paths : [__dirname + '/views/client'],

  compilers: {
    tmpl: function(module, filename)  {
      var source = require('fs').readFileSync(filename, 'utf8');
      source = source.replace(/\n/g, '').replace(/'/g, "\'")
      source = "module.exports = $.template('" + filename.replace(/.+\//g,'') + "', '" + source +"');";
      module._compile(source, filename);
    }
  }
}

var package = stitch.createPackage(options);

app.get('/templates', package.createServer());





var port = process.env.PORT || 3000;

app.listen(port);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

@@ -1722,34 +1722,47 @@
 te);%0A%0A  
-socket.on('client:
+var rebroadcastEvents = %7B%0A    '
 card-mov
@@ -1765,17 +1765,18 @@
 -moving'
-,
+ :
  functio
@@ -1780,17 +1780,16 @@
 tion
- 
 (card) %7B
 %0A   
@@ -1776,36 +1776,32 @@
 function(card) %7B
-%0A   
  cards%5Bcard.id%5D 
@@ -1811,91 +1811,17 @@
 ard;
-%0A    socket.broadcast.emit('server:card-moving', card);%0A  %7D);%0A%0A  socket.on('client:
+ %7D,%0A    '
 card
@@ -1827,17 +1827,18 @@
 d-letgo'
-,
+ :
  functio
@@ -1844,22 +1844,17 @@
 on(card)
- %7B%0A   
+%7B
  cards%5Bc
@@ -1872,90 +1872,17 @@
 ard;
-%0A    socket.broadcast.emit('server:card-letgo', card);%0A  %7D);%0A%0A  socket.on('client:
+ %7D,%0A    '
 card
@@ -1892,17 +1892,18 @@
 stroyed'
-,
+ :
  functio
@@ -1911,20 +1911,16 @@
 (card) %7B
-%0A   
  delete 
@@ -1938,94 +1938,17 @@
 id%5D;
-%0A    socket.broadcast.emit('server:card-destroyed', card);%0A  %7D);%0A%0A  socket.on('client:
+ %7D,%0A    '
 curs
@@ -1955,25 +1955,26 @@
 or-movement'
-,
+ :
  function (u
@@ -1974,17 +1974,16 @@
 tion
- 
 (user) %7B
 %0A   
@@ -1978,20 +1978,16 @@
 (user) %7B
-%0A   
  users%5Bu
@@ -2005,217 +2005,184 @@
 ser;
+ %7D,
 %0A    
-socket.broadcast.emit('server:cursor-movement', user);%0A  %7D);%0A%0A  socket.on('client:card-created', function(data) %7B%0A    cards%5Bdata.id%5D = data;%0A    socket.broadcast.emit('server:card-created', data);%0A  %7D);%0A%0A
+'card-created' : function(card) %7B cards%5Bcard.id%5D = card; %7D,%0A    'clear-board' : function() %7B cards = %7B%7D; %7D%0A  %7D;%0A%0A  _.each(rebroadcastEvents, function(fn, eventName)%7B%0A  
   so
@@ -2197,28 +2197,29 @@
 'client:
-clear-board'
+' + eventName
 , functi
@@ -2233,28 +2233,30 @@
 ) %7B%0A    
-cards = %7B%7D;%0A
+  fn(data);%0A  
     sock
@@ -2281,28 +2281,29 @@
 'server:
-clear-board'
+' + eventName
 , data);
@@ -2305,20 +2305,29 @@
 ata);%0A  
+ 
+ 
 %7D);%0A
+  %7D);%0A%0A
 %7D);%0A%0A%0Ava

refactor appjs rebroadcast events