var rss = require('rssee.js').create({'interval':15}),
    sys = require('sys');

rss.on('article', function(a) {
	console.log('Une news : ' + sys.inspect(a));
});
rss.start('http://localhost:8181/rss.xml');
rss.on('article', function(a) {
    socket.sockets.emit("myrss","▸ "+a);
    console.log(sys.inspect(a)+"\n");
socket.on('myrss', function(json) {
 console.log("myrss");
 console.log(json);
 $("<li></li>").html(json)
      	       .prependTo("#myrss")
.css({opacity:0}).slideDown("slow").animate({opacity:1},"slow");
});
var rss1 	= require('rssee').create({'interval':30}),
    rss2 	= require('rssee').create({'interval':30}),
    rss3 	= require('rssee').create({'interval':30}),
	 sys  	= require('sys'),
	 express = require('express');


function linkify(text) {
	return '<a href="'+text.link+'" target="_blank">'+text.title+'</a>';
}

app = express.createServer();
app.configure(function(){
  app.use(express.static(__dirname + '/rss'));
});
app.get('/', function(req, res, next){
  res.render('/rss/index.html');
});
app.listen(8080);
console.log('Server running at http://localhost:8080/');

var socket  = require('socket.io').listen(app);
socket.set('log level', 1);

rss1.on('article', function(a) {
    socket.sockets.emit("hackernews","▸ "+linkify(a));
    console.log(sys.inspect(a)+"\n");
});
rss2.on('article', function(a) {
    socket.sockets.emit("lejournaldugeek","▸ "+linkify(a));
    console.log(sys.inspect(a)+"\n");
});
rss3.on('article', function(a) {
    socket.sockets.emit("reddit","▸ "+linkify(a));
    console.log(sys.inspect(a)+"\n");
});
rss1.start('http://news.ycombinator.com/rss');
rss2.start('http://feeds.feedburner.com/LeJournalduGeek');
rss3.start('http://www.reddit.com/.rss');

(function(document, window, undefined) {
 var script = document.createElement("script");
 script.src = "http://code.jquery.com/jquery.min.js";
 script.onload = function() {
 $.getScript("http://127.0.0.1:8080/socket.io/socket.io.js", function() {
 var socket = io.connect('http://127.0.0.1:8080');
   	socket.on('hackernews', function(json) {
			console.log("hackernews");
      	console.log(json);
			$("<li></li>").html(json)
       		.prependTo("#hackernews")
       		.css({opacity:0}).slideDown("slow").animate({opacity:1},"slow");
   		});
   	socket.on('lejournaldugeek', function(json) {
			console.log("lejournaldugeek");
      	console.log(json);
			$("<li></li>").html(json)
       		.prependTo("#lejournaldugeek")
       		.css({opacity:0}).slideDown("slow").animate({opacity:1},"slow");
   		});
   	socket.on('reddit', function(json) {
			console.log("reddit");
      	console.log(json);
			$("<li></li>").html(json)
       		.prependTo("#reddit")
       		.css({opacity:0}).slideDown("slow").animate({opacity:1},"slow");
   		});
   	socket.on("disconnect", function() {
		console.log("disconnected");
   });
  });
 };
 document.getElementsByTagName("head")[0].appendChild(script);
})(document, window);