/*
    @author: Nicola Poluzzi
    @website: http://www.poluz.net/materiale:rss-ajax-parser
 
    Copyright (C) 2007  Nicola Poluzzi
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
 
 
function getRSS () 
{
    // al caricamento, mostra il throbber per alleggerire l'attesa
    document.getElementById("rss_display").innerHTML += "<div style='text-align: center;'><img style='padding: 60px 0 60px 0;' src='_pix/ajax-loader.gif' alt='caricamento...' /></div>";
	
    // carico l'xml chiamando lo script php
    if (window.ActiveXObject) 
	{
        //IE
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    } 
	else if (window.XMLHttpRequest) 
	{
        //altri
        xhr = new XMLHttpRequest();
    } 
	else 
	{
        document.getElementById("rss_display").innerHTML = "<p>Si è verificato un errore nella ricezione delle informazioni</p>";
    }
 
    xhr.open("GET", "_getCachedRss.php", true);
 
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("Pragma", "no-cache");
 
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                if (xhr.responseText != null) {
                    processRSS(xhr);
                } else {
                    document.getElementById("rss_display").innerHTML += "<p>Impossibile ricevere le informazioni. Il server ha restituito una risposta vuota.</p>";
                    return false;
                }
            } else {
                document.getElementById("rss_display").innerHTML += "<p>Ricevuto un errore " + xhr.status + ": " + xhr.statusText + " </p>";
            }
        }
    }
 
    xhr.send(null);
}
 

function processRSS (xmlhttp) 
{
	var rssxml = xmlhttp.responseXML;
	var rsshtml = "<ul class=\"from_twitter\">\n";
	var maxitems = 5;
	var items = rssxml.getElementsByTagName("item").length;
	if (items > 0)
	{
		var displayeditems = 0;
		for (var i=0; i<items; i++)
		{
			var elemento = rssxml.getElementsByTagName("item")[i];
			var descrizione = elemento.getElementsByTagName("description")[0].firstChild.nodeValue;
			var d = descrizione.toString();
			if(d.charAt(11) != "@")
			{			
				var data = elemento.getElementsByTagName("pubDate")[0].firstChild.nodeValue;
					data = data.replace("[[","<");
					data = data.replace("]]",">");
			
				var link = elemento.getElementsByTagName("link")[0].firstChild.nodeValue;
				var datalink = "<small>"+ data + "</small>";
					descrizione = descrizione.substring(12); //cambiare il valore numerico con la lunghezza del nome utente;
				if (i== 0) {descrizione = "<b>"+descrizione+"</b>";}
				var rsshtmlitem =  "\n\t<li><p class='tweet'>" + descrizione + datalink + "</p></li>\n";
				rsshtml = rsshtml + rsshtmlitem;
				displayeditems++;
			}
			if (displayeditems == maxitems) break;
		}
		rsshtml = rsshtml + "\n</ul>";
		$(".from_twitter").show("slow");
	}
	else
	{
		rsshtml = "<ul class=\"from_twitter\"><li><p>Momentaneamente non disponibile.</p></li></ul>";
	}
    document.getElementById("rss_display").innerHTML = rsshtml;
    return true;
 
}