/*
*  un lecteur de flux 
*
*  @author: turmel fabien <thegnou2@gmail.com>
*
*/
(function($)
{
  $.prototype.temps_ecoule=function()
  {
    now=new Date();
    return (now.getTime()-this.getTime())*1000/60;
  }
  $.prototype.aujourdhui=function(){
    now=new Date();
    if (this.getDate()==now.getDate() && this.getMonth()==now.getMonth() && this.getFullYear()== now.getFullYear())
    return true;
    return false;
  }
  $.prototype.hier=function(){
    now=new Date();
    if ((this.getDate()+1)==now.getDate() && this.getMonth()==now.getMonth() && this.getFullYear()== now.getFullYear())
    return true;
    return false;
  }
  $.prototype.abbrege=function(){
    localdate=this.toLocaleString();
    var localdate = localdate.replace(/\s/g,' ');
   	var words = localdate.split(' ');
	return words[1]+' '+ words[2]+' '+words[3];
  }
  $.prototype.humanise=function(){
    temps=this.temps_ecoule();
    if(temps<60){
      return "il y a "+ temps +" minutes";
    }
    if(temps>60 && temps<(60*4)){
      heures=temps/60;
      return "il y a "+ heures+" heures";
    }
    if(this.aujourdhui()){
      return "Aujourdhui à "+ this.getHours() +" h "+this.getMinutes();
    }
    if(this.hier()){
      return "Hier à "+  this.getHours() +" h "+this.getMinutes();
    }
    else
    {
      return this.abbrege();
    }
  
  }  
}
)(Date)



function lecteurflux(){
  var feed = new google.feeds.Feed("http://caenalekhine.actifforum.com/feed/");
  id="newsfeed";
  lire_posts(feed,"Caen Alekhine",id);
  feed = new google.feeds.Feed("http://echecsplus.forumpro.fr/feed/");
  lire_posts(feed, "Echecs Plus",id);
  feed = new google.feeds.Feed("http://echiquierdubocage.forum2jeux.com/feed/");
  lire_posts(feed,"Flers Briouze",id);
  
}

function flux_derniers_articles(){
  var feed =new google.feeds.Feed("http://caenalekhine.fr/caenalekhine-articles.xml")
  lire_posts(feed,"derniers articles","last");
}

function lire_posts(feed,title,id){
  var container = document.getElementById(id);
  posts=[];
  feed.load(function(result) {
    if (!result.error) {
      for (var i = 0; i < result.feed.entries.length; i++) {
        var post = result.feed.entries[i];
        posts[i]=post;
      }
      writeposts(container,posts,title);
    }
  });
  
}

function trimtext(text,length){
 	var t = text.replace(/\s/g,' ');
        var exp1=new RegExp("((ht|f)tps?://.*)[\\s]+","gi"); 
        t= text.replace(exp1," ")
 	t= text.replace(/<img /g,"<img width=160 ")
 	var words = t.split(' ');
	if(words.length<=length)	return t;
	var ret='';
	for(var i=0;i<length;i++){
		ret+=words[i]+' ';
	}
	return ret+'[...]';
}
	
function writeposts(container,posts,title){

 	var html = '<h3>'+title+'</h3>';
	html+="<ul>"
	for(var k in posts){
		html+=format(posts[k])
	}
	html += '</ul>';
	container.innerHTML+=html;
	
}
	
function format(post){
	var html='<li> <a href="'+post.link+'">'+post.title+'</a></li>';
	date=new Date(post.publishedDate)
	html+='<li><em> par '+post.author+' '+ date.humanise()+'</em></li>'
	html+='<li>'+trimtext(post.content,20)+'</li>'
	return html;	
}

