var sanitizer = require('sanitizer');
var _ = require("underscore");
var twitter = require('ntwitter');
var cronJob = require('cron').CronJob;
var fs = require('fs');
var events = require('events');
var eventEmitter = new events.EventEmitter();
var emotionKeywords = ['#amazed', '#angry', '#annoyed', '#awesome', '#awkward', '#bored', 
					   '#calm', '#confused', '#delighted', '#depressed', '#elated', 
					   '#excited', '#happy', '#helpless', '#hopeful', '#hurt', '#jealous', 
					   '#joyful', '#lonely', '#love', '#neat', '#nervous', 
					   '#proud', '#relaxed', '#sad', '#scared', '#sexy', '#sleepy', 
					   '#sorry', '#sweet', '#thrilled', '#upset' ];
var emotionList = {
	total: 0,
	keywords: {}
};

_.each(emotionKeywords, function (keyword) {
	emotionList.keywords[keyword] = 0;
});

function createStream(t) {
	t.stream('statuses/filter', { track: emotionKeywords }, function (stream) {
		stream.on('data', function (tweet) {
			if (tweet.text !== undefined) {
				var text = tweet.text.toLowerCase();
				var match = _.find(emotionKeywords, function (keyword) {
					return text.indexOf(keyword.toLowerCase()) !== -1;	
				});
				
				if (match) {
					emotionList.keywords[match] += 1;				
					emotionList.total += 1;
					eventEmitter.emit('update');
				}
			}
		});
	});	
}

fs.readFile('../../../shared/config/twitter.json', function (err, data) {
	var t;
	if (!err) {
		var auth = JSON.parse(data);
		t = new twitter(auth);

		if (t) {
			createStream(t);
		} else {
			t = new twitter(auth);
			if (t) {
				createStream(t);
			}
		}		
	}
});

new cronJob('0 0 0 * * *', function () {
    emotionList.total = 0;

    _.each(emotionKeywords, function(keyword) { 
    	emotionList.keywords[keyword] = 0; 
    });

    feeling.emit('data', emotionList);
}, null, true);

function how_were_feeling_get(request, response) {
	response.render("how_were_feeling/index", { data: emotionList });
}

function how_were_feeling_io(socket, io, feeling) {
	feeling.emit('data', { data: emotionList });

	eventEmitter.on('update', function () {
		feeling.emit('data', { data: emotionList });		
	});
}

function how_were_feeling_post(request, response, io) {}


exports.how_were_feeling_io = how_were_feeling_io;
exports.how_were_feeling_get = how_were_feeling_get;
@@ -156,24 +156,52 @@
 uire('fs');%0A
+var util = require('util');%0A
 var events =
@@ -2041,98 +2041,225 @@
 %7D);%0A
-%0A%09eventEmitter.on('update', function () %7B%0A%09%09feeling.emit('data', %7B data: emotionList %7D);%09%09
+%09%0A%09function sendOutData () %7B%0A%09%09feeling.emit('data', %7B data: emotionList %7D);%09%09%0A%09%7D%0A%0A%09eventEmitter.on('update', sendOutData);%0A%09%0A%09socket.on('disconnect', function () %7B%0A%09%09eventEmitter.removeListener('update', sendOutData);
 %0A%09%7D)

Fix Mem Leak