var http = require('http');
var Router = require('node-simple-router');
var fs = require('fs');
var router = Router();

var indexHTML = fs.readFileSync('search.html').toString();
router.get('/', function (request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });
  response.end(indexHTML);
});

var firstPassHTML = fs.readFileSync('index.html').toString();
router.get('/first/:videoIndex/:id', function (request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });
  response.end(firstPassHTML);
});

router.post('/first', function (request, response) {
  var stats = JSON.parse(request.post.stats);
  var transcriptions = JSON.parse(request.post.transcriptions);
  var captionFileName = stats.video.replace(/\ /g,"_") + "-" + stats.name + ".json";
  var statsFileName = stats.video.replace(/\ /g,"_") + "-" + stats.name + ".json";
  fs.writeFileSync("captions/first/" + captionFileName, request.post.transcriptions, {mode: 0777});
  fs.writeFileSync("stats/first/" + statsFileName, request.post.stats, {mode: 0777});
  response.writeHead(200, {
    'Content-Type': 'application/json'
  });
  response.end(JSON.stringify({success: true}));
});

router.post('/second', function (request, response) {
  var stats = JSON.parse(request.post.stats);
  var transcriptions = JSON.parse(request.post.transcriptions);
  var captionFileName = stats.video.replace(/\ /g,"_") + "-" + stats.name + ".json";
  var statsFileName = stats.video.replace(/\ /g,"_") + "-" + stats.name + ".json";
  fs.writeFileSync("captions/second/" + captionFileName, request.post.transcriptions, {mode: 0777});
  fs.writeFileSync("stats/second/" + statsFileName, request.post.stats, {mode: 0777});
  response.writeHead(200, {
    'Content-Type': 'application/json'
  });
  response.end(JSON.stringify({success: true}));
});

var secondPassHTML = fs.readFileSync('editor.html').toString();
router.get('/second/:videoIndex/:id', function (request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });
  response.end(secondPassHTML);
});

var server = http.createServer(router);
server.listen(80);
@@ -498,32 +498,145 @@
 pe': 'text/html'
+,%0A    %22Access-Control-Allow-Origin%22 : %22*%22,%0A    %22Access-Control-Allow-Methods%22 : %22POST, GET, PUT, DELETE, OPTIONS%22
 %0A  %7D);%0A  respons
@@ -2137,32 +2137,145 @@
 pe': 'text/html'
+,%0A    %22Access-Control-Allow-Origin%22 : %22*%22,%0A    %22Access-Control-Allow-Methods%22 : %22POST, GET, PUT, DELETE, OPTIONS%22
 %0A  %7D);%0A  respons
@@ -2360,8 +2360,9 @@
 ten(80);
+%0A

Allow CORS video loading