/*JSON -> HTML parser for Del.icio.us feeds*/





function sortByDesc(a, b) {

    var x = a.d.toLowerCase();

    var y = b.d.toLowerCase();

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}

function replaceEntity(strP){
	strP = strP.replace(/\&amp\;/g,"&");
	strP = strP.replace(/\&lt\;/g, "<");
	strP = strP.replace(/\&gt\;/g, ">");
	return strP = strP.replace(/\&quot\;/g, "")
}

function showImage(img){ return (function(){ img.style.display='inline'; }) }

function formatDelicJSON(container_id) {

  var ul = document.createElement('ul')

  var sposts = Delicious.posts.sort(sortByDesc)

    for (var i=0, post; post = sposts[i]; i++) {

        var li = document.createElement('li')

        var a = document.createElement('a')

		var p = document.createElement('p')

        /*a.style.marginLeft = '20px'*/

        var img = document.createElement('img')

        img.style.position = 'absolute'

        img.style.display = 'none'

        img.height = img.width = 16

        img.src = post.u.split('/').splice(0,3).join('/')+'/favicon.ico'

        /*img.onload = showImage(img);// We don't need the favicons*/

        a.setAttribute('href', post.u)

        a.appendChild(document.createTextNode(replaceEntity(post.d)))

        li.appendChild(img)

		p.appendChild(document.createTextNode(replaceEntity(post.n)))        

		li.appendChild(a)	

		li.appendChild(p)

        ul.appendChild(li)

    }

    document.getElementById(container_id).appendChild(ul)

}

