// lastmod 23 settembre 2008

// Tratto da http://code.google.com/apis/gdata/samples/blogger_sample.html
// con adattamenti: visualizza solo i post degli ultimi "global_max_giorni" giorni
// nel div identificato dalla stringa contenuta in "global_data_div"


/**
 * Lists blog entries from the specified JSON feed
 * by creating a new 'ul' element in the DOM.  Each
 * bullet is the title of one blog entry, and contains
 * a hyperlink to that entry's URL.
 *
 * @param {JSON} json is the JSON object pulled from the Blogger service.
 */
function listEntries(json) {
    removeOldResults();
    var a,
	adesso=new Date(),
	alturl,
	datapost,
	day,
	entry,
	giorni,
	li,
	month,
	txt,
	ul = document.createElement('ul'),
	published,
	x,
	year;

    for (var i = 0; i < json.feed.entry.length; i++) {
	entry = json.feed.entry[i];

	year=entry.updated.$t.substr(0,10).split('-')[0];
	month=entry.updated.$t.substr(0,10).split('-')[1]-1;
	day=entry.updated.$t.substr(0,10).split('-')[2];
	datapost=new Date(year,month,day);
	giorni= Math.round((adesso-datapost)/1000/60/60/24);
	if (giorni>global_max_giorni) continue;

	for (var k = 0; k < entry.link.length; k++) {
	    if (entry.link[k].rel == 'alternate') {
		alturl = entry.link[k].href;
		break;
	    }
	}

	li = document.createElement('li');
	a = document.createElement('a');
	a.href = alturl;
	a.target = '_blank';
	x=entry.published.$t.substr(0,10).split('-')
	published= x[2] + '/' + x[1] + '/' + x[0];
	a.title =  '[' + published + '] ' + entry.content.$t.replace(/(<([^>]+)>)/ig,"").substr(0,256);

	txt = document.createTextNode(entry.title.$t);
	a.appendChild(txt);
	li.appendChild(a);

	ul.appendChild(li);
    }

    ul.setAttribute('id', 'blog_news');

    // Install the bullet list of blog posts.
    document.getElementById(global_data_div).appendChild(ul);
}

/**
 * Deletes pre-existing children of the data div from the page. The data div 
 * may contain a "Loading..." message, or the results of a previous query. 
 * This old data should be removed before displaying new data.
 */
function removeOldResults() {
  var div = document.getElementById(global_data_div);
  if (div.firstChild) {
    div.removeChild(div.firstChild);
  }
}
