// recent-delicious.js

var MAX_DELICIOUS = 10;
var DELICIOUS_JSON = "http://www.kawa.net/rss/recent-delicious.json";

function delicious_update ( data, area ) {
    if ( ! data ) return;

    var items = data.rss.channel.item;
    if ( ! items ) return;
    if ( items.title ) items = [ items ];
    if ( items.length > MAX_DELICIOUS ) items.length = MAX_DELICIOUS;

    var frag = document.createDocumentFragment();

    for( var i=0; i<items.length; i++ ) {
        if ( ! items[i].link ) continue;
        var title = items[i].title;
        if ( ! title ) title = items[i].link;
        var atag = document.createElement( "a" );
        var txt1 = document.createTextNode( title );
        var divtag = document.createElement( "div" );
        atag.href = items[i].link;
        if ( items[i].description ) atag.title = items[i].description;
        atag.appendChild( txt1 );
        divtag.className = 'barlink';
        divtag.appendChild( atag );
        frag.appendChild( divtag );
    }

    delicious_display( area, frag );
};

function delicious_load ( area ) {
    delicious_display( area, "Now loading..." );
    var http = new JKL.ParseXML.JSON( DELICIOUS_JSON );
    var func = function ( data ) {
        delicious_update( data, area );
    };
    var err = function ( err ) {
        delicious_display( area, "[error] "+err );
    };
//  http.onerror( err );
    http.async( func );
    http.parse();
};

function delicious_display ( area, elem ) {
    if ( typeof(elem) == "string" ) {
        elem = document.createTextNode( elem );
    }
    var here = document.getElementById(area);
    if ( here.firstChild ) {
        here.replaceChild( elem, here.firstChild );
    } else {
        here.appendChild( elem );
    }
}