function gfeedfetcherTITULARES(divid){
  this.feedlabels=[] // array holding lables for each RSS feed
  this.feedurls=[]
  this.feeds=[] // array holding combined RSS feeds' entries from Feed API (result.feed.entries)
  this.feedsfetched=4; // number of feeds fetched
  this.feedlimit=10;
  this.showoptions="" // Optional components of RSS entry to show (none by default)
  this.sortstring="date" // sort by "date" by default
  this.feedcontainer=document.getElementById(divid); // el DIV del resumen
}

gfeedfetcherTITULARES.prototype.addFeed=function(label,url){
 	this.feedlabels[this.feedlabels.length]=label;
 	this.feedurls[this.feedurls.length]=url
}

gfeedfetcherTITULARES.prototype.filterfeed=function(feedlimit, sortstr){
	this.feedlimit=feedlimit
	if (typeof sortstr!="undefined")
	this.sortstring=sortstr
}

gfeedfetcherTITULARES.prototype.displayoptions=function(parts){
	this.showoptions=parts //set RSS entry options to show ("date, datetime, time, snippet, label, description")
}

gfeedfetcherTITULARES.prototype.init=function(){
	this.feedsfetched=0 //reset number of feeds fetched to 0 (in case init() is called more than once)
	this.feeds=[] //reset feeds[] array to empty (in case init() is called more than once)
	this.feedcontainer.innerHTML='<img class="loadingICO" src="http://bolsanegra.net/wp-content/themes/bolsaNegraHome/images/BNP_loadingTitulares.gif" />'
	var displayer=this
	for (var i=0; i<this.feedurls.length; i++){ //loop through the specified RSS feeds' URLs
		var feedpointer=new google.feeds.Feed(this.feedurls[i]) //create new instance of Google Ajax Feed API
		var items_to_show=(this.feedlimit<=this.feedurls.length)? 1 : Math.floor(this.feedlimit/this.feedurls.length) //Calculate # of entries to show for each RSS feed
		if (this.feedlimit%this.feedurls.length>0 && this.feedlimit>this.feedurls.length && i==this.feedurls.length-1) //If this is the last RSS feed, and feedlimit/feedurls.length yields a remainder
			items_to_show+=(this.feedlimit%this.feedurls.length) //Add that remainder to the number of entries to show for last RSS feed
		feedpointer.setNumEntries(items_to_show) //set number of items to display
		feedpointer.load(function(r){displayer._fetch_data_as_array(r)}) //call Feed.load() to retrieve and output RSS feed
	}
}

gfeedfetcherTITULARES._formatdate=function(datestr, showoptions){
	var itemdate=new Date(datestr)
	var parseddate=(showoptions.indexOf("datetime")!=-1)? itemdate.toLocaleString() : (showoptions.indexOf("date")!=-1)? itemdate.toLocaleDateString() : (showoptions.indexOf("time")!=-1)? itemdate.toLocaleTimeString() : ""
	var itemdate2=new Date(datestr)
	var parseddate2 = isodatetime(itemdate2)
	var prueba = parseddate2.substring(0,19)
	prueba = prueba + "Z"
	var parseddate3= prettyDate(prueba)
	return "<span class='fechaT'>" + parseddate3 + "</span>"
}

gfeedfetcherTITULARES._sortarray=function(arr, sortstr){
	var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
	if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
		arr.sort(function(a,b){
		var fielda=a[sortstr].toLowerCase()
		var fieldb=b[sortstr].toLowerCase()
		return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
		})
	}
	else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
		try{
			arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
		}
		catch(err){}
	}
}

gfeedfetcherTITULARES.prototype._fetch_data_as_array=function(result){
	var thisfeed=(!result.error)? result.feed.entries : "" //get all feed entries as a JSON array or "" if failed
	if (thisfeed=="") //if error has occured fetching feed
		alert("Google Feed API Error: "+result.error.message)
	for (var i=0; i<thisfeed.length; i++) //For each entry within feed
	result.feed.entries[i].ddlabel=this.feedlabels[this.feedsfetched] //extend it with a "ddlabel" property
	this.feeds=this.feeds.concat(thisfeed) //add entry to array holding all feed entries
	this._signaldownloadcomplete() //signal the retrieval of this feed as complete (and move on to next one if defined)
}

gfeedfetcherTITULARES.prototype._signaldownloadcomplete=function(){
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._displayresult(this.feeds) //display results
}

gfeedfetcherTITULARES.prototype._displayresult=function(feeds){
  gfeedfetcherTITULARES._sortarray(feeds, this.sortstring)

      var rssoutput="<ul>";
      var avatar = "";
      var elacceso = "";
      for (var i=0; i<feeds.length; i++){
        switch(this.feeds[i].ddlabel) {
          case "jmiur":
            avatar = "jmiur";
            elacceso='JMiur en <a href=\"http://vagabundia.bolsanegra.net/\" target=\"_blank\" class=\"urlT\">Vagabundia</a>' ;
            break;
          case "cata":
            avatar = "cata";
            elacceso='CaTa en <a href=\"http://infinitosmundos.bolsanegra.net/\" target=\"_blank\" class=\"urlT\">Infinitos Mundos</a>' ;
            break;
          case "salva":
            avatar = "salva";
            elacceso='Salva en <a href=\"http://salvagoxo.bolsanegra.net/\" target=\"_blank\" class=\"urlT\">Salvagoxo.blog</a>' ;
            break;
          case "nico":
            avatar = "nico";
            elacceso='Nico en <a href=\"http://himsomnio.com/\" target=\"_blank\" class=\"urlT\">Himsomnio</a>' ;
            break;

        }
        var itemthumb = "<img src='http://bolsanegra.net/wp-content/themes/bolsaNegraHome/images/iconos/" + avatar + "16.jpg' />";
        var itemtitle="<a href=\"" + feeds[i].link + "\" target=\"_blank\" class=\"tituloT\">" + feeds[i].title + "</a>"
        var itemdate=gfeedfetcherTITULARES._formatdate(feeds[i].publishedDate, this.showoptions)
        var itemdescription=/description/i.test(this.showoptions)? ""+feeds[i].content : /snippet/i.test(this.showoptions)? "\n" + feeds[i].contentSnippet  : ""
        rssoutput += '<li><a title="Click para ver un resumen" onclick="new Effect.toggle(\'' + avatar + '\',\'blind\'); return false;" class="avatarT" href="#">' + itemthumb + itemtitle + '</a>' + ' - ' + elacceso + ' - ' + itemdate + '</li>';
        rssoutput += '<div id="' + avatar +'" class="descripcionT" style="display:none">' + itemdescription + '</div>';    
      }
      rssoutput+="</ul>";
      this.feedcontainer.innerHTML=rssoutput;

}

// --------------------------------------------------------- MOSTRAR FEEDS EN GENERAL ---------------------------------------------------------
	// la funcion no tiene parametros
function bsn_titulares(){
  var newsfeed = new gfeedfetcherTITULARES("listatitulares"); // crear el objeto
  newsfeed.addFeed("cata", "http://feeds.feedburner.com/InfinitosMundos");
  newsfeed.addFeed("jmiur", "http://feeds.feedburner.com/bolsanegra/POCj");
  newsfeed.addFeed("salva", "http://feeds.feedburner.com/salvagoxo");
  newsfeed.addFeed("nico", "http://feeds.feedburner.com/himsomnio");
  newsfeed.displayoptions("label datetime snippet"); 
  newsfeed.filterfeed(4, "date"); // la cantidad de feeds
  newsfeed.init();
}
