var config = require('config').config

var express = require('express')
  , stylus = require('stylus')
  , jade = require('jade')
  , auth = require('connect-auth')
  , mongoose = require('mongoose')
  , MongoStore = require('connect-mongodb')
  , helpers = require(__dirname + '/app/helpers/helpers')
  , log = require('logging').from(__filename)
  , NotFound = require(__dirname + '/app/lib/errors').NotFound
  , app = express.createServer()

var airbrake = require('airbrake').createClient(config.airbrakeKey)
airbrake.handleExceptions()

app.use(stylus.middleware({ src: __dirname + '/app'
                          , dest: __dirname + '/public'
                          , compress: true
                          , debug: false
                          }))

app.configure(function(){
  app.use(express.cookieParser())
  app.use(express.bodyParser())
  app.use(express.methodOverride())
  //app.use(express.logger())

  app.dynamicHelpers(helpers.dynamicHelpers)
  app.helpers(helpers.helpers)

  app.set('view engine', 'jade')
  app.set('views', __dirname + '/app/views');

  app.use(express.session({ secret: config.sessionSecret }))

  app.use(auth([
      auth.Facebook({ appId: config.fbAppId
                    , appSecret: config.fbAppSecret
                    , scope: 'email'
                    , callback: config.fbCallbackAddress
                    })
               ]))
})


app.error(function(err, req, res, next){
  if (err instanceof NotFound) {
    res.render('404', { status: 404 })
  } else {
    res.render('500', { status: 500 })

    if (err instanceof Error) {
      if (config.airbrakeEnabled) {
        airbrake.notify(err)
      }

      log(err.stack)
    } else {
      log(err)
    }
  }
})

app.configure('development', function() {
  app.use(express.static(__dirname + '/public'))
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
  app.set('view cache', false)
})

app.configure('testing', function() {
  app.use(express.static(__dirname + '/public'))
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
  app.set('view cache', false)
})

app.configure('production', function() {
  const oneYear = 31557600000
  app.use(express.static(__dirname + '/public', { maxAge: oneYear }))
  app.use(express.errorHandler())
  app.set('view cache', true) 
})

mongoose.connect('mongodb://localhost/' + config.database)

require(__dirname + '/app/models/models')(mongoose)
require(__dirname + '/config/routes')(app, mongoose)

app.db = mongoose

module.exports = app

@@ -436,16 +436,58 @@
 Server()
+%0A  , MongoStore = require('connect-mongo')
 %0A%0Avar ai
@@ -1120,70 +1120,8 @@
 );%0A%0A
-  app.use(express.session(%7B secret: config.sessionSecret %7D))%0A%0A
   ap
@@ -1737,32 +1737,94 @@
 ', function() %7B%0A
+  app.use(express.session(%7B secret: config.sessionSecret %7D))%0A%0A
   app.use(expres
@@ -1996,32 +1996,94 @@
 ', function() %7B%0A
+  app.use(express.session(%7B secret: config.sessionSecret %7D))%0A%0A
   app.use(expres
@@ -2266,16 +2266,172 @@
 ion() %7B%0A
+  app.use(express.session(%7B%0A    secret: config.sessionSecret,%0A    store: new MongoStore(%7B%0A      url: 'mongodb://localhost/' + config.database%0A    %7D)%0A  %7D))%0A%0A
   const 
@@ -2552,24 +2552,24 @@
 rHandler())%0A
+
   app.set('v
@@ -2585,17 +2585,16 @@
 ', true)
- 
 %0A%7D)%0A%0Amon

add mongostore for production