// Quick and Dirty hack to parse atom feeds from blogger/feedburner
var doc = document;

function setFeed(elemId, feedUrl, amount)
{
    div = doc.getElementById(elemId);
    delFeed(div);
    div.appendChild(doc.createTextNode('Trying to retrieve the feed entries from the server...'));
    getAjaxFeed(div, feedUrl, amount);
}

function getDocFeed(div, feedUrl, amount)
{
    // does not work for now...
    if(window.ActiveXObject)
    {
	var xml = new ActiveXObject("Microsoft.XMLDOM");
        xml.async = false;
    }
    else if(doc.implementation && doc.implementation.createDocument)
    {
        xml = doc.implementation.createDocument("", "doc", null);
    }
    xml.load("/feed.php?url=" + feedUrl);
    items = xml.getElementsByTagName('entry');
    delFeed(div);
    parseFeed(div, items, amount);
}

function getAjaxFeed(div, feedurl, amount)
{
    var req = new XMLHttpRequest();

    if (req)
    {
	req.onreadystatechange = function()
	{
	    if (req.readyState == 4 && req.status == 200)
	    {
		delFeed(div);
		res = req.responseXML;
		items = res.getElementsByTagName('entry');
		parseFeed(div, items, amount);
	    }
	};
		
	// ---
	req.open('GET', "/feed.php?url=" + feedurl);
	req.send(null);
	// ---
    }
}


function delFeed(div)
{
    while (div.hasChildNodes())
    {
	div.removeChild(div.firstChild);
    }
}

function parseFeed(div, items, amount)
{
    delFeed(div);
    var itemdate=new Array();

    for(i = 0; i < amount; i++)
    {
        fitem = doc.createElement('span');
        attribute = doc.createAttribute('class');
        attribute.value = "feed";
        fitem.setAttributeNode(attribute);

        itemtitle = items[i].getElementsByTagName('title')[0].firstChild.data;
        ititle = doc.createElement('span');
        attribute = doc.createAttribute('class');
        attribute.value = "title";
        ititle.setAttributeNode(attribute);
        ititle.appendChild(doc.createTextNode(itemtitle));
        fitem.appendChild(ititle);

        itemdescr = items[i].getElementsByTagName('content')[0].firstChild.data;
        idescr = doc.createElement('span');
        attribute = doc.createAttribute('class');
        attribute.value = "entry";
        idescr.setAttributeNode(attribute);
        idescr.innerHTML = itemdescr;
        //idescr.appendChild(doc.createTextNode(itemdescr));
        fitem.appendChild(idescr);

        temp = items[i].getElementsByTagName('published')[0].firstChild.data;
        itemdate[0] = temp.substring(0,temp.indexOf("T"));
        tempi = temp.indexOf("T");
        itemdate[1] = temp.substring(tempi+1,tempi+6);
					 
        idate = doc.createElement('span');
        attribute = doc.createAttribute('class');
        attribute.value = "date";
        idate.setAttributeNode(attribute);
        idate.appendChild(doc.createTextNode("Published at " + itemdate[1] + " on " + itemdate[0]));
        fitem.appendChild(idate);

        div.appendChild(fitem);
    }
}